【问题标题】:why does opencv threshold returns absurd output on a very simple image?为什么opencv阈值会在一个非常简单的图像上返回荒谬的输出?
【发布时间】:2021-07-14 07:37:43
【问题描述】:

我正在尝试使用 cv2 阈值计算图像中的种子。测试图像如下:

当我运行以下代码来创建掩码时:

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('S__14278933.jpg')
#img = cv2.fastNlMeansDenoisingColored(img,None,10,10,7,21)

mask = cv2.threshold(img[:, :, 0], 255, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
plt.imshow(mask)

我得到以下面具:

但理想情况下,它应该在中心给出一个小黄点。我已经用其他图片试过了,效果很好。

有人可以帮忙吗?

【问题讨论】:

    标签: python-3.x opencv image-processing opencv3.0


    【解决方案1】:

    试试看

    img=cv2.imread('foto.jpg',0)
    mask = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV )[1]
    

    【讨论】:

    • 我得到了相同的图像,几乎。
    【解决方案2】:

    您图像中的照明似乎不均匀。尝试使用Adaptive Thresholding

    import cv2
    import numpy as np
    
    # image path
    path = "D://opencvImages//"
    fileName = "c6pBO.jpg"
    
    # Reading an image in default mode:
    inputImage = cv2.imread(path + fileName)
    
    # Convert the image to grayscale:
    grayImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
    
    # Get binary image via Adaptive Thresholding :
    windowSize = 31
    windowConstant = 40
    binaryImage = cv2.adaptiveThreshold( grayImage, 255, cv2.ADAPTIVE_THRESH_MEAN_C, 
                                         cv2.THRESH_BINARY_INV, windowSize, windowConstant )
    
    cv2.imshow("binaryImage", binaryImage)
    cv2.waitKey(0)
    

    不过,您可能希望在此之后应用 Area Filter,因为图像上的暗部分会产生噪点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-14
      • 2017-05-18
      • 2013-08-08
      • 2014-07-03
      • 2023-03-17
      • 2022-01-13
      • 1970-01-01
      • 2018-03-05
      相关资源
      最近更新 更多