OpenCV COM

Post your working scripts, libraries and tools for AHK v1.1 and older
kerk
Posts: 5
Joined: 19 Nov 2021, 03:56

Re: OpenCV COM

Post by kerk » 03 Jan 2022, 20:37

malcev wrote:
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.

This is my test code.

Thanks.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#include gdip_all.ahk
#include Gdip_ImageSearch.ahk

;openCV test
F3::
{

    ; register com
    hOpencv := DllCall("LoadLibrary", "str", "c:\work\opencv454\build\x64\vc14\bin\opencv_world454.dll", "ptr")
    hOpencvCom := DllCall("LoadLibrary", "str", "C:\WORK\AHKs\autoit_opencv_com454.dll", "ptr")
    DllCall("autoit_opencv_com454.dll\DllInstall", "int", 1, "wstr",  "", "cdecl")

    ;cmd runas admin
    ; regsvr32 /n /i autoit_opencv_com454.dll

    cv := ComObjCreate("OpenCV.cv")
    mat := ComObjCreate("OpenCV.cv.Mat")
    
  
   
    ; gdi
    pToken := Gdip_StartUp()
    hwndAHKs:=WinExist("AHKs")
    
    WinGetPos ,,,wid, hei



    
    pBits:=0x00000000
    chdc := CreateCompatibleDC(GetDC(hwndAHKs))
    hbm := CreateDIBSection(wid, hei,chdc,32,pBits)
    obm := SelectObject(chdc, hbm)
    hhdc := GetDC(hwndAHKs)    
	
    ;NOTSRCCOPY 
    ;BitBlt(chdc, 0, 0, wid, hei, hhdc, 0, 0, 0x330008) 
    
    ;SRCCOPY 
    BitBlt(chdc, 0, 0, wid, hei, hhdc, 0, 0, 0xCC0020)
    
    

    ;hbm HBITMAP for BitBlt result check
    ;pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
    ;Gdip_SaveBitmapToFile(pBitmap,"test2.png")
    

    val:= (wid * 4 + 3) & -4

    resultMat := mat.create( hei, wid, 24, pBits, val ) 
  
	resultMat := cv.flip(resultMat, 0)


    ; it works (haystack)
    cv.imshow("img", resultMat)

    ; it works (imwrite test)
    cv.imwrite("filename.jpg", resultMat)

    ; it works (needle)
    img_tmpl := cv.imread(".\s.png")

    ; it works (for MatchTemplate test)
    ;resultMat2 := cv.imread(".\filename.jpg")

    
         
   
    
    ;cv.TM_SQDIFF_NORMED  
    method:=3

    ;An error occured   0xE06D7363 , normal "imread"ed image works, but ".create"ed memDC image doesn't . I dunno why..
    ;res := cv.matchTemplate(resultMat2,img_tmpl,method)
    res := cv.matchTemplate(resultMat,img_tmpl,method)
    
            
        
    cv.minMaxLoc(res)
  
    exts:=cv.extended

    x:=exts[3][0]
    y:=exts[3][1]
    
    msgbox, match %x% %y%
    



    SelectObject(chdc, obm)

    ReleaseDC(hhdc)    
    DeleteObject(hbm)
    DeleteDC(hhdc)
    DeleteDC(chdc)


    Gdip_Shutdown(pToken)

    cv.destroyAllWindows()
    
    DllCall("autoit_opencv_com454.dll\DllInstall", "int", 0, "wstr", "", "cdecl")
    DllCall("FreeLibrary", "ptr", hOpencv)
    DllCall("FreeLibrary", "ptr", hOpencvCom)  

    return
}
;openCV test



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

Re: OpenCV COM

Post by kerk » 03 Jan 2022, 20:39

This is my test code.

Thank you!

Code: Select all

 #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#include gdip_all.ahk
#include Gdip_ImageSearch.ahk

