opencv中自适应阈值和普通阈值的区别

发布于 2024-12-18 18:13:23 字数 605 浏览 4 评论 0原文

我有这个灰色视频流: 在此处输入图像描述

该图像的直方图:

在此处输入图像描述

阈值图像:

  threshold( image, image, 150, 255, CV_THRESH_BINARY );

我得到:

>预计。

当我使用以下方法进行自适应阈值处理时:

adaptiveThreshold(image, image,255,ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY,15,-5);

我得到:

在此处输入图像描述

这看起来像边缘检测而不是阈值处理。我期望的是黑色和白色区域。所以我的问题是,为什么这看起来像边缘检测而不是阈值处理。

提前谢谢

I have this gray video stream:
enter image description here

The histogram of this image:

enter image description here

The thresholded image by :

  threshold( image, image, 150, 255, CV_THRESH_BINARY );

i get :

enter image description here

Which i expect.

When i do adaptive thresholding with :

adaptiveThreshold(image, image,255,ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY,15,-5);

i get :

enter image description here

Which looks like edge detection and not thresholding. What i expected was black and white areas . So my question is, why does this look like edge detection and not thresholding.

thx in advance

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

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

发布评论

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

评论(2

只是在用心讲痛 2024-12-25 18:13:23

自适应阈值的工作原理类似于这个

该函数将灰度图像转换为二值图像
到公式:

<前><代码> THRESH_BINARY

“THRESH_BINARY"

<前><代码> THRESH_BINARY_INV

“THRESH_BINARY_INV"

其中 T(x,y) 是为每个像素单独计算的阈值。

阈值的工作方式不同

该函数将固定级别阈值应用于单通道数组。

因此,听起来adaptiveThreshold逐像素计算阈值,而threshold则针对整个图像计算阈值——它用一个标尺测量整个图像,而另一个标尺为每个像素创建一个新的“标尺”。

Adaptive Threshold works like this:

The function transforms a grayscale image to a binary image according
to the formulas:

    THRESH_BINARY

THRESH_BINARY

    THRESH_BINARY_INV

THRESH_BINARY_INV

where T(x,y) is a threshold calculated individually for each pixel.

Threshold works differently:

The function applies fixed-level thresholding to a single-channel array.

So it sounds like adaptiveThreshold calculates a threshold pixel-by-pixel, whereas threshold calculates it for the whole image -- it measures the whole image by one ruler, whereas the other makes a new "ruler" for each pixel.

烟柳画桥 2024-12-25 18:13:23

我在为 OCR 目的进行自适应阈值处理时遇到了同样的问题。 (抱歉,这是 Python 而不是 C++)

img = cv.LoadImage(sys.argv[1])
bwsrc = cv.CreateImage( cv.GetSize(img), cv.IPL_DEPTH_8U, 1)
bwdst = cv.CreateImage( cv.GetSize(img), cv.IPL_DEPTH_8U, 1)

cv.CvtColor(img, bwsrc, cv.CV_BGR2GRAY)
cv.AdaptiveThreshold(bwsrc, bwdst, 255.0, cv.CV_THRESH_BINARY, cv.CV_ADAPTIVE_THRESH_MEAN_C,11)
cv.ShowImage("threshhold", bwdst)
cv.WaitKey()

最后一个参数是用于计算每个像素阈值的邻域大小。如果你的邻域太小(我的邻域是 3),它的工作原理类似于边缘检测。一旦我把它做得更大,它就按预期工作了。当然,“正确”的尺寸取决于图像的分辨率以及您正在查看的特征的尺寸。

I had the same issue doing adaptive thresholding for OCR purposes. (sorry this is Python not C++)

img = cv.LoadImage(sys.argv[1])
bwsrc = cv.CreateImage( cv.GetSize(img), cv.IPL_DEPTH_8U, 1)
bwdst = cv.CreateImage( cv.GetSize(img), cv.IPL_DEPTH_8U, 1)

cv.CvtColor(img, bwsrc, cv.CV_BGR2GRAY)
cv.AdaptiveThreshold(bwsrc, bwdst, 255.0, cv.CV_THRESH_BINARY, cv.CV_ADAPTIVE_THRESH_MEAN_C,11)
cv.ShowImage("threshhold", bwdst)
cv.WaitKey()

The last paramter is the size of the neighborhood used to calculate the threshold for each pixel. If your neighborhood is too small (mine was 3), it works like edge detection. Once I made it bigger, it worked as expected. Of course, the "correct" size will depend on the resolution of your image, and size of the features you're looking at.

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