Python 自适应阈值参数混淆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28763419/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:42:29  来源:igfitidea点击:

Adaptive Threshold parameters confusion

pythonopencv

提问by Maryam Tahir

Can anyone please tell me what are the parameters in these Adaptive Threshold functions and how are they controlling black and white pixels.

谁能告诉我这些自适应阈值函数中的参数是什么以及它们如何控制黑白像素。

cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
            cv2.THRESH_BINARY,11,2)
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
            cv2.THRESH_BINARY,11,2)

采纳答案by GPPK

Python: cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) → dst

Parameters:

参数:

src – Source 8-bit single-channel image.
dst – Destination image of the same size and the same type as src .
maxValue – Non-zero value assigned to the pixels for which the condition is satisfied. See the details below.
adaptiveMethod – Adaptive thresholding algorithm to use, ADAPTIVE_THRESH_MEAN_C or ADAPTIVE_THRESH_GAUSSIAN_C . See the details below.
thresholdType – Thresholding type that must be either THRESH_BINARY or THRESH_BINARY_INV .
blockSize – Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
C – Constant subtracted from the mean or weighted mean (see the details below). Normally, it is positive but may be zero or negative as well.

Taken from here:and it also explains the method in a lot more detail.

取自此处:并且还更详细地解释了该方法。

回答by meelo

Add to the answer from GPPK.

添加到 GPPK 的答案中。

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

该函数根据以下公式将灰度图像转换为二值图像:

  • THRESH_BINARY
  • THRESH_BINARY

enter image description here

在此处输入图片说明

  • THRESH_BINARY_INV
  • THRESH_BINARY_INV

enter image description here

在此处输入图片说明

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

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

  • For the method ADAPTIVE_THRESH_MEAN_C , the threshold value T(x,y) is a mean of the blockSize x blockSize neighborhood of (x, y) minus C .
  • For the method ADAPTIVE_THRESH_GAUSSIAN_C , the threshold value T(x, y) is a weighted sum (cross-correlation with a Gaussian window) of the blockSize x blockSize neighborhood of (x, y) minus C . The default sigma (standard deviation) is used for the specified blockSize .
  • 对于 ADAPTIVE_THRESH_MEAN_C 方法,阈值 T(x,y) 是 (x, y) 减去 C 的 blockSize x blockSize 邻域的平均值。
  • 对于方法 ADAPTIVE_THRESH_GAUSSIAN_C ,阈值 T(x, y) 是 (x, y) 减去 C 的 blockSize x blockSize 邻域的加权和(与高斯窗口的互相关)。默认 sigma(标准偏差)用于指定的 blockSize 。