如何快速判断一幅图象中某种颜色的多少?(100分)

  • 主题发起人 主题发起人 aaycsdn
  • 开始时间 开始时间
A

aaycsdn

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.BitBtn1Click(Sender: TObject);
var t1,i,j,s:integer;
begin
s:=0;
t1:=GetTickCount;
with image1.Picture do begin
for i:=0 to Width -1 do
for j:=0 to Height -1 do
if Bitmap.Canvas .Pixels[i,j]=clred then
s:=s+1;
end;
t1:=GetTickCount-t1;
end;
结果,图象为200*200的,用120-130ms!
如何才能提高速度?
 
用扫描线,假设你的图象是真彩图 :

var
i,j,t1,s :Integer;
P :PByteArray;
begin
s :=0
t1:=GetTickCount;
with image1.Picture do
begin
for j :=0 to Bitmap.Height-1 do
begin
P :=Bitmap.ScanLine[j];
for i :=0 to Bitmap.Width-1 do
begin
if P[3*i]=255 then
Inc(s);
end;
end;
end;
t1:=GetTickCount-t1;
end;
 
对楼上的补充:
>> if P[3*i]=255 then
>> Inc(s);
这两句有问题,要统计颜色这样也太简单点吧。
反正是要很长的一段,P[3*i],P[3*i+1],P[3*i+2]就代表了一种颜色,
然后你把它用数组统计一下。
 
呵呵,一点小小的疏漏,谢谢提醒。

比如要找红色,那么 :

if (P[3*i]=255) and (P[3*i+1]=0) and (P[3*i+2]=0) then
Inc(s);
 
问题中、、、
 
16位增强色。如何??
 
如果你对精度要求不高,你可以转化为真彩色;
或者你好好看看这里的帖子,我说的很详细了 :

http://www.delphibbs.com/delphibbs/dispq.asp?lid=530544
 
多人接受答案了。
 
后退
顶部