opencv-ahk bindings

Post your working scripts, libraries and tools.
User avatar
thqby
Posts: 432
Joined: 16 Apr 2021, 11:18
Contact:

opencv-ahk bindings

16 Apr 2022, 05:38

The project is opencv-ahk bindings, use autoit-opencv project to generate opencv header file data, and then rebind it to ahk_v2.
This library dependents on opencv_world4.5.5 x64 and ahk_v2h.beta.3-fixed.5(exe or dll)
The parameter type definition is the same as that of C++ version, and some functions are not completely overloaded.
opencv documentation

Download on GitHub

examples

Code: Select all

#DllLoad opencv_ahk.dll
#DllLoad AutoHotkey64.dll

; cv := ObjFromPtr(DllCall('opencv_ahk.dll\opencv_init', 'ptr', DllCall(A_AhkPath '\ahkGetApi', 'cdecl ptr'), 'cdecl ptr'))	; ahk_h
cv := ObjFromPtr(DllCall('opencv_ahk.dll\opencv_init', 'ptr', DllCall('autohotkey64.dll\ahkGetApi', 'cdecl ptr'), 'cdecl ptr'))

cvcons := cv.constants
img := cv.imread(A_ScriptDir '\1.png')
cv.imshow('img', img)
cv.waitKey()
cv.cvtColor(img, img_grey := cv.mat(), cvcons.COLOR_BGR2GRAY)
cv.imshow('img', img_grey)
cv.waitKey()
ret := cv.threshold(img_grey, thresh := cv.mat(), 200, 255, cvcons.THRESH_BINARY)
cv.imshow('img', thresh)
cv.waitKey()
cv.findContours(thresh, contours := cv.Vector_Vector_Point(), hierarchy := cv.vector_vec4i(), cvcons.RETR_TREE, cvcons.CHAIN_APPROX_SIMPLE)
cv.drawContours(img, contours, -1, [0, 0, 255, 0], 2)
cv.imshow("img", img)
cv.waitKey()
cv.putText(img, "hello ahk⛅", [0,50], cv.constants.FONT_HERSHEY_SIMPLEX, 1, [0, 0, 255], 2, 8, 0)
cv.putText(img, "hello ahk⛅", [0,50], cv.constants.FONT_HERSHEY_SIMPLEX, 1, [0, 0, 255], 2, 8, 1)
td := cv.TextDraw(,48)
td.putText(img, 'hello ahk⛅', [0,150], [255], 1)
td.putText(img, 'hello ahk⛅', [0,150], [255], 0)
loop 40 {
	row := A_Index
	loop 40
		img[row + 270, A_Index + 20] := [Random(0, 255),Random(0, 255),Random(0, 255),]
}
cv.imshow('img', img)
cv.waitKey()
cv.destroyAllWindows()
parameter type conversion
  1. `InputArray, OutputArray, InputOutputArray`, these parameters can use `Mat` or `UMat` or `cuda_GpuMat` or `Vector` objects.
  • `vector<xx>`, use `Vector_xx`(if the class exists) or `Array` objects.
  • the classes and structs of opencv, use class instances of the corresponding type(if the class exists), or `Array`(if the class does not exist).
  • When overloaded functions have integer, float and string type parameters at the same time, the corresponding types shall be strictly used.
  • Parameters of the `CV_OUT` macro tag that the type is not `(Input)OutputArray`, use `VarRef`, and the initial value needs to be provided when there is a `CV_IN` macro tag.
image.png
image.png (32.72 KiB) Viewed 2792 times
Attachments
1.png
1.png (15.99 KiB) Viewed 2793 times
Last edited by thqby on 16 Nov 2022, 00:19, edited 1 time in total.
kazhafeizhale
Posts: 77
Joined: 25 Dec 2018, 10:58

Re: opencv-ahk bindings

17 Apr 2022, 03:12

example: matchtemplate
find image from screen, use dxgi and opencv
ImageSearch use 300ms vs dxgi&opencv 30ms

https://github.com/kazhafeizhale/opencv_ahk/tree/main/matchtemplate
Image

Code: Select all

#DllLoad opencv_ahk.dll
#include <wincapture> 
#include <cv2>
#include <log>
SetWorkingDir A_ScriptDir

cv := ObjFromPtr(DllCall('opencv_ahk.dll\opencv_init', 'ptr', DllCall(A_AhkPath '\ahkGetApi', 'ptr'), 'cdecl ptr'))
dxcp := wincapture.DXGI() 

rtn := find_image_from_screen(A_ScriptDir '\template.png', 0, 0, A_ScreenWidth, A_ScreenHeight)

