为什么图像上的阈值处理与包含其他东西的图​​像中的阈值处理会产生不同的结果?

发布于 2025-01-14 03:54:10 字数 1588 浏览 1 评论 0原文

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

在我的测试程序中,我读入了两个图像。一种是物体单独存在,另一种是物体与其他物体一起在图像中。当我将阈值应用于 image1 时,我得到了我想要的结果。然而,当我将阈值应用于 image2 时,我部分得到了我想要的结果,但其中一个块没有正确阈值。关于可能导致此问题的任何想法?

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)

I have an image that when I apply a binary threshold on it, it gives a different result compared to when it is in an image with other things. I'm not sure if I'm setting the parameters for the thresholding function incorrectly as I took the example from the OpenCV documentation. Link to documentation: https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html

In my test program, I read in two images. One where the object is by itself and another where it is in an image with other objects. When I applied the threshold to the image1 I got the result I wanted. However, when I applied the threshold to image2 I partially got the result I wanted, but one of the blocks did not threshold correctly. Any ideas as to what may be causing this?

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑寡妇 2025-01-21 03:54:10

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

This is because of your usage of OTSU. It sets some sort of automatic threshold. Please refer to the following link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文