;openCV test
F3::
{

    ; register com
    hOpencv := DllCall("LoadLibrary", "str", "c:\work\opencv454\build\x64\vc14\bin\opencv_world454.dll", "ptr")
    hOpencvCom := DllCall("LoadLibrary", "str", "C:\WORK\AHKs\autoit_opencv_com454.dll", "ptr")
    DllCall("autoit_opencv_com454.dll\DllInstall", "int", 1, "wstr",  "", "cdecl")

    ;cmd runas admin
    ; regsvr32 /n /i autoit_opencv_com454.dll

    cv := ComObjCreate("OpenCV.cv")
    mat := ComObjCreate("OpenCV.cv.Mat")
    
  
   
    ; gdi
    pToken := Gdip_StartUp()
    hwndAHKs:=WinExist("AHKs")
    
    WinGetPos ,,,wid, hei



    
    pBits:=0x00000000
    chdc := CreateCompatibleDC(GetDC(hwndAHKs))
    hbm := CreateDIBSection(wid, hei,chdc,32,pBits)
    obm := SelectObject(chdc, hbm)
    hhdc := GetDC(hwndAHKs)    
	
    ;NOTSRCCOPY 
    ;BitBlt(chdc, 0, 0, wid, hei, hhdc, 0, 0, 0x330008) 
    
    ;SRCCOPY 
    BitBlt(chdc, 0, 0, wid, hei, hhdc, 0, 0, 0xCC0020)
    
    

    ;hbm HBITMAP for BitBlt result check
    ;pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
    ;Gdip_SaveBitmapToFile(pBitmap,"test2.png")
    

    val:= (wid * 4 + 3) & -4

    resultMat := mat.create( hei, wid, 24, pBits, val ) 
  
	resultMat := cv.flip(resultMat, 0)


    ; it works (haystack)
    cv.imshow("img", resultMat)

    ; it works (imwrite test)
    cv.imwrite("filename.jpg", resultMat)

    ; it works (needle)
    img_tmpl := cv.imread(".\s.png")

    ; it works (for MatchTemplate test)
    ;resultMat2 := cv.imread(".\filename.jpg")

    
         
   
    
    ;cv.TM_SQDIFF_NORMED  
    method:=3

    ;An error occured   0xE06D7363 , normal "imread"ed image works, but ".create"ed memDC image doesn't . I dunno why..
    ;res := cv.matchTemplate(resultMat2,img_tmpl,method)
    res := cv.matchTemplate(resultMat,img_tmpl,method)
    
            
        
    cv.minMaxLoc(res)
  
    exts:=cv.extended

    x:=exts[3][0]
    y:=exts[3][1]
    
    msgbox, match %x% %y%
    



    SelectObject(chdc, obm)

    ReleaseDC(hhdc)    
    DeleteObject(hbm)
    DeleteDC(hhdc)
    DeleteDC(chdc)


    Gdip_Shutdown(pToken)

    cv.destroyAllWindows()
    
    DllCall("autoit_opencv_com454.dll\DllInstall", "int", 0, "wstr", "", "cdecl")
    DllCall("FreeLibrary", "ptr", hOpencv)
    DllCall("FreeLibrary", "ptr", hOpencvCom)  

    return
}
;openCV test


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

Re: OpenCV COM

Post by malcev » 03 Jan 2022, 21:00

Try create CreateDIBSection with 24bpp.

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

Re: OpenCV COM

Post by kerk » 03 Jan 2022, 21:30

@malcev

Oh Thanks man . I owe you one

Here is my test code and it works.

Now we can try more CV things with this .

Thanks @malcev

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#include gdip_all.ahk
#include Gdip_ImageSearch.ahk

F4::
RELOAD
return

;openCV test
F3::
{

    ; register com
    hOpencv := DllCall("LoadLibrary", "str", "c:\work\opencv454\build\x64\vc14\bin\opencv_world454.dll", "ptr")
    hOpencvCom := DllCall("LoadLibrary", "str", "C:\WORK\AHKs\autoit_opencv_com454.dll", "ptr")
    DllCall("autoit_opencv_com454.dll\DllInstall", "int", 1, "wstr",  "", "cdecl")

    ;cmd runas admin
    ; regsvr32 /n /i autoit_opencv_com454.dll

    cv := ComObjCreate("OpenCV.cv")
    mat := ComObjCreate("OpenCV.cv.Mat")
    
  
   
    ; gdi
    pToken := Gdip_StartUp()
    
    ; hwnd of a window
    hwndAHKs:=WinExist("AHKs")
    
    ; get width and height
    WinGetPos ,,,wid, hei


    
    ; create memDC for screenshot
    pBits:=0x00000000
    chdc := CreateCompatibleDC(GetDC(hwndAHKs))
    hbm := CreateDIBSection(wid, hei,chdc,24,pBits)
    obm := SelectObject(chdc, hbm)
    hhdc := GetDC(hwndAHKs)    

    BitBlt(chdc, 0, 0, wid, hei, hhdc, 0, 0, 0xCC0020)
    

    
    val:= (wid * 3 + 3) & -4

    img_haystack := mat.create( hei, wid, 16, pBits, val ) 
  
	img_haystack := cv.flip(img_haystack, 0)

    

    ; show haystack image
    cv.imshow("haystack", img_haystack)

    ; write test
    cv.imwrite("filename.jpg", img_haystack)

    ; needle image
    img_tmpl := cv.imread(".\s.png")


   
    
    ;cv.TM_SQDIFF_NORMED  try other methods
    method:=3

    res := cv.matchTemplate(img_haystack,img_tmpl,method)
    
            
        
    cv.minMaxLoc(res)
  
    exts:=cv.extended

    x:=exts[3][0]
    y:=exts[3][1]
    
    ;result max match x,y
    msgbox, match %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("autoit_opencv_com454.dll\DllInstall", "int", 0, "wstr", "", "cdecl")
    DllCall("FreeLibrary", "ptr", hOpencv)
    DllCall("FreeLibrary", "ptr", hOpencvCom)  

    return
}
;openCV test