cv.rectangle(rtn["src"], [rtn["x"], rtn["y"], rtn["template"]["w"], rtn["template"]["h"]], [0, 255, 0], 2, 8, 0)
cv.imshow('src', rtn["src"])
MsgBox(rtn["x"] "  " rtn["y"] " sim:" rtn["sim"])
/**
* 屏幕指定区域找图
* @param template_path 目标图像路径
* @param thresh 图像灰度二值化 0-255
* @param x ROI x
* @param y ROI y
* @param w ROI w
* @param h ROI h
* @param screen_agin 重新截图
* true 重新截图, false 使用上次图像
* @return  {map}
* Map("src", l_screen_shot_roi,"x", x, "y", y, "sim", sim, "template", Map("w", template.cols ,"h", template.rows))
*/
find_image_from_screen(template_path, x, y, w, h, thresh := 100, screen_again := true)
{
	static l_screen_shot := cv.Mat()
	l_new_image := false
	if(screen_again)
		screen_shot()
	l_screen_shot.copyTo(l_screen_shot_roi := cv.Mat())
	l_screen_shot_roi := l_screen_shot_roi([x, y, w, h])
	template := cv.imread(template_path)
	cv.cvtColor(l_screen_shot_roi, gray_src := cv.Mat(), cv2.CV_COLOR_BGR2GRAY)
	cv.cvtColor(template, gray_template := cv.Mat(), cv2.CV_COLOR_BGR2GRAY)
	cv.threshold(gray_src, gray_src, thresh, 255, cv2.CV_THRESH_BINARY)
	cv.threshold(gray_template, gray_template, thresh, 255, cv2.CV_THRESH_BINARY)

	method := cv2.CV_TM_CCORR_NORMED
	;method := cv2.CV_TM_SQDIFF_NORMED
	;method := cv2.CV_TM_CCOEFF_NORMED
	cv.matchTemplate(gray_src, gray_template, res := cv.Mat(), method)
	cv.minMaxLoc(res, &minVal, &maxVal, &minLoc, &maxLoc)

	if(method == cv2.CV_TM_SQDIFF || method == cv2.CV_TM_SQDIFF_NORMED)
		matchLoc := minLoc, sim := minVal
	else
		matchLoc := maxLoc, sim := maxVal

	x := matchLoc[1]
	y := matchLoc[2]
	rtn := Map("src", l_screen_shot_roi,"x", x, "y", y, "sim", sim, "template", Map("w", template.cols ,"h", template.rows))
	log.info(rtn)
	return rtn

	screen_shot()
	{
		cb := CallbackCreate(revice)
		dxcp.capture(cb)
		while(l_new_image == false){
		}
		l_new_image := false
	}
	revice(pdata, pitch, sw, sh, tick) 
	{
		if tick && pdata 
		{
			bb := BitmapBuffer(pdata, pitch, sw, sh)
			bb := bb.cvtBytes(3)
			wid := bb.width ,hei := bb.height, pBits:= bb.Ptr, Channels := 3 ;通道
			mat := cv.Mat(hei, wid, cv.constants.CV_8UC3, pBits, (wid * Channels + 3) & -4)
		}
		mat.copyTo(l_screen_shot)
		l_new_image := true
	}
}
Last edited by kazhafeizhale on 18 Apr 2022, 04:38, edited 1 time in total.
kazhafeizhale
Posts: 77
Joined: 25 Dec 2018, 10:58

Re: opencv-ahk bindings

17 Apr 2022, 03:21

example : image process
Image
Image
Image
https://github.com/kazhafeizhale/opencv_ahk/tree/main/graphAngle

Code: Select all

#include "cv2.ah2"
#DllLoad opencv_ahk.dll
cv := ObjFromPtr(DllCall('opencv_ahk.dll\opencv_init', 'ptr', DllCall(A_AhkPath '\ahkGetApi', 'ptr'), 'cdecl ptr'))
MsgBox("key down in image")

img := cv.imread(A_ScriptDir '\1.png')
copy := img.clone()
cv.imshow('img', img)
cv.waitkey()
cv.cvtColor(img, img_grey := cv.Mat(), cv.constants.COLOR_BGR2GRAY)
cv.imshow('img', img_grey)
cv.waitkey()
cv.Canny(img_grey, canny := cv.Mat(), 80.0, 160.0)
cv.imshow('img', canny)

k := cv.getStructuringElement(cv2.CV_MORPH_RECT, [3, 3], [-1, -1])
cv.dilate(canny, canny, k)
cv.imshow('img', canny)

