#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
hOpencv := DllCall("LoadLibrary", "str", "opencv_world455.dll", "ptr")
hOpencvCom := DllCall("LoadLibrary", "str", "autoit_opencv_com455.dll", "ptr")
DllCall("autoit_opencv_com455.dll\DllInstall", "int", 1, "wstr", A_IsAdmin = 0 ? "user" : "", "cdecl")
global cv := ComObjCreate("OpenCV.cv")
img := cv.imread("test.png") ;读取图片
global showImg := img.clone()
cv.namedWindow("Image")
proc := RegisterCallback("drawcircle", "F")
DllCall("opencv_world455.dll\cvSetMouseCallback", "Astr", "Image", "ptr", proc, "ptr", 0)
drawcircle(event, x, y, flag){
ToolTip, % event "`n" x " " y "`n" flag
If (event == 1){ ;鼠标左键按下
cv.circle(showImg, ComArrayMake([x,y]), 100, ComArrayMake([255,0,0]), 0) ;画圆
}
}
While true{
cv.imshow("Image", showImg)
}
ComArrayMake(inputArray) ;这个函数用来转换参数,比如函数原来需要(0,0),调用这个函数ComArrayMake([0,0])就可以转换过去
{
arr := ComObjArray(VT_VARIANT:=12, inputArray.Length())
Loop,% inputArray.Length()
{
arr[A_Index-1] := inputArray[A_Index]
}
return arr
}
上面用到的dll和图片蓝奏云链接
https://wwm.lanzouw.com/iHcJi0033fob天黑版opencv_ahk.dll使用(改变了调用方式,优化速度...)
相关文件:https://wwz.lanzouw.com/iAkK803eaaud
cv2.ahk和log.ahk来自社区群友zzZ...
可以用文件中的天黑版的v2h版ahk运行。
示例:鼠标作为画笔
#Dllload lib
#DllLoad opencv_ahk.dll
#include <cv2>
#include <log>
SetWorkingDir A_ScriptDir
;初始化opencv模块
cv := ObjFromPtr(DllCall('opencv_ahk.dll\opencv_init', 'ptr', DllCall(A_AhkPath '\ahkGetApi', 'ptr'), 'cdecl ptr'))
img := cv.imread("image/test.png")
cv.namedWindow("image")
cv.moveWindow("image", 100, 100)
cv.setMouseCallback('image', drawcircle)
proc := CallbackCreate(drawcircle)
drawcircle(event, x, y, flag){
ToolTip event "`n" x " " y "`n" flag
If (event == 1){ ;鼠标左键按下
cv.circle(img, [x, y], 100, [255,0,0], 0) ;画圆
}
}
While true{
cv.imshow("image", img)
}
cv.waitKey()
cv.destroyAllWindows()
有错误请联系我改正!
本系列所有贡献者(AutoHotKey中文社区群友)不分先后:天黑请闭眼,zzZ...,演好自己,僵尸,城西,Tebayaki。
标签:event,鼠标,AHK,dll,opencv,cv,ahk,ptr From: https://www.cnblogs.com/ff888/p/16783529.html