Chappier
Posts: 44
Joined: 21 Aug 2021, 21:58

Re: OpenCV COM

Post by Chappier » 26 Jan 2022, 21:39

@malcev do you know if is possible to resize a GDI+ pbitmap or hbitmap?
Tried to resize a image this way:

Code: Select all

cv := ComObjCreate("OpenCV.cv")
img := cv.imread("C:\test.png")

arr := [900, 900]
cv.resize(img, arr, 0.5, 0.5, 1)

cv.imshow("Image", img)
cv.waitKey()
cv.destroyAllWindows()

; unregister com
DllCall("autoit_opencv_com454.dll\DllInstall", "int", 0, "wstr", A_IsAdmin = 0 ? "user" : "", "cdecl")
DllCall("FreeLibrary", "ptr", hOpencv)
DllCall("FreeLibrary", "ptr", hOpencvCom)
The image is loaded into the GUI, however throw an error in the resize line.

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

Re: OpenCV COM

Post by malcev » 27 Jan 2022, 01:52

1) img i is not pbitmap nor hbitmap. It is cv::Mat object.
2) You need to send not just array, but comobjarray.
As return value You will get resized cvMat object.

Chappier
Posts: 44
Joined: 21 Aug 2021, 21:58

Re: OpenCV COM

Post by Chappier » 27 Jan 2022, 08:59

Could you provide an example resizing a image and showing it in the gui?
Would like to compare the size of the image in memory before and after resizing.
Is opencv any good in compressing images?
I know it’s not pbitmap or hbitmap I was asking if is possible to convert one format to another, like cvmat to hbitmap.

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

Re: OpenCV COM

Post by malcev » 27 Jan 2022, 09:18

Could you provide an example resizing a image and showing it in the gui?
Try by Yourself.
See examples in first post.

kazhafeizhale
Posts: 77
Joined: 25 Dec 2018, 10:58

Re: OpenCV COM

Post by kazhafeizhale » 14 Feb 2022, 09:48

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

Code: Select all

; register com
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\DllInstall", "int", 1, "wstr", A_IsAdmin = 0 ? "user" : "", "cdecl")

cv := ComObjCreate("OpenCV.cv")
img := cv.imread("D:\Unnamed.bmp")
cv.imshow("Image", img)
cv.waitKey()
cv.destroyAllWindows()

; unregister com
DllCall("autoit_opencv_com454.dll\DllInstall", "int", 0, "wstr", A_IsAdmin = 0 ? "user" : "", "cdecl")
DllCall("FreeLibrary", "ptr", hOpencv)
DllCall("FreeLibrary", "ptr", hOpencvCom)
Drawing contours:

Code: Select all