cv.waitkey()
cv.findContours(canny, contours := cv.Vector_Vector_Point(), cv2.CV_RETR_EXTERNAL, cv2.CV_CHAIN_APPROX_SIMPLE, [])

approx_contours := cv.Vector_Vector_Point()
array_of_contours := contours.toArray()
loop(contours.Size)
{
    approx_contour := cv.Vector_Point()
    cv.approxPolyDP(cv.Vector_Point(array_of_contours[A_Index]), approx_contour, 30, true)
    approx_contours.push(approx_contour)
    cv.drawContours(copy, approx_contours, A_Index - 1, [0, 255, 0], 3, 1, 16)
    cv.imshow('img', copy)
    cv.waitkey()
    array_of_approx_contour := approx_contour.toArray()
    min_index := 1
    loop(array_of_approx_contour.Length)
    {
        next := A_Index == array_of_approx_contour.Length ? 1 : A_Index + 1
        length := getDistance(array_of_approx_contour[A_Index], array_of_approx_contour[next])
        if (A_Index == 1)
            min_length := length
        else if (length < min_length)
        {
            min_index := A_Index
            min_length := length
        }
    }

    next := (min_index == array_of_approx_contour.Length) ? 1 : min_index + 1
    fixed_point_index := Mod(min_index + 2, 5) + 1
    center := [(array_of_approx_contour[min_index][1] + array_of_approx_contour[next][1]) / 2, (array_of_approx_contour[min_index][2] + array_of_approx_contour[next][2]) / 2]
    cv.line(copy, center, array_of_approx_contour[fixed_point_index], [0, 255, 0])
    angel := get_point_angle(center, array_of_approx_contour[fixed_point_index])
    cv.putText(copy, String(Integer(angel)), center, cv2.CV_FONT_HERSHEY_SIMPLEX, 0.45, [0, 0, 255], 2)
    cv.imshow('img', copy)
    cv.waitkey()
}

cv.imshow('img', copy)

;opencv  求两点间距
getDistance(pointO, pointA)
{
    distance := (pointO[1] - pointA[1])**2 + (pointO[2] - pointA[2])**2
    distance := Sqrt(distance)
    return distance
}

/************************************************************************
*函数名:        get_point_angle
*
*函数作用:      已知2个坐标点,求从 0------->x 逆时针需旋转多少角度到该位置
*
*                   |
*                   |
*                   |
*                   |
*------------------------------------> x
*                   | 0
*                   |
*                   |
*                   |
*                   v
*                   y
*
*函数参数:
*CvPoint2D32f pointO  - 起点
*CvPoint2D32f pointA  - 终点
*
*函数返回值:
*double         向量OA,从 0------->x 逆时针需旋转多少角度到该位置
**************************************************************************/
get_point_angle(pointO, pointA)
{
    point := [(pointA[1] - pointO[1]), (pointA[2] - pointO[2])]

    if ((0 == point[1]) && (0 == point[2]))
    {
        return 0
    }

    if (0 == point[1])
    {
        angle := 90
        return angle
    }

    if (0 == point[2])
    {
        angle := 0
        return angle
    }

    temp := Abs(float(point[2]) / float(point[1]))
    temp := ATan(temp)
    temp := temp * 180 / cv2.CV_PI

    if ((0 < point[1]) && (0 < point[2]))
    {
        angle := 360 - temp
        return angle
    }

    if ((0 > point[1]) && (0 < point[2]))
    {
        angle := 360 - (180 - temp)
        return angle
    }

    if ((0 < point[1]) && (0 > point[2]))
    {
        angle := temp
        return angle
    }

    if ((0 > point[1]) && (0 > point[2]))
    {
        angle := 180 - temp
        return angle
    }
    return -1
}
dkzeanah
Posts: 6
Joined: 24 Apr 2022, 03:23

Re: opencv-ahk bindings

29 May 2022, 06:33

What is the part about autoit v3? is that needed? can this be used for webcam input/ tracking actual video?
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: opencv-ahk bindings

29 May 2022, 14:43

can this be used for webcam input/ tracking actual video?
It depends on what do You want to track.
By the way smbape has created udf for using dlib in autoit
http://dlib.net/
https://www.autoitscript.com/forum/topic/207773-dlib-udf/
kazhafeizhale wrote:
17 Apr 2022, 03:12
ImageSearch use 300ms vs dxgi&opencv 30ms
For me (1920x1080 win10) imagesearch gets sceenshot from desktop and searches in it template.png about 30 msec
david8u8
Posts: 6
Joined: 03 Nov 2022, 10:43

Re: opencv-ahk bindings

12 Nov 2022, 09:36

Great work, very good, nice,than you very much.

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: Descolada and 21 guests