【问题标题】:Can I add a bias to Otsu thresholding in OpenCV?我可以在 OpenCV 中为 Otsu 阈值添加偏差吗?
【发布时间】:2020-01-17 11:54:28
【问题描述】:

这是我的例子。从左到右:

  1. 原图
  2. 灰度 + (3,3) 高斯模糊
  3. Otsu 阈值处理 + 反转像素

我想捕捉更多笔触的微弱部分。我了解 Otsu Thresholding 尝试在像素强度直方图的两个峰值之间应用阈值点,但我想稍微偏一些,以便捕获一些较亮的像素。

是否可以开箱即用?还是我需要手动操作?

【问题讨论】:

    标签: python opencv image-thresholding


    【解决方案1】:

    我有一个关于橡皮鸭现象的答案。

    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)
    

    这就是我得到的。通过清理可能出现在外面的不需要的斑点,我会得到我想要的。

    【讨论】:

      【解决方案2】:

      偏置 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】:

        在 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 );
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-05-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-23
          • 1970-01-01
          相关资源
          最近更新 更多