OpenCV COM

Post your working scripts, libraries and tools for AHK v1.1 and older
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

OpenCV COM

Post by malcev » 06 Nov 2021, 16:31

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
}
Last edited by malcev on 26 Feb 2023, 02:32, edited 7 times in total.

kairushinjuu
Posts: 46
Joined: 07 May 2020, 07:02

Re: OpenCV COM

Post by kairushinjuu » 06 Nov 2021, 20:19

Been waiting for something like this <3

iseahound
Posts: 1446
Joined: 13 Aug 2016, 21:04
Contact:

Re: OpenCV COM

Post by iseahound » 06 Nov 2021, 22:22

👍 Nice. Will play around with it later.

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

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: OpenCV COM

Post by malcev » 06 Nov 2021, 23:31

It is already compiled.

leosouza85
Posts: 90
Joined: 22 Jul 2016, 16:28

Re: OpenCV COM

Post by leosouza85 » 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?

SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: OpenCV COM

Post by SOTE » 09 Nov 2021, 05:36

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).

peameedo112
Posts: 6
Joined: 12 Feb 2020, 19:14

Re: OpenCV COM

Post by peameedo112 » 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?

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: OpenCV COM

Post by malcev » 09 Nov 2021, 09:10

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")

peameedo112
Posts: 6
Joined: 12 Feb 2020, 19:14

Re: OpenCV COM

Post by peameedo112 » 10 Nov 2021, 04:53

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
	}
}

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: OpenCV COM

Post by malcev » 10 Nov 2021, 07:01

For me my code with run as admin works, like it should.

leosouza85
Posts: 90
Joined: 22 Jul 2016, 16:28

Re: OpenCV COM

Post by leosouza85 » 10 Nov 2021, 21:53

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

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: OpenCV COM

Post by malcev » 13 Nov 2021, 15:03

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.

ogib
Posts: 2
Joined: 22 Sep 2018, 02:23

Re: OpenCV COM

Post by ogib » 18 Nov 2021, 00:55

any examples of matchTemplate please

nadure
Posts: 22
Joined: 26 Mar 2021, 23:02
Location: Korea
Contact:

Re: OpenCV COM

Post by nadure » 18 Nov 2021, 07:25

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()
*/

kerk
Posts: 5
Joined: 19 Nov 2021, 03:56

Re: OpenCV COM

Post by kerk » 29 Dec 2021, 20:04

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. :)

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: OpenCV COM

Post by malcev » 01 Jan 2022, 07:19

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.

iseahound
Posts: 1446
Joined: 13 Aug 2016, 21:04
Contact:

Re: OpenCV COM

Post by iseahound » 01 Jan 2022, 22:42

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...

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: OpenCV COM

Post by malcev » 02 Jan 2022, 03:42

Author changed dll.
I fixed 1 post.

kerk
Posts: 5
Joined: 19 Nov 2021, 03:56

Re: OpenCV COM

Post by kerk » 02 Jan 2022, 19:18

@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.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: OpenCV COM

Post by malcev » 03 Jan 2022, 02:27

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.

Post Reply

Return to “Scripts and Functions (v1)”