dbgba优化整理OpenCV系列示例打包下载20240513:
Bitmap转MAT - 截图搜图示例.ahk
; https://www.autohotkey.com/boards/viewtopic.php?p=437009#p437009 #NoEnv SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory. #SingleInstance Force #include <Gdip_all> ; 加载并初始化OpenCV hOpenCV := DllCall("LoadLibrary", "Str", "opencv_world490.dll", "Ptr") autoit_opencv_com_path := A_ScriptDir "\autoit_opencv_com490.dll" hOpenCVCom := DllCall("LoadLibrary", "Str", autoit_opencv_com_path, "Ptr") ComObjCreate := Func("_ComObjCreate").Bind(autoit_opencv_com_path) cv := ComObjCreate.Call("OpenCV.cv") mat := ComObjCreate("OpenCV.cv.Mat") ; gdi pToken := Gdip_StartUp() ; hwnd of a window【请确保窗口已经打开,不然接下来会报错】 hwndAHKs:=WinExist("AutoHotkey 中文帮助") ; get width and height 获取窗口宽度和高度 WinGetPos , , ,wid, hei ; create memDC for screenshot 为屏幕截图创建 memDC pBits:=0x00000000 hhdc := GetDC(hwndAHKs) chdc := CreateCompatibleDC(hhdc) ; 【调色板函数?】参数hhdc可删除 ; pBits 指向变量的指针,该变量接收指向 DIB 位值位置的指针 hbm := CreateDIBSection(wid, hei, chdc, 24, pBits) ; 返回hBitmap obm := SelectObject(chdc, hbm) BitBlt(chdc, 0, 0, wid, hei, hhdc, 0, 0, 0xCC0020) /* pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm) ; VarSetCapacity(hBitmap, 0) DllCall("Gdiplus.dll\GdipCreateHBITMAPFromBitmap", "UInt", pBitmap, A_PtrSize = 8 ? "UPtr*" : "UInt*", hBitmap, "Int", 0XFFFFFFFF) ; hBitmap := ImagePutHBitmap("filename.png") Gui, +LastFound -Resize Gui, Add, Picture, , % "HBITMAP:*" hBitmap Gui, Show, AutoSize, Example */ val:= (wid * 3 + 3) & -4 ; Channels := 3 ; 通道 ; MsgBox % hei "," wid "," 16 "," pBits "," val img_haystack := mat.create( hei, wid, 16, pBits, val ) /* static cv::Mat cv::Mat::create( int rows, int cols, int type, void* data, size_t step = cv::Mat::AUTO_STEP ); */ img_haystack := cv.flip(img_haystack, 0) ; 显示 haystack 图像 cv.imshow("haystack", img_haystack) ; 写入测试 ; cv.imwrite("filename.png", img_haystack) ; 要找的图像【需要一个图片大小判断,找的图大于被找的图会报错】 img_tmpl := cv.imread(".\soutu.png") ; 不能是中文名 ;cv.TM_SQDIFF_NORMED try other methods ; cv.TM_SQDIFF_NORMED 尝试其他方法 method:=3 ; MsgBox % img_haystack "," img_tmpl "," method ; 找图功能 res := cv.matchTemplate(img_haystack, img_tmpl, method) cv.minMaxLoc(res) exts:=cv.extended x:=exts[3][0] y:=exts[3][1] ;结果最大匹配 x,y msgbox, 找图匹配坐标: %x% %y% ;release things . I think there is little memory leak. 放东西。我认为几乎没有内存泄漏。 SelectObject(chdc, obm) ReleaseDC(hhdc) DeleteObject(hbm) DeleteDC(hhdc) DeleteDC(chdc) Gdip_Shutdown(pToken) cv.destroyAllWindows() DllCall("FreeLibrary", "ptr", hOpencv) DllCall("FreeLibrary", "ptr", hOpencvCom) return _ComObjCreate(opencvPath, ComObject) { DllCall(opencvPath "\DllActivateManifest") ComObject := ComObjCreate(ComObject) DllCall(opencvPath "\DllDeactivateActCtx") Return ComObject }
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。
评论(0)