找回密码
 加入
搜索
查看: 5946|回复: 10

[图形处理] 求原理。他是如何实现【图像识别】技术的纯au3搜图.au3例1_2.7z

  [复制链接]
发表于 2012-7-9 21:47:43 | 显示全部楼层 |阅读模式
#include <GdiPlus.au3>
;#include <myOcrFunc.au3>

_GDIPlus_Startup ()

$s_dir = @ScriptDir
$hBitmap1 = _GDIPlus_BitmapCreateFromFile($s_dir & "\Source.png")
$array1 = _myReadBitmapMsg($hBitmap1, 1)

$hBitmap2 = _GDIPlus_BitmapCreateFromFile($s_dir & "\Search.png")
$array2 = _myReadBitmapMsg($hBitmap2, 1)

$iMax = _GDIPlus_ImageGetWidth ($hBitmap1) / _GDIPlus_ImageGetWidth ($hBitmap2)

$t=TimerInit()
$aPosMsg = _ArrayComp($array1, $array2, False, $iMax)
ConsoleWrite("找到一个图片块用去时间:"&TimerDiff($t)&'毫秒,位置信息(为空未找到):'&$aPosMsg&@CRLF)

$t=TimerInit()
$aPosMsg = _ArrayComp($array1, $array2, True, $iMax)
ConsoleWrite("找到所有图片块用去时间:"&TimerDiff($t)&'毫秒,位置信息(为空未找到):'&$aPosMsg&@CRLF)

If $aPosMsg<>"" Then 
        Local $aPos, $i, $hGraphics = _GDIPlus_ImageGetGraphicsContext ($hBitmap1)
        $aPosMsg = StringSplit($aPosMsg,"|",2)
        For $i = 0 To UBound($aPosMsg)-1
                $aPos = StringSplit($aPosMsg[$i],",",2)
                _GDIPlus_GraphicsDrawRect($hGraphics, $aPos[0], $aPos[1], $aPos[2], $aPos[3])
        Next
        _GDIPlus_ImageSaveToFile($hBitmap1, $s_dir&"\Target.bmp")
        _GDIPlus_GraphicsDispose ($hGraphics)
EndIf

_GDIPlus_ImageDispose ($hBitmap1)
_WinAPI_DeleteObject ($hBitmap1)
_GDIPlus_ImageDispose ($hBitmap2)
_WinAPI_DeleteObject ($hBitmap2)
_GDIPlus_ShutDown ()
Exit

;一般来说,当$hBitmap2是小图时$iType=1, 较大时$iType=0, 如果已知$array2哪个元素进行搜索比较合理,直接指定为$iY
Func _ArrayComp($array1, $array2, $SearchAll=False, $iMax=0, $iY=0, $iType=0)
        Local $iExtended=0
        If $iMax>0 And $iY=0 Then
                Local $t=TimerInit()
                Local $sBmpData1 = ""
                For $i = 0 To UBound($array1)-1;相当于_ArrayToString或_myReadBitmapMsg($hBitmap,0),但更快
                        $sBmpData1 &= $array1[$i]
                Next;===>当多个$array2搜索时,这个内容考虑在udf外执行,_ArrayComp($sBmpData1, $array1, $array2, $SearchAll=False, $iMax=0, $iY=0, $iType=0)
                For $i = 0 To UBound($array2)-1
                        If StringReplace($array2[$i], StringLeft($array2[$i],6),"")="" Then ContinueLoop
                        Select
                                Case $iType = 0
                                        $iY = $i
                                        ExitLoop
                                #cs
                                Case $iType = 1 And Not @AutoItX64 ;pusofalse提供的多线程方法
                                        ;该方法中映射文件到内容部分 $pBaseAddress 暂用本段代替
                                        Local $tBuff = DllStructCreate("char ["&StringLen($sBmpData1)+1&"]")
                                        DllStructSetData($tBuff, 1, $sBmpData1)
                                        $pBaseAddress = DllStructGetPtr($tBuff)
                                        ;http://www.autoitx.com/thread-20592-1-3.html
                                #ce
                                Case Else
                                        StringReplace($sBmpData1,$array2[$i],"",0, 1)
                                        If $iExtended=0 Or $iExtended>@extended Then 
                                                $iExtended=@extended
                                                If $iExtended=0 Then Return "";没有匹配的图块
                                                $iY = $i
                                                If $iExtended<=$iMax Then ExitLoop
                                        EndIf
                        EndSelect
                Next
                ConsoleWrite("获取最佳搜索串"&TimerDiff($t)&','&$iExtended&@CRLF)
        EndIf

        ;Local $t=TimerInit()
        If UBound($array1)<UBound($array2) Then Return ""
        Local $s_re="", $y, $y2, $iW2=StringLen($array2[$iY]), $iPos;, $iSearchPos
        For $y = $iy To UBound($array1)-1
                $iPos = 0;$iSearchPos = 1
                While $y+UBound($array2)<=UBound($array1)
                        $iPos = StringInStr($array1[$y], $array2[$iY], 1, 1, $iPos+1);$iPos = StringInStr($array1[$y], $array2[$iY], 1, 1, $iSearchPos)
                        Select
                        Case $iPos = 0
                                ContinueLoop(2)
                        Case Mod($iPos-1,6)<>0 ;Or $y<$iy
                                ;$iSearchPos = $iPos+1
                                ContinueLoop
                        EndSelect
                        For $y2 = $iY To UBound($array2)-1
                                If StringMid($array1[$y+$y2-$iy], $iPos, $iW2)<>$array2[$y2] Then
                                        ;$iSearchPos = $iPos+1
                                        ContinueLoop(2)
                                EndIf
                        Next
                        For $y2 = 0 To $iY-1
                                If StringMid($array1[$y+$y2-$iy], $iPos, $iW2)<>$array2[$y2] Then
                                        ;$iSearchPos = $iPos+1
                                        ContinueLoop(2)
                                EndIf
                        Next
                        $s_re &= ($iPos-1)/6&','&$y-$iy&','&$iW2/6&','&UBound($array2)&"|"
                        StringReplace($s_re,"|","")
                        If (Not $SearchAll) Or ($iExtended>0 And @extended>=$iExtended) Then ExitLoop(2)
                        ;$iSearchPos = $iPos+1
                WEnd
        Next
        If StringRight($s_re,1)="|" Then $s_re = StringTrimRight($s_re,1)
        ;ConsoleWrite(TimerDiff($t)&@CRLF);18
        Return $s_re
