zoukankan      html  css  js  c++  java
  • 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;

  • 相关阅读:
    Android客户端消息推送原理简介
    优秀程序员的十个习惯
    能让你成为更优秀程序员的10个C语言资源
    成大事必备9种能力、9种手段、9种心态
    33个优秀的HTML5应用演示 (转)
    Maven学习:Eclipse使用maven构建web项目(转)
    使用fiddler模拟http get
    TLS握手
    风暴英雄 http 302重定向 正在等待游戏模式下载完成
    page template in kentico
  • 原文地址:https://www.cnblogs.com/MaxWoods/p/3106288.html
Copyright © 2011-2022 走看看