C++ opencv中自适应阈值和普通阈值的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8315505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between adaptive thresholding and normal thresholding in opencv
提问by Olivier_s_j
I have this gray video stream:
我有这个灰色视频流:
The histogram of this image:
此图像的直方图:
The thresholded image by :
阈值图像由:
threshold( image, image, 150, 255, CV_THRESH_BINARY );
i get :
我明白了:
Which i expect.
我期待。
When i do adaptive thresholding with :
当我使用以下方法进行自适应阈值处理时:
adaptiveThreshold(image, image,255,ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY,15,-5);
i get :
我明白了:
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
提前谢谢
采纳答案by djhaskin987
Adaptive Threshold works like this:
自适应阈值的工作原理是这样:
The function transforms a grayscale image to a binary image according to the formulas:
THRESH_BINARY
THRESH_BINARY_INV
where T(x,y) is a threshold calculated individually for each pixel.
该函数根据以下公式将灰度图像转换为二值图像:
THRESH_BINARY
THRESH_BINARY_INV
其中 T(x,y) 是为每个像素单独计算的阈值。
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.
所以这听起来像adaptiveThreshold逐个像素地计算阈值,而阈值计算整个图像的阈值——它用一个标尺测量整个图像,而另一个为每个像素制作一个新的“标尺”。
回答by nont
I had the same issue doing adaptive thresholding for OCR purposes. (sorry this is Python not C++)
我在为 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()
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.
最后一个参数是用于计算每个像素阈值的邻域大小。如果你的邻居太小(我的是 3),它的工作原理就像边缘检测。一旦我把它变大,它就会按预期工作。当然,“正确”的尺寸取决于图像的分辨率以及您正在查看的特征的尺寸。