简单阈值法
此方法是直截了当的。如果像素值大于阈值,则会被赋为一个值(可能为白色),否则会赋为另一个值(可能为黑色)。使用的函数是 cv.threshold。第一个参数是源图像,它应该是灰度图像。第二个参数是阈值,用于对像素值进行分类。第三个参数是 maxval,它表示像素值大于(有时小于)阈值时要给定的值。opencv 提供了不同类型的阈值,由函数的第四个参数决定。不同的类型有:
cv::ThresholdTypes {
cv::THRESH_BINARY = 0,
cv::THRESH_BINARY_INV = 1,
cv::THRESH_TRUNC = 2,
cv::THRESH_TOZERO = 3,
cv::THRESH_TOZERO_INV = 4,
cv::THRESH_MASK = 7,
cv::THRESH_OTSU = 8,
cv::THRESH_TRIANGLE = 16
}
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")
img := cv.imread("2.png")
img_grey := cv.cvtColor(img, CV_COLOR_BGR2GRAY:=6)
cv.threshold(img_grey, 100, 255, CV_THRESH_BINARY:=0, img)
cv.imshow("Image", img)
cv.waitKey()
cv.destroyAllWindows()
自适应阈值
在前一节中,我们使用一个全局变量作为阈值。但在图像在不同区域具有不同照明条件的条件下,这可能不是很好。在这种情况下,我们采用自适应阈值。在此,算法计算图像的一个小区域的阈值。因此,我们得到了同一图像不同区域的不同阈值,对于不同光照下的图像,得到了更好的结果。
cv.ADAPTIVE_THRESH_MEAN_C 阈值是指邻近地区的平均值。
cv.ADAPTIVE_THRESH_GAUSSIAN_C 阈值是权重为高斯窗的邻域值的加权和。
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")
img := cv.imread("2.png")
img_grey := cv.cvtColor(img, CV_COLOR_BGR2GRAY:=6)
th := cv.adaptiveThreshold(img_grey, 255, CV_ADAPTIVE_THRESH_MEAN_C:=0, CV_THRESH_BINARY:=0, 11, 2)
cv.imshow("Image", th)
cv.waitKey()
cv.destroyAllWindows()
Otsu二值化
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")
img := cv.imread("2.png")
img_grey := cv.cvtColor(img, CV_COLOR_BGR2GRAY:=6)
cv.threshold(img_grey, 100, 255, CV_THRESH_BINARY:=0 | CV_THRESH_OTSU:=8, img)
cv.imshow("Image", img)
cv.waitKey()
cv.destroyAllWindows()
天黑版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.cvtColor(img, img_gray := cv.MAT(), cv2.CV_COLOR_BGR2GRAY)
cv.threshold(img_gray, threshold1 := cv.MAT(), 100, 255, cv2.CV_THRESH_BINARY)
cv.imshow("threshold1", threshold1)
;自适应阈值
cv.adaptiveThreshold(img_gray, threshold2 := cv.MAT(), 255, cv2.CV_ADAPTIVE_THRESH_MEAN_C, cv2.CV_THRESH_BINARY, 11, 2)
cv.imshow("threshold2", threshold2)
;otsu二值化
cv.threshold(img_gray, threshold3 := cv.MAT(), 100, 255, cv2.CV_THRESH_BINARY | cv2.CV_THRESH_OTSU)
cv.imshow("threshold3", threshold3)
cv.waitKey()
cv.destroyAllWindows()
有错误请联系我改正!
本系列所有贡献者(AutoHotKey中文社区群友)不分先后:天黑请闭眼,zzZ…,演好自己,僵尸,城西,Tebayaki。
先点个赞
?