Page 1 of 3

OpenCV COM

Posted: 06 Nov 2021, 16:31
by malcev
https://www.autoitscript.com/forum/topic/206432-opencv-v4-udf
On autohotkey use something like this:

Code: Select all

opencv_world_path := A_ScriptDir "\opencv-4.7.0-windows\opencv\build\x64\vc16\bin\opencv_world470.dll"
opencv_ffmpeg_path := A_ScriptDir "\opencv-4.7.0-windows\opencv\build\x64\vc16\bin\opencv_videoio_ffmpeg470_64.dll"
autoit_opencv_com_path := A_ScriptDir "\autoit-opencv-com\autoit_opencv_com470.dll"
hOpencv := DllCall("LoadLibrary", "str", opencv_world_path, "ptr")
hOpencvFfmpeg := DllCall("LoadLibrary", "str", opencv_ffmpeg_path, "ptr")
hOpencvCom := DllCall("LoadLibrary", "str", autoit_opencv_com_path, "ptr")
ComObjCreate := Func("_ComObjCreate").Bind(autoit_opencv_com_path)

; comobject need to create with call.
cv := ComObjCreate.Call("OpenCV.cv")
img := cv.imread("D:\Unnamed.bmp")
cv.imshow("Image", img)
cv.waitKey()
cv.destroyAllWindows()
return


_ComObjCreate(opencvPath, comobject)
{
   DllCall(opencvPath "\DllActivateManifest")
   comobject := ComObjCreate(comobject)
   DllCall(opencvPath "\DllDeactivateActCtx")
   return comobject
}
Drawing contours:

Code: Select all

cv := ComObjCreate.Call("OpenCV.cv")
img := cv.imread("D:\Unnamed.bmp")
img_grey := cv.cvtColor(img, CV_COLOR_BGR2GRAY := 6)
cv.threshold(img_grey, 100, 255, CV_THRESH_BINARY := 0)
thresh := cv.extended.1
contours := cv.findContours(thresh, CV_RETR_TREE := 3, CV_CHAIN_APPROX_SIMPLE := 2)
arr := ComObjArray(VT_VARIANT:=12, 4)
arr[0] := 0
arr[1] := 0
arr[2] := 255
arr[3] := 0
cv.drawContours(img, contours, -1, arr, 2)
cv.imshow("Image", img)
cv.waitKey()
cv.destroyAllWindows()
return


_ComObjCreate(opencvPath, comobject)
{
   DllCall(opencvPath "\DllActivateManifest")
   comobject := ComObjCreate(comobject)
   DllCall(opencvPath "\DllDeactivateActCtx")
   return comobject
}

Re: OpenCV COM

Posted: 06 Nov 2021, 20:19
by kairushinjuu
Been waiting for something like this <3

Re: OpenCV COM

Posted: 06 Nov 2021, 22:22
by iseahound
👍 Nice. Will play around with it later.

I wish someone would compile it and rename AutoIt to AutoHotkey though

Re: OpenCV COM

Posted: 06 Nov 2021, 23:31
by malcev
It is already compiled.

Re: OpenCV COM

Posted: 08 Nov 2021, 22:55
by leosouza85
Thank you @malcev so this is already useable with the code that you posted and the files on the link, using autoit name?

Re: OpenCV COM

Posted: 09 Nov 2021, 05:36
by SOTE
iseahound wrote:
06 Nov 2021, 22:22
👍 Nice. Will play around with it later.
Agree. Great post by malcev.
I wish someone would compile it and rename AutoIt to AutoHotkey though
Maybe you are somewhat joking (as you are a very experienced user), but for those that might not realize it, they can take or use the GitHub source as a reference and then compile their own .dll file and name it to whatever they like. Or, just use the .dll file from GitHub as is. Luckily AutoIt is similar enough to AutoHotkey, that dlls it would use, might be the easier option in many cases because of similar examples.

There are also a lot of bindings and wrappers for OpenCV in many other programming languages. So the person has various options to choose from. But, I did notice that when OpenCV switched over from also providing a C API to almost exclusively C++ for newer features, that there seems to be less up to date options in other non-mainstream languages. I guess it might depend how important the latest features are to a person. There is also OpenVX out there, which appears to be a competitor, but have to study up more about it.
leosouza85 wrote:
08 Nov 2021, 22:55
Thank you @malcev so this is already useable with the code that you posted and the files on the link, using autoit name?
The autoit-opencv-4.5.4-com-v1.0.0-rc.0.7z zip file contains the autoit_opencv_com454.dll. The .dll file was not made with AutoIt, but with C++ (probably Visual Studio).

