OpenCV COM

Post your working scripts, libraries and tools for AHK v1.1 and older
kazhafeizhale
Posts: 77
Joined: 25 Dec 2018, 10:58

Re: OpenCV COM

27 Mar 2022, 05:36

malcev wrote:
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.
OK, thanks
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: OpenCV COM

27 Mar 2022, 06:58

May be You need smth like this?

Code: Select all

cv := ComObjCreate("OpenCV.cv")
big := cv.imread("big.jpg")
small := cv.imread("small.jpg")
roi := ComObjArray(VT_VARIANT:=12, 4)
roi[0] := 0
roi[1] := 0
roi[2] := small.width
roi[3] := small.height
small.copyTo(ComObjCreate("OpenCV.cv.Mat").create(big, roi))
cv.imshow("copy", big)
cv.waitKey()
cv.destroyAllWindows()
kazhafeizhale
Posts: 77
Joined: 25 Dec 2018, 10:58

Re: OpenCV COM

27 Mar 2022, 23:03

malcev wrote:
27 Mar 2022, 06:58
May be You need smth like this?

Code: Select all

cv := ComObjCreate("OpenCV.cv")
big := cv.imread("big.jpg")
small := cv.imread("small.jpg")
roi := ComObjArray(VT_VARIANT:=12, 4)
roi[0] := 0
roi[1] := 0
roi[2] := small.width
roi[3] := small.height
small.copyTo(ComObjCreate("OpenCV.cv.Mat").create(big, roi))
cv.imshow("copy", big)
cv.waitKey()
cv.destroyAllWindows()
This Method is valid
V2 code:

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)
roi.copyTo(ComObject("OpenCV.cv.Mat").create(big, arr))
cv.imshow("Image", big)

cv.waitKey()
cv.destroyAllWindows()
BabyMamaDrama
Posts: 5
Joined: 30 Apr 2022, 20:09

Re: OpenCV COM

30 Apr 2022, 20:20

Hi, this forum thread was a great read to get myself into opencv with AHK. Thank you!
I was wondering if there are any examples / info available on how to convert a GDI bitmap to an OpenCV format?
I would assume pixels would need to be transferred to an OpenCV mat image with the same dimensions?

I am currently saving the bitmap first to a file and then reading the new file with OpenCV, which is not very convenient or efficient at all.
An example is given below:

Code: Select all

bmp := Gdip_BitmapFromScreen(0 "|" 0 "|" 250 "|" 250)
Gdip_SaveBitmapToFile(bmp, "screenshots/test.png")
img := cv.imread("screenshots/test.png")

; do something with the OpenCV image...

Gdip_DisposeImage(bmp)
Any help would be appreciated!
BabyMamaDrama
Posts: 5
Joined: 30 Apr 2022, 20:09

Re: OpenCV COM

01 May 2022, 11:46

malcev wrote:
30 Apr 2022, 21:24
viewtopic.php?p=437009#p437009
Ty, this got me a little bit closer to the final result.
I managed to construct the image, but any attempt of closing the window showing it, completely locks up my system. (I have to CTRL-ALT-DEL to close the AHK process to restore it)

Code shown below:

Code: Select all

bmp := Gdip_BitmapFromScreen(0 "|" 0 "|" 250 "|" 250)

;copy bmp to mat image
Gdip_GetImageDimensions(bmp, width, height)
E1 := Gdip_LockBits(bmp, 0, 0, width, height, Stride, Scan0, BitmapData)
mat := mat.create(height, width, 24, Scan0, Abs(Stride)) ; I assume this is where it goes wrong...
mat_clone := mat.clone()
Gdip_UnlockBits(bmp, E1)
cv.imshow("new", mat_clone)
cv.waitKey()
cv.destroyAllWindows()

Gdip_DisposeImage(bmp)
Any tips on how to fix this?
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: OpenCV COM

01 May 2022, 13:05

Use code from my link.
BabyMamaDrama
Posts: 5
Joined: 30 Apr 2022, 20:09

Re: OpenCV COM

01 May 2022, 18:24

malcev wrote:
01 May 2022, 13:05
Use code from my link.

Alright so both examples (given by you and the one I posted) were working. Thank you!
It was the AutoHotInterception library that is unable to interact with the window that is created from cv.imshow.
It completely locks up the system when you attempt to interact with the window with LButton through AutoHotInterception's commands.

two working examples on how to convert a Gdip bitmap to an OpenCV mat image:

Code: Select all

x := 0
y := 0
w := 250
h := 250

pBits := 0x00000000
chdc  := CreateCompatibleDC()
hbm   := CreateDIBSection(w, h, chdc, 24, pBits)
obm   := SelectObject(chdc, hbm)
hhdc  := GetDC()

BitBlt(chdc, 0, 0, w, h, hhdc, x, y, RASTER_SRCCOPY:=0x00CC0020)
val := (w * 3 + 3) & -4
img := mat.create(h, w, 16, pBits, val)
img := cv.flip(img, 0)

SelectObject(chdc, obm)
ReleaseDC(hhdc)    
DeleteObject(hbm)
DeleteDC(hhdc)
DeleteDC(chdc)

