图像加法
通过 OpenCV 函数,cv.add()将两个图像相加,两个图像应该具有相同的深度和类型,或者第二个图像可以是像素值,比如(255,255,255),白色值。
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")
img1 := cv.imread("2.png")
img2 := cv.imread("11.png")
add_img := cv.Add(img2, img1)
cv.imshow("Image1", add_img)
cv.waitKey()
cv.destroyAllWindows()
原图对比相加后图片运行结果:
图像混合
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")
img1 := cv.imread("2.png")
img2 := cv.imread("11.png")
new_img := cv.addWeighted(img1, 0.8, img2, 0.2, 0)
cv.imshow("Image", new_img)
cv.waitKey()
cv.destroyAllWindows()
按位操作
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")
img1 := cv.imread("test.png")
img2 := cv.imread("test.png")
cv.rectangle(img1,ComArrayMake([384, 50]),ComArrayMake([510, 178]),ComArrayMake([0, 100, 100]),-1)
cv.imshow("Image_1", img1)
cv.rectangle(img2,ComArrayMake([454, 130]),ComArrayMake([580, 258]),ComArrayMake([0, 0, 255]),-1)
cv.imshow("Image_2", img2)
or_img := cv.bitwise_or(img1, img2)
xor_img := cv.bitwise_xor(img1, img2)
and_img := cv.bitwise_and(img1, img2)
cv.imshow("Image_or", or_img) ;或
cv.imshow("Image_xor", xor_img) ;异或
cv.imshow("Image_and", and_img) ;与
not_img := cv.imread("2.png")
not_img := cv.bitwise_not(not_img)
cv.imshow("Image_not", not_img) ;非
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使用(改变了调用方式,优化速度…)
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'))
; cv.namedWindow("image")
; cv.moveWindow("image", 100, 100)
;图像加法
img := cv.imread("image/lena.png")
img1 := cv.Mat(img.rows, img.cols, cv2.CV_8UC3, [127, 127, 127])
; cv.imshow("image", img)
; cv.imshow("image1", img1)
add_img := cv.Mat()
cv.add(img, img1, add_img)
cv.imshow("image_add", add_img)
;图像混合
img2 := cv.imread("image/cv_logo.png")
new_img := cv.Mat()
cv.addWeighted(img, 0.8, img2, 0.2, 0, new_img)
cv.imshow("new_image", new_img)
;按位操作
img3 := cv.Mat(580, 860, cv2.CV_8UC3, [0, 0, 0])
img4 := cv.Mat(580, 860, cv2.CV_8UC3, [0, 0, 0])
cv.rectangle(img3, [384, 50], [510, 178], [0, 100, 100], -1)
cv.rectangle(img4, [454, 130], [580, 258], [0, 0, 255], -1)
or_img := cv.Mat()
cv.bitwise_or(img3, img4, or_img)
xor_img := cv.Mat()
cv.bitwise_xor(img3, img4, xor_img)
and_img := cv.Mat()
cv.bitwise_and(img3, img4, and_img)
cv.imshow("Image_or", or_img) ;或
cv.imshow("Image_xor", xor_img) ;异或
cv.imshow("Image_and", and_img) ;与
not_img_new := cv.Mat()
cv.bitwise_not(img, not_img_new)
cv.imshow("Image_not", not_img_new) ;非
有错误请联系我改正!
本系列所有贡献者(AutoHotKey中文社区群友)不分先后:天黑请闭眼,zzZ…,演好自己,僵尸,城西,Tebayaki。