delphi之模糊找图
AutoHotkey的源碼,模糊找圖和精確找圖思路一樣,也是用笨方法。原來的C代碼比較難看懂,這里的delphi代碼,很容易弄明白。
以下是模糊的找圖。如果需要,可以再做優化處理。注意我這里去掉了透明處理,需要的自己加上吧。
因為用到了iif函數,別忘了uses IdGlobal;
// 模糊判斷,在大圖里的(x,y)位置上是不是小圖?
// 其中nV是R,G,B的偏差值,0..255
function BmpCmpEx(bmpBig,bmp:TBitmap;x,y:integer;nV:byte):boolean;
var??????????????????????????????????
? i,j:integer;
? row1, row2:pRGBTripArray;
? p1,p2:TRGBTriple;
? pLow,pHigh:TRGBTriple;
begin
? result:=true;
? for j:=0 to bmp.Height-1 do
? begin
??? row1:=bmpBig.ScanLine[y+j];
??? row2:=bmp.ScanLine[j];
??? for i:= 0 to bmp.Width-1 do
??? begin
????? p1:=row1[x+i];
????? p2:=row2[i];
????? // uses IdGlobal, 如果使用IfThen代替iif, Uses Math
?????? ? pLow.rgbtRed? := iif(nV > p2.rgbtRed,? 0 , p2.rgbtRed - nV);
?????? ? pLow.rgbtGreen := iif(nV > p2.rgbtGreen, 0 , p2.rgbtGreen - nV);
?????? ? pLow.rgbtBlue ?:= iif(nV > p2.rgbtBlue , 0 , p2.rgbtBlue - nV);
?????? ? pHigh.rgbtRed? := iif((nV > $FF - p2.rgbtRed) , $FF , p2.rgbtRed + nV);
?????? ? pHigh.rgbtGreen := iif((nV > $FF - p2.rgbtGreen) , $FF , p2.rgbtGreen + nV);
?????? ? pHigh.rgbtBlue ?:= iif((nV > $FF - p2.rgbtBlue) , $FF , p2.rgbtBlue + nV);
????? if not ((p1.rgbtRed>=pLow.rgbtRed) and (p1.rgbtRed<=pHigh.rgbtRed)
??????? and (p1.rgbtGreen>=pLow.rgbtGreen) and (p1.rgbtGreen<=pHigh.rgbtGreen)
??????? and (p1.rgbtBlue>=pLow.rgbtBlue) and (p1.rgbtBlue<=pHigh.rgbtBlue)) then
????????????? ? begin
??????? result:=false;
??????? exit;
????? end;
??? end;
? end;
end;
?
以下是模糊找圖, 調用模糊判斷
// 模糊找圖,在大圖里的(x1,y1)和(x2,y2)中找出小圖來?
// 其中nV是R,G,B的偏差值,0..255
// 當返回true時,以下變量存放找到的位置
//??? bmpFindX:integer;
//??? bmpFindY:integer;
function BmpFindEx(bmpBig,bmp:TBitmap;x1,y1,x2,y2:integer;nV:byte):Boolean;
var
? x,y:integer;
begin
? if x1+y1+x2+y2=0 then
? begin
??? x1:=0;
??? y1:=0;
??? x2:=bmpBig.Width-1;
??? y2:=bmpBig.Height-1;
? end;
? for y:=y1 to y2-1 do //行
? begin
??? if bmp.Height>y2-y then //高度不夠,失敗了
????? break;
??? for x:=x1 to x2-1 do //列
??? begin
????? if (bmp.Width>x2-x) then //寬度不夠,本行完成檢查了
??????? break;
????? begin
??????? if BmpCmpEx(bmpBig,bmp,x,y,nV) then
??????? begin
????????? result:=true;
????????? bmpFindX:=x;
????????? bmpFindY:=y;
????????? exit;
??????? end;
????? end; // end if
??? end; // end for x
? end; // end for y
? result:=false; // 到這里就是失敗
end;
總結
以上是生活随笔為你收集整理的delphi之模糊找图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Eclipse+PyDev+Django
- 下一篇: highchart 柱状图,列宽自适应(