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

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

我有一张图像,当我在其上应用二进制阈值时,它给出的结果与它在包含其他东西的图​​像中时的结果不同。我不确定我是否错误地设置了阈值函数的参数,因为我从 OpenCV 文档中获取了示例。 Link 到文档:https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html

在我的测试程序中,我读取了两张图片。一个是对象本身,另一个是它与其他对象在图像中。当我将阈值应用于 image1 时,我得到了我想要的结果。但是,当我将阈值应用于 image2 时,我部分得到了我想要的结果,但是其中一个块没有正确设置阈值。关于可能导致此问题的原因有什么想法吗?

#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)

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