普通阈值
OpenCV中的阈值用于相对于提供的阈值分配像素值。在阈值处理中,将每个像素值与阈值进行比较,如果像素值小于阈值则设置为0,否则设置为最大值(一般为255)。
在OpenCV中,有多种阈值类型可供选择,以下是一些常见的阈值类型:
二进制阈值化:选择一个特定的阈值量,大于该阈值的像素点的灰度值设定为最大值(如8位灰度值最大为255),小于该阈值的像素点的灰度值设定为0。
反二进制阈值化:与二进制阈值化相似,只是最后的设定值相反。在8位灰度图中,例如大于阈值的设定为0,而小于该阈值的设定为255。
截断阈值化:同样需要选择一个阈值,图像中大于该阈值的像素点被设定为该阈值,小于该阈值的保持不变。
阈值化为0:选择一个阈值,然后对图像做如下处理:1. 像素点的灰度值大于该阈值的不进行任何改变;2. 像素点的灰度值小于该阈值的,其灰度值全部变为0。
反阈值化为0:与阈值化为0类似,只是像素点的灰度值大于该阈值的不进行任何改变,而小于该阈值的,其灰度值全部变为0。
其实就是调用threshold()函数进行阈值处理,它有4个参数:
参数1:需要处理的原图,一般是灰度图。
参数2:设定的阈值。
参数3:最大阈值,一般为255。
参数4:阈值的方式,主要有5种,分别为cv.THRESH_BINARY、cv.THRESH_BINARY_INV、cv.THRESH_TRUNC、cv.THRESH_TOZERO、cv.THRESH_TOZERO_INV。
/** @brief Applies a fixed-level threshold to each array element.
The function applies fixed-level thresholding to a multiple-channel array. The function is typically
used to get a bi-level (binary) image out of a grayscale image ( #compare could be also used for
this purpose) or for removing a noise, that is, filtering out pixels with too small or too large
values. There are several types of thresholding supported by the function. They are determined by
type parameter.
Also, the special values #THRESH_OTSU or #THRESH_TRIANGLE may be combined with one of the
above values. In these cases, the function determines the optimal threshold value using the Otsu's
or Triangle algorithm and uses it instead of the specified thresh.
@note Currently, the Otsu's and Triangle methods are implemented only for 8-bit single-channel images.
@param src input array (multiple-channel, 8-bit or 32-bit floating point).
@param dst output array of the same size and type and the same number of channels as src.
@param thresh threshold value.
@param maxval maximum value to use with the #THRESH_BINARY and #THRESH_BINARY_INV thresholding
types.
@param type thresholding type (see #ThresholdTypes).
@return the computed threshold value if Otsu's or Triangle methods used.
@sa adaptiveThreshold, findContours, compare, min, max
*/
CV_EXPORTS_W double threshold( InputArray src,

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