Re: OpenCV COM

Posted: 09 Nov 2021, 08:37
by peameedo112
Hello, if script is running as admin, it will throw error 0x800401F3 invalid class string --> cv:=ComobjCreate("OpenCV.cv").
Any help?

Re: OpenCV COM

Posted: 09 Nov 2021, 09:10
by malcev
1) This library works on ahk x64 only.
2) You need to check what return this dllcalls (may be You place dlls on different path)

Code: Select all

hOpencv := DllCall("LoadLibrary", "str", "opencv-4.5.4-vc14_vc15\opencv\build\x64\vc15\bin\opencv_world454.dll", "ptr")
hOpencvCom := DllCall("LoadLibrary", "str", "autoit-opencv-com\autoit_opencv_com454.dll", "ptr")
DllCall("autoit_opencv_com454.dll\DllAtlSetPerUserRegistration", "int", 1, "cdecl")
DllCall("autoit_opencv_com454.dll\DllRegisterServer", "cdecl")

Re: OpenCV COM

Posted: 10 Nov 2021, 04:53
by peameedo112
yes, script works fine if it not run as admin.

Code here:

Code: Select all


RunAsAdmin() 

hOpencv := DllCall("LoadLibrary", "str", "opencv_world454.dll", "ptr")
hOpencvCom := DllCall("LoadLibrary", "str", "autoit_opencv_com454.dll", "ptr")
DllCall("autoit_opencv_com454.dll\DllAtlSetPerUserRegistration", "int", 1, "cdecl")
DllCall("autoit_opencv_com454.dll\DllRegisterServer", "cdecl")

cv := ComObjCreate("OpenCV.cv")
img := cv.imread("123.png")
cv.imshow("Image", img)
cv.waitKey()
cv.destroyAllWindows()

RunAsAdmin() {
	loop, %0%  ; For each parameter:
	{
		param := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
		params .= A_Space . param
	}
	ShellExecute := A_IsUnicode ? "shell32\ShellExecute":"shell32\ShellExecuteA"
	if not A_IsAdmin {
		if A_IsCompiled
		   DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_ScriptFullPath, str, params , str, A_WorkingDir, int, 1)
		else
		   DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_AhkPath, str, """" . A_ScriptFullPath . """" . A_Space . params, str, A_WorkingDir, int, 1)
		exitApp
	}
}

Re: OpenCV COM

Posted: 10 Nov 2021, 07:01
by malcev
For me my code with run as admin works, like it should.

Re: OpenCV COM

Posted: 10 Nov 2021, 21:53
by leosouza85
peameedo112 wrote:
09 Nov 2021, 08:37
Hello, if script is running as admin, it will throw error 0x800401F3 invalid class string --> cv:=ComobjCreate("OpenCV.cv").
Any help?
Im having this error too

Re: OpenCV COM

Posted: 13 Nov 2021, 15:03
by malcev
Author of COM explained me how to run script from regular user as administrator.
https://www.autoitscript.com/forum/topic/206432-opencv-v4-udf/page/6/?tab=comments#comment-1492294
I updated 1 post.

Re: OpenCV COM

Posted: 18 Nov 2021, 00:55
by ogib
any examples of matchTemplate please

Re: OpenCV COM

Posted: 18 Nov 2021, 07:25
by nadure
This is the code for using open cv matchTemplate example.

it works
:)

Code: Select all

hOpencv := DllCall("LoadLibrary", "str", "opencv-4.5.4-vc14_vc15\opencv\build\x64\vc15\bin\opencv_world454.dll", "ptr")
hOpencvCom := DllCall("LoadLibrary", "str", "autoit-opencv-com\autoit_opencv_com454.dll", "ptr")
DllCall("autoit_opencv_com454.dll\DllAtlSetPerUserRegistration", "int", 1, "cdecl")
DllCall("autoit_opencv_com454.dll\DllRegisterServer", "cdecl")

