我可以在 OpenCV 中为 Otsu 阈值添加偏差吗?
Can I add a bias to Otsu thresholding in OpenCV?
这是我的例子。从左到右:
- 原图
- 灰度 + (3,3) 高斯模糊
- Otsu 阈值处理 + 反转像素
我想捕捉更多笔触的微弱部分。我知道 Otsu Thresholding 尝试在像素强度直方图的两个峰值之间应用阈值点,但我想稍微调整一下,以便捕获一些较亮的像素。
开箱即用吗?或者我需要手动操作吗?
我有橡皮鸭现象的礼貌回答。
th, th_img = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
返回元组的第0个索引(th
)是Otsu二值化算法选择的阈值。我可以丢弃 th_img
并在 th
中应用任何我喜欢的偏差,然后再将其用于常规二进制阈值处理。
desired_th = th*1.2
_, th_img = cv2.threshold(blur, desired_th, 255, cv2.THRESH_BINARY)
这是我得到的。通过清理可能出现在外面的不需要的斑点,我会得到我想要的东西。
在 C++ 中,我经常 "tune out" 将 (otsu) 阈值函数返回的阈值乘以一个因子并将其传递回(固定)阈值函数:
//get the threshold computed by otsu:
double otsuThresh = cv::threshold( inputImage, otsuBinary, 0, 255,cv::THRESH_OTSU );
//tune the threshold value:
otsuThresh = 0.5 * otsuThresh;
//threshold the input image with the new value:
cv::threshold( inputImage, binaryFixed, otsuThresh, 255, cv::THRESH_BINARY );
有偏见的 Otsu 阈值的替代方法是进行基于区域的阈值处理,如下所示:
thr = .8
blur_hor = cv2.filter2D(img[:, :, 0], cv2.CV_32F, kernel=np.ones((11,1,1), np.float32)/11.0, borderType=cv2.BORDER_CONSTANT)
blur_vert = cv2.filter2D(img[:, :, 0], cv2.CV_32F, kernel=np.ones((1,11,1), np.float32)/11.0, borderType=cv2.BORDER_CONSTANT)
output = ((img[:,:,0]<blur_hor*thr) | (img[:,:,0]<blur_vert*thr)).astype(np.uint8)*255
这是我的例子。从左到右:
- 原图
- 灰度 + (3,3) 高斯模糊
- Otsu 阈值处理 + 反转像素
我想捕捉更多笔触的微弱部分。我知道 Otsu Thresholding 尝试在像素强度直方图的两个峰值之间应用阈值点,但我想稍微调整一下,以便捕获一些较亮的像素。
开箱即用吗?或者我需要手动操作吗?
我有橡皮鸭现象的礼貌回答。
th, th_img = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
返回元组的第0个索引(th
)是Otsu二值化算法选择的阈值。我可以丢弃 th_img
并在 th
中应用任何我喜欢的偏差,然后再将其用于常规二进制阈值处理。
desired_th = th*1.2
_, th_img = cv2.threshold(blur, desired_th, 255, cv2.THRESH_BINARY)
这是我得到的。通过清理可能出现在外面的不需要的斑点,我会得到我想要的东西。
在 C++ 中,我经常 "tune out" 将 (otsu) 阈值函数返回的阈值乘以一个因子并将其传递回(固定)阈值函数:
//get the threshold computed by otsu:
double otsuThresh = cv::threshold( inputImage, otsuBinary, 0, 255,cv::THRESH_OTSU );
//tune the threshold value:
otsuThresh = 0.5 * otsuThresh;
//threshold the input image with the new value:
cv::threshold( inputImage, binaryFixed, otsuThresh, 255, cv::THRESH_BINARY );
有偏见的 Otsu 阈值的替代方法是进行基于区域的阈值处理,如下所示:
thr = .8
blur_hor = cv2.filter2D(img[:, :, 0], cv2.CV_32F, kernel=np.ones((11,1,1), np.float32)/11.0, borderType=cv2.BORDER_CONSTANT)
blur_vert = cv2.filter2D(img[:, :, 0], cv2.CV_32F, kernel=np.ones((1,11,1), np.float32)/11.0, borderType=cv2.BORDER_CONSTANT)
output = ((img[:,:,0]<blur_hor*thr) | (img[:,:,0]<blur_vert*thr)).astype(np.uint8)*255