EndFunc

Func _myReadBitmapMsg($hBitmap, $iType=1);
        Local $aBmpData[5]
    $aBmpData[1] = _GDIPlus_ImageGetWidth ($hBitmap)
    $aBmpData[2] = _GDIPlus_ImageGetHeight ($hBitmap)
        Local $BitmapData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $aBmpData[1], $aBmpData[2], $GDIP_ILMREAD, $GDIP_PXF24RGB)
        $aBmpData[3] = Abs(DllStructGetData($BitmapData, "Stride"))
        $aBmpData[4] = DllStructGetData($BitmapData, "Scan0");
        _GDIPlus_BitmapUnlockBits($hBitmap, $BitmapData)
        Local $tBuff, $iH
        Switch $iType;已测试比StringRegExp快
                Case -1
                        Return $aBmpData
                Case 0
                        For $iH = 1 To $aBmpData[2]
                                $tBuff = DllStructCreate("byte[" & ($aBmpData[1]*3) & "]", $aBmpData[4] + ($iH-1)*$aBmpData[3])
                                $aBmpData[0] &= StringTrimLeft(DllStructGetData($tBuff, 1),2)
                        Next
                        Return $aBmpData
                Case Else
                        Local $aRet[$aBmpData[2]]
                        For $iH = 1 To $aBmpData[2]
                                $tBuff = DllStructCreate("byte[" & ($aBmpData[1]*3) & "]", $aBmpData[4] + ($iH-1)*$aBmpData[3])
                                $aRet[$iH-1] = StringTrimLeft(DllStructGetData($tBuff, 1), 2)
                        Next
                        Return $aRet
        EndSwitch
EndFunc

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 1威望 -5 贡献 -5 收起 理由
afan -5 -5

查看全部评分

发表于 2012-7-9 22:26:56 | 显示全部楼层
这个怎么搜图??????
 楼主| 发表于 2012-7-9 22:35:47 | 显示全部楼层
回复 2# fhqbbfcu2050
弟弟。。。你就木有看到上面的图片吗????????????????、、、、、、
发表于 2012-7-9 23:08:46 | 显示全部楼层
像放大镜那种的吗?
发表于 2012-7-10 00:04:39 | 显示全部楼层
myorc这个文件呢?
发表于 2012-7-10 08:10:13 | 显示全部楼层
漂亮的不错.
发表于 2012-7-10 09:49:53 | 显示全部楼层
我对这方面不熟悉。GDI啊。高级应用啊。。进来学习下思路。
发表于 2012-7-10 10:26:44 | 显示全部楼层
不行,看你这个弄了个通宵,还是没看懂....到是可以改图片了...我是不是太菜了点.
 楼主| 发表于 2012-7-14 21:23:23 | 显示全部楼层
你们难道就不能将上面的图片下载下来然后按代码名修改吗???
发表于 2021-12-7 07:33:57 | 显示全部楼层
漂亮的不错.
发表于 2021-12-7 17:39:17 | 显示全部楼层
学习,共同进步
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-3 21:09 , Processed in 0.089509 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表