cv := ComObjCreate("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()
Also We can permanently register Com (does not call DllCall("autoit_opencv_com454.dll\DllInstall", "int", 0, "wstr", A_IsAdmin = 0 ? "user" : "", "cdecl"))
And after that run scripts faster like this

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

cv := ComObjCreate("OpenCV.cv")
img := cv.imread("D:\Unnamed.bmp")
cv.imshow("Image", img)
cv.waitKey()
cv.destroyAllWindows()
Hi, How i know contours values have what? traverse

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

Re: OpenCV COM

Post by malcev » 14 Feb 2022, 10:15

Something like this:

Code: Select all

contours := cv.findContours(thresh, CV_RETR_TREE := 3, CV_CHAIN_APPROX_SIMPLE := 2)
loop % contours.maxindex() + 1
{
   data := contours[A_Index-1].data
   loop % contours[A_Index-1].dims*contours[A_Index-1].rows
      a .= numget(data+0, (A_Index-1)*4, "int") "-"
   a := SubStr(a, 1, -1) "`n"
}
msgbox % a

kazhafeizhale
Posts: 77
Joined: 25 Dec 2018, 10:58

Re: OpenCV COM

Post by kazhafeizhale » 16 Feb 2022, 05:02


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

Re: OpenCV COM

Post by malcev » 16 Feb 2022, 05:13

Dont know. Read python documentation.

kazhafeizhale
Posts: 77
Joined: 25 Dec 2018, 10:58

Re: OpenCV COM

Post by kazhafeizhale » 23 Mar 2022, 23:10

Code: Select all

Mat src = imread("test.png");
src(Rect(0, 0, 0, 0));
    /** @overload
    @param m Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied
    by these constructors. Instead, the header pointing to m data or its sub-array is constructed and
    associated with it. The reference counter, if any, is incremented. So, when you modify the matrix
    formed using such a constructor, you also modify the corresponding elements of m . If you want to
    have an independent copy of the sub-array, use Mat::clone() .
    @param roi Region of interest.
    */
    Mat(const Mat& m, const Rect& roi);
this constructor how to realize?

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

Re: OpenCV COM

Post by malcev » 24 Mar 2022, 19:11

For what exactly do You need this?
May be smth like this.

Code: Select all

cv := ComObjCreate("OpenCV.cv")
src := cv.imread("test.png")
roi := ComObjArray(VT_VARIANT:=12, 4)
roi[0] := 100
roi[1] := 200
roi[2] := 300
roi[3] := 400
ComObjCreate("OpenCV.cv.Mat").create(src, roi)

kazhafeizhale
Posts: 77
Joined: 25 Dec 2018, 10:58

Re: OpenCV COM

Post by kazhafeizhale » 24 Mar 2022, 21:08

malcev wrote:
24 Mar 2022, 19:11
For what exactly do You need this?
May be smth like this.

Code: Select all

cv := ComObjCreate("OpenCV.cv")
src := cv.imread("test.png")
roi := ComObjArray(VT_VARIANT:=12, 4)
roi[0] := 100
roi[1] := 200
roi[2] := 300
roi[3] := 400
ComObjCreate("OpenCV.cv.Mat").create(src, roi)
And the original image does not occupy the same block of memory
The constructor I mentioned above changes the new image and synchronizes the original image

Code: Select all

cv := ComObject("OpenCV.cv")
mt := ComObject("OpenCV.cv.Mat")
big := cv.imread("11.png")
small := cv.imread("2.png")

arr := ComObjArray(VT_VARIANT:=12, 4)
arr[0] := 0
arr[1] := 0
arr[2] := small.cols()
arr[3] := small.rows()
roi := mt.create(big, arr)

roi := cv.addWeighted(small, 0.5, roi, 0.5, 0.0)
cv.imshow("Image", big)

cv.waitKey()
cv.destroyAllWindows()
Last edited by kazhafeizhale on 27 Mar 2022, 04:42, edited 1 time in total.

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

Re: OpenCV COM

Post by malcev » 25 Mar 2022, 05:10

OK. You can see here how to create mat objects and choose one that You need
https://github.com/smbape/node-autoit-opencv-com/blob/main/src/declarations/Mat.js

kazhafeizhale
Posts: 77
Joined: 25 Dec 2018, 10:58

Re: OpenCV COM

Post by kazhafeizhale » 27 Mar 2022, 01:08

Hi malcev
I want use this methoud, but i don't know how to use, can you give a example? thanks!

Code: Select all

["cv.Mat.Mat", "", [], [
        ["Mat", "src", "", []],
        ["Rect", "roi", "", []]
    ], "", ""],

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

Re: OpenCV COM

Post by malcev » 27 Mar 2022, 02:04


kazhafeizhale
Posts: 77
Joined: 25 Dec 2018, 10:58

Re: OpenCV COM

Post by kazhafeizhale » 27 Mar 2022, 04:44

malcev wrote:
27 Mar 2022, 02:04
I already gave it.
viewtopic.php?p=453114#p453114
not create func, The large image was not modified at the end

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

Re: OpenCV COM

Post by malcev » 27 Mar 2022, 05:01

I do not understand what do You want.
It is better to ask Your question to author on autoit forum.

Post Reply

Return to “Scripts and Functions (v1)”