为什么对图像进行阈值处理与在图像中与其他事物相比会给出不同的结果?

青葱年少 python 197

原文标题Why does thresholding on image give a different result compared to when it is in an image with other things?

我有一个图像,当我在其上应用二进制阈值时,它给出的结果与它在具有其他事物的图像中时不同。我不确定我是否错误地设置了阈值函数的参数,因为我示例取自OpenCV文档。文档链接:https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html

在我的测试程序中,我读入了两张图片。一种是物体本身,另一种是它与其他物体的图像。当我将阈值应用于 image1 时,我得到了我想要的结果。但是,当我将阈值应用于 image2 时,我部分得到了我想要的结果,但是其中一个块的阈值不正确。关于可能导致这种情况的任何想法?

Image1 and its resultant threshold

Image2 and its resultant threshold

#Reading in image1
img1 = cv2.imread("Templates/YellowBlocks/SS.png", cv2.IMREAD_COLOR)
#Reading in image2
img2 = cv2.imread("Test Program 1.png", cv2.IMREAD_COLOR)
#Converting image1 to grayscale
grayImg1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
#Converting image2 to grayscale
grayImg2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
#Applying a binary threshold on image1 to show text in black and white
ret1,thresh1 = cv2.threshold(grayImg1, 0, 255,cv2.THRESH_OTSU+cv2.THRESH_BINARY)
#Applying a binary threshold on image2 to show text in black and white
ret2,thresh2 = cv2.threshold(grayImg2, 0, 255,cv2.THRESH_OTSU+cv2.THRESH_BINARY)
#Displaying image1
cv2.imshow('img1', img1)
#Displaying thresholded image1
cv2.imshow('thresh1', thresh1)
#Displaying image2
cv2.imshow('img2', img2)
#Displaying thresholded image2
cv2.imshow('thresh2', thresh2)
cv2.waitKey(0)

原文链接:https://stackoverflow.com//questions/71477356/why-does-thresholding-on-image-give-a-different-result-compared-to-when-it-is-in

回复

我来回复
  • Esraa Abdelmaksoud的头像
    Esraa Abdelmaksoud 评论

    这是因为您使用了 OTSU。它设置了某种自动阈值。请参考以下链接

    2年前 0条评论