如何获得pIplImage中的颜色百分比(即使用Delphi),如图像中红色(clRed),绿色(clGreen),蓝色(clBlue)的百分比。
我想用OpenCv模拟这个img.Canvas.Pixels[0, 0]
,但不知道如何逐像素地浏览图像但不知道如何。
为例:
procedure TForm1.Button4Click(Sender: TObject);
var i, j, r, b, g, tot : integer;
begin
r := 0; b := 0; g := 0;
for i := 0 to Image1.Width - 1 do
for j := 0 to image1.Height - 1 do
if image1.Canvas.Pixels[i,j] = clRed then
inc(r)
else if image1.Canvas.Pixels[i,j] = clBlue then
inc(b)
else if image1.Canvas.Pixels[i,j] = clGreen then
inc(g);
tot := Image1.Width * Image1.Height;
showmessage('Red %:' + IntToStr(r div tot)+#13#10+
'Green %:'+IntToStr(g div tot)+#13#10+
'Blue %:'+IntToStr(b div tot));
end;
在我的搜索中,我发现我必须将pIplImage
转换为Mat(使用cvMat()
),使用cvGet2D将其拆分为数组,以获得4个数组,RGB和灰色。但下一步并不知道什么......
var
mat : TCvMat;
res : TCvScalar;
begin
object_filename := 'd:\1.bmp';
image := cvLoadImage(pcvchar(@object_filename[1]), CV_LOAD_IMAGE_UNCHANGED);
mat := cvMat(image.width, image.height, 0, image);
for i := 0 to mat.rows - 1 do
for j := 0 to mat.cols - 1 do
begin
res := cvGet2D(@mat, i, j);
writeln('red: ', res.val[0]:2:2);
writeln('green: ', res.val[1]:2:2);
writeln('Blue: ', res.val[2]:2:2);
writeln('Gray: ', res.val[3]:2:2);
writeln;
end;
readln;
end.
但是只返回val [0]和实际值,其余都是零。
最后,使用带有OpenCV的pIplImage计算颜色百分比的最快方法是什么。更少的时间,记忆,cpu ...
答案 0 :(得分:0)
以下是解决方案:
var
img : pIplImage;
channels: array [0 .. 2] of pIplImage;
BGRSum : Array [0 .. 2] of TCvScalar;
i, total: Integer;
begin
try
img := cvLoadImage(filename, 1);
for i := 0 to 2 do
channels[i] := cvCreateImage(cvGetSize(img), 8, 1);
cvSplit(img, channels[0], channels[1], channels[2], 0);
for i := 0 to 2 do
BGRSum[i] := cvSum(channels[i]);
total := img^.width * img^.height * 255;
WriteLn('Color percentage of RGB(ex red 50%, green 25% and blue %25) in an image is');
writeln('red: ', BGRSum[2].val[0] / total * 100:2:2);
writeln('green: ', BGRSum[1].val[0] / total * 100:2:2);
writeln('blue: ', BGRSum[0].val[0] / total * 100:2:2);
readln;
except
on E: Exception do
writeln(
E.ClassName, ': ', E.Message);
end;
end.
感谢Laex