这是我的例子。左到右:
- 原始图像
- 灰度 + (3,3) 高斯模糊
- 大津阈值 + 反转像素
我想捕捉更多笔触的微弱部分。我知道 Otsu Thresholding 试图在像素强度直方图的两个峰值之间应用阈值点,但我想稍微偏一下,这样我就可以捕捉到一些较亮的像素。
有可能开箱即用吗?还是我需要手动做一些事情?
这是我的例子。左到右:
我想捕捉更多笔触的微弱部分。我知道 Otsu Thresholding 试图在像素强度直方图的两个峰值之间应用阈值点,但我想稍微偏一下,这样我就可以捕捉到一些较亮的像素。
有可能开箱即用吗?还是我需要手动做一些事情?
I have an answer courtesy of the rubber duck phenomenon.
th, th_img = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
The 0th index of the returned tuple (th
) is the threshold value that the Otsu Binarization algorithm chose. I can discard th_img
and apply whatever bias I like to th
before using it in regular binary thresholding.
desired_th = th*1.2
_, th_img = cv2.threshold(blur, desired_th, 255, cv2.THRESH_BINARY)
Here's what I get. By cleaning up the unwanted speckles that could appear on the outside, I'll get what I was looking for.
偏置 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
在 C++ 中,我经常“调出”(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 );