cv.imshow("new", img)
cv.waitKey()
cv.destroyAllWindows()
cv.releaseMat(img)

Code: Select all

x := 0
y := 0
w := 250
h := 250

bmp := Gdip_BitmapFromScreen(x "|" y "|" w "|" h)
E1  := Gdip_LockBits(bmp, x, y, w, h, Stride, Scan0, BitmapData)
img := mat.create(h, w, 24, Scan0, Stride)
Gdip_UnlockBits(bmp, E1)
Gdip_DisposeImage(bmp)

cv.imshow("new", img)
cv.waitKey()
cv.destroyAllWindows()
cv.releaseMat(img)
dbgba
Posts: 20
Joined: 02 Apr 2021, 22:11

Re: OpenCV COM

23 Aug 2022, 23:22

Thank you for the tutorial on this topic, and share the functions of bitmap and mat conversion. Let opencv do more.

Code: Select all

#include Gdip_all.ahk

hOpenCV := DllCall("LoadLibrary", "Str", "opencv_world455.dll", "Ptr")
hOpenCVCom := DllCall("LoadLibrary", "Str", "autoit_opencv_com455.dll", "Ptr")

Try cv := ComObjCreate("OpenCV.cv")
  catch
    DllCall("autoit_opencv_com455.dll\DllInstall", "int", 1, "Wstr", A_IsAdmin=0 ? "user" : "", "cdecl")
    , cv := ComObjCreate("OpenCV.cv")

pToken := Gdip_StartUp()  ; GDI library load


pBitmap := Gdip_CreateBitmapFromFile("1.png")

; MsgBox % Clipboard := Gdip_BitmapToBase64(pBitmap, "JPG", 70)  ; pBitmap to Base64
; pBitmap := Gdip_BitmapFromBase64(Base64)  ; Base64 to pBitmap

; pBitmap := Gdip_CreateBitmapFromHBITMAP(hBitmap)  ; hBitmap to pBitmap
; hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)  ; pBitmap to hBitmap


img_Mat := BitmapToMat(pBitmap)

cv.imshow("OpenCV.img_Mat", img_Mat)

MsgBox After clicking "OK", the demo Mat converts the hBitmap for Gui to display


img := cv.imread("1.png")

hBitmap := MatToBitmap(img)

Gui, Add, Picture, , % "HBITMAP:*" hBitmap
Gui, Show, AutoSize, AHK window display
Return

GuiClose:
    ExitApp


BitmapToMat(pBitmap) {
    Static hbm2
    Try Func_Mat := ComObjCreate("OpenCV.cv.Mat")
      catch
        Throw Exception("This function requires autoit_opencv_com to be preloaded in order to use")

    DeleteObject(hbm2)

    , hdc := CreateCompatibleDC()
    , Gdip_GetImageDimensions(pBitmap, W, H)  ; Returns the width and height of the pBitmap
    , hbm := Gdip_CreateHBITMAPFromBitmap(pBitmap)
    , obm := SelectObject(hdc, hbm)

    , hdc2 := CreateCompatibleDC()
    , hbm2 := CreateDIBSection(W, -H, hdc2, 24, pBits)
    , obm := SelectObject(hdc2, hbm2)
    , BitBlt(hdc2, 0, 0, W, H, hdc, 0, 0, 0xCC0020)

    , DeleteObject(hbm)
    , DeleteDC(hdc)
    , DeleteDC(hdc2)

    Return Func_Mat.create( H, W, 16, pBits, (W * 3 + 3) & -4 )  ; Channels := 3
}


MatToBitmap(CVmat) {
    Static hbm
    Try cv := ComObjCreate("OpenCV.cv")
      catch
        Throw Exception("This function requires autoit_opencv_com to be preloaded in order to use")

    if (CVmat.channels == 1)
        CVmat := cv.cvtcolor(CVmat, 9)  ; COLOR_GRAY2BGRA := 9
    else if (CVmat.channels == 3)
        CVmat := cv.cvtcolor(CVmat, 0)  ; COLOR_BGR2BGRA := 0
    ; https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.imaging.pixelformat?view=netframework-4.6
    pBitmap := Gdip_CreateBitmap(CVmat.Cols, CVmat.Rows, 2498570, CVmat.Step1, CVmat.data)
    , DeleteObject(hbm)
    , hbm := Gdip_CreateHBITMAPFromBitmap(pBitmap)
    Return hbm
}
Share a gesture recognition project of OpenCV + Mediapipe
viewtopic.php?f=28&t=107278
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: OpenCV COM

22 Feb 2023, 08:20

Author updated his library and now We dont need to register com object.
Updated 1 post.
KimSLi
Posts: 1
Joined: 21 Mar 2023, 01:52

Re: OpenCV COM

21 Mar 2023, 01:57

hello malcev, can you give me a little hint on how to find image using opencv on autohotkey ?
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: OpenCV COM

21 Mar 2023, 02:54

viewtopic.php?p=437009#p437009
Only it was written before I changed syntax.
We dont need install com library with this function

Code: Select all

DllCall("autoit_opencv_com454.dll\DllInstall", "int", 1, "wstr",  "", "cdecl")
Now We need to create com objects like in 1 post in this topic.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: emandreck and 252 guests