AHK调用opencv(十三)图像的几何变换 – ahk_v2_beta3

AHK调用opencv(十三)图像的几何变换 – ahk_v2_beta3

SetWorkingDir A_ScriptDir

hOpencv := DllCall("LoadLibrary", "str", "opencv_world455.dll", "ptr")
hOpencvCom := DllCall("LoadLibrary", "str", "autoit_opencv_com455.dll", "ptr")
;DllCall("autoit_opencv_com455.dll\DllInstall", "int", 1, "wstr", A_IsAdmin = 0 ? "user" : "", "cdecl")

cv := ComObject("OpenCV.cv")
t_mat := ComObject("OpenCV.cv.MAT")
point2f0 := ComObject("OpenCV.VectorOfPoint2f")
point2f1 := ComObject("OpenCV.VectorOfPoint2f")
point2f2 := ComObject("OpenCV.VectorOfPoint2f")
point2f3 := ComObject("OpenCV.VectorOfPoint2f")

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

;缩放
;img_resize := cv.resize(img, ComArrayMake([150, 150]))
img_resize := cv.resize(img, ComArrayMake([0, 0]), 0.5, 0.3, 2) ;如果第二个参数都为0,则按比例缩放,最后一个参数cv::INTER_CUBIC = 2

;平移变换
t_mat := t_mat.zeros(2, 3, 5)
t_mat.float_set_at(0, 0, 1)
t_mat.float_set_at(0, 2, 20)
t_mat.float_set_at(1, 1, 1)
t_mat.float_set_at(1, 2,10)
img_warpAffine := cv.warpAffine(img, t_mat, img.size())

;旋转
rows := img.rows()
cols := img.cols()
m0 := cv.getRotationMatrix2D(ComArrayMake([(cols-1)/2,(rows-1)/2]),90,1)
img_revolve := cv.warpAffine(img, m0, img.size())

;仿射变换
point2f0.push_back(ComArrayMake([0, 0]))
point2f0.push_back(ComArrayMake([cols-1, 0]))
point2f0.push_back(ComArrayMake([0, rows-1]))

point2f1.push_back(ComArrayMake([10, 100]))
point2f1.push_back(ComArrayMake([200, 50]))
point2f1.push_back(ComArrayMake([100, 200]))
m1 := cv.getAffineTransform(point2f0, point2f1)
img_getAffine := cv.warpAffine(img, m1, img.size())

;透视变换
point2f2.push_back(ComArrayMake([40, 50]))
point2f2.push_back(ComArrayMake([100, 190]))
point2f2.push_back(ComArrayMake([200, 50]))
point2f2.push_back(ComArrayMake([300, 190]))

point2f3.push_back(ComArrayMake([50, 60]))
point2f3.push_back(ComArrayMake([120, 200]))
point2f3.push_back(ComArrayMake([200, 50]))
point2f3.push_back(ComArrayMake([300, 190]))
m2 := cv.getPerspectiveTransform(point2f2, point2f3)
img_getPerspective := cv.warpPerspective(img, m2, img.size())


cv.imshow("Image", img)
cv.imshow("Image_resize", img_resize)
cv.imshow("Image_warpAffine", img_warpAffine)
cv.imshow("Img_revolve", img_revolve)
cv.imshow("img_getAffine", img_getAffine)
cv.imshow("img_getPerspective", img_getPerspective)

cv.waitKey()
cv.destroyAllWindows()

ComArrayMake(inputArray)
{
	arr := ComObjArray(VT_VARIANT:=12, inputArray.Length)
	Loop inputArray.Length
	{
		arr[A_Index-1] := inputArray[A_Index]
	}
	return arr
}

天黑版opencv_ahk.dll使用(改变了调用方式,优化速度…)

相关文件:https://wwz.lanzouw.com/iAkK803eaaud

cv2.ahk和log.ahk来自社区群友zzZ…

可以用文件中的天黑版的v2h版ahk运行。

示例:图像的几何变换

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

;初始化opencv模块
cv := ObjFromPtr(DllCall('opencv_ahk.dll\opencv_init', 'ptr', DllCall(A_AhkPath '\ahkGetApi', 'ptr'), 'cdecl ptr'))
img := cv.imread("image/lena.png")
cv.imshow("image", img)

point2f0 := cv.Vector_point()

;缩放
cv.resize(img, img_resize := cv.MAT(), [0, 0], 0.5, 0.3, 2)
cv.imshow("img_resize", img_resize)

;平移变换
t_mat := cv.MAT().zeros(2, 3, cv2.CV_32FC1)
t_mat[0, 0] := 1
t_mat[0, 2] := 20
t_mat[1, 1] := 1
t_mat[1, 2] := 10
cols := img.cols
rows := img.rows
img_size := [cols, rows]
cv.warpAffine(img, img_warpAffine := cv.MAT(), t_mat, img_size)
cv.imshow("img_warpAffine", img_warpAffine)

;旋转
m0 := cv.getRotationMatrix2D([(img.cols-1)/2,(img.rows-1)/2], 90, 1)
cv.warpAffine(img, img_revolve := cv.MAT(), m0, img_size)
cv.imshow("img_revolve", img_revolve)

;仿射变换
point2f0 := cv.Vector_Point2f([[0, 0], [cols-1, 0], [0, rows-1]])
point2f1 := cv.Vector_Point2f([[10, 100], [200, 50], [100, 200]])
m1 := cv.getAffineTransform(point2f0, point2f1)
cv.warpAffine(img, img_getAffine := cv.MAT(), m1, img_size)
cv.imshow("img_getAffine", img_getAffine)

;透视变换
AffinePointsSrc := cv.Vector_Point2f([[40, 50], [100, 190], [200, 50], [300, 190]])
AffinePointsDst := cv.Vector_Point2f([[50, 60], [120, 200], [200, 50], [300, 190]])
TransImage := cv.getPerspectiveTransform(AffinePointsSrc, AffinePointsDst)
cv.warpPerspective(img, img_getPerspective := cv.MAT(), TransImage, img_size)
cv.imshow("img_getPerspective", img_getPerspective)


cv.waitKey()
cv.destroyAllWindows()

有错误请联系我改正!

本系列所有贡献者(AutoHotKey中文社区群友)不分先后:天黑请闭眼,zzZ…,演好自己,僵尸,城西,Tebayaki。

给TA捐赠
共{{data.count}}人
人已捐赠
其他

百度语音合成

2022-3-26 19:35:30

其他案例

AHK_L版调用H版dll实现多线程函数

2022-3-30 11:09:16

3 条回复 A文章作者 M管理员
  1. AHK中文社区
    1河许人给您打赏了¥5
  2. AHK中文社区

    坚持一个很棒的方向

  3. dbgba
    dbgba给您打赏了¥5
个人中心
购物车
优惠劵
有新私信 私信列表
搜索