cv := ComObjCreate("OpenCV.cv")

src := cv.imread("hats.png", 0) ; 0 > GrayScale
templit := cv.imread("hat.png", 0) ; 0 > graScale
dst := cv.imread("hats.png")

result := cv.matchTemplate(src, templit, 1)

cv.minMaxLoc(result)
t := cv.extended

; Msgbox,% t[0] . "`n" . t[1] . "`n" . t[2][0] . "`n" . t[3]
minVal := t[0], minVal := t[1]
minLoc := t[2], maxLoc := t[3]
; minVal, maxVal, minLoc, maxLoc

x := minLoc[0], y := minLoc[1]

w := templit.width, h := templit.height
; Msgbox,% w . "`n" . h

; Show GrayScale Img
; cv.imshow("Image", templit)
; cv.waitKey()

cv.destroyAllWindows()

; dst := cv.rectangle(dst, (x, y), (x +  w, y + h) , (0, 0, 255), 1)
dst := cv.rectangle( dst
	, ComArrayMake([x, y])
	, ComArrayMake([x +  w, y + h]) 
	, ComArrayMake([0, 0, 255])
	, 1)


dst := cv.resize(dst, ComArrayMake([800, 600]), 1)
cv.imshow("dst", dst)
cv.waitKey(0)
cv.destroyAllWindows()

DllCall("autoit_opencv_com454.dll\DllUnregisterServer", "cdecl")
DllCall("FreeLibrary", "ptr", hOpencv)
DllCall("FreeLibrary", "ptr", hOpencvCom)

return

ComArrayMake(inputArray)
{
	arr := ComObjArray(VT_VARIANT:=12, inputArray.Length())
	Loop,% inputArray.Length()
	{
		arr[A_Index-1] := inputArray[A_Index]
	}
	return arr
}


/*

src = cv2.imread("hats.png", cv2.IMREAD_GRAYSCALE)
templit = cv2.imread("hat.png", cv2.IMREAD_GRAYSCALE)
dst = cv2.imread("hats.png")

result = cv2.matchTemplate(src, templit, cv2.TM_SQDIFF_NORMED)

minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result)
x, y = minLoc
h, w = templit.shape

dst = cv2.rectangle(dst, (x, y), (x +  w, y + h) , (0, 0, 255), 1)
cv2.imshow("dst", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
*/

Re: OpenCV COM

Posted: 29 Dec 2021, 20:04
by kerk
This library updated.

Has anyone had any success with create Mat from HBITMAP?

autoit udf fucntion ( _OpenCV_GetDesktopScreenMat ) was updated.

I am trying to convert that function to our ahk's , but didn't work.

All I want to do is just grab a picture and imgsearch with cv.matchtemplate function.

I think many of you hope too. :)

Re: OpenCV COM

Posted: 01 Jan 2022, 07:19
by malcev

Code: Select all

cv := ComObjCreate("OpenCV.cv")
img := ComObjCreate("OpenCV.cv.Mat").create(height, width, (channels-1)*8, pBits, (width*channels+3)&-4)
img := cv.flip(img, 0)   ; if needed to flip
pBits - pointer to image bits, channels - number of color channels.

Re: OpenCV COM

Posted: 01 Jan 2022, 22:42
by iseahound
This doesn't work for me, I get valid hModules from LoadLibrary, then the next call to autoit_opencv_com454.dll\DllAtlSetPerUserRegistration fails.

regsvr32 /n /i:user autoit_opencv_com454.dll from the install.bat file also fails for me, not sure why...

Re: OpenCV COM

Posted: 02 Jan 2022, 03:42
by malcev
Author changed dll.
I fixed 1 post.

Re: OpenCV COM

Posted: 02 Jan 2022, 19:18
by kerk
@malcev

I could create a Mat image from HBITMAP .

and flip was required because of windows Y axis is negative values.

but I couldn't imwrite or matchtemplate the result Mat object.

The only thing I succeeded is imshow with the result Mat.

malcev plz little help. thanks.

Re: OpenCV COM

Posted: 03 Jan 2022, 02:27
by malcev
For me imwrite works:

Code: Select all

cv.imwrite("filename.jpg", img)
How to use matchTemplate nadure already showed in this topic.
Post Your code.