OK, thanks
OpenCV COM
-
- Posts: 77
- Joined: 25 Dec 2018, 10:58
Re: OpenCV COM
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()
-
- Posts: 77
- Joined: 25 Dec 2018, 10:58
Re: OpenCV COM
This Method is validmalcev wrote: ↑27 Mar 2022, 06:58May 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()
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()
-
- Posts: 5
- Joined: 30 Apr 2022, 20:09
Re: OpenCV COM
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:
Any help would be appreciated!
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)
-
- Posts: 5
- Joined: 30 Apr 2022, 20:09
Re: OpenCV COM
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)
Re: OpenCV COM
Use code from my link.
-
- Posts: 5
- Joined: 30 Apr 2022, 20:09
Re: OpenCV COM
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)
Re: OpenCV COM
Thank you for the tutorial on this topic, and share the functions of bitmap and mat conversion. Let opencv do more.
Share a gesture recognition project of OpenCV + Mediapipe
viewtopic.php?f=28&t=107278
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
}
viewtopic.php?f=28&t=107278
Re: OpenCV COM
Author updated his library and now We dont need to register com object.
Updated 1 post.
Updated 1 post.
Re: OpenCV COM
hello malcev, can you give me a little hint on how to find image using opencv on autohotkey ?
Re: OpenCV COM
viewtopic.php?p=437009#p437009
Only it was written before I changed syntax.
We dont need install com library with this function
Now We need to create com objects like in 1 post in this topic.
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")
Return to “Scripts and Functions (v1)”
Who is online
Users browsing this forum: emandreck and 252 guests