threshold segmentation
- src: input image, only single-channel images can be input, usually grayscale images
- dst: output graph
- thresh: threshold
- maxval: When the pixel value exceeds the threshold (or is less than the threshold, depending on the type), the value assigned
- type: type of binary operation, including the following 5 types: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO; cv2.THRESH_TOZERO_INV
- cv2.THRESH_BINARY takes maxval (maximum value) for the part exceeding the threshold, otherwise takes 0
- cv2.THRESH_BINARY_INV Inversion of THRESH_BINARY
- cv2.THRESH_TRUNC The part greater than the threshold is set to the threshold, otherwise it remains unchanged
- cv2.THRESH_TOZERO The part greater than the threshold is not changed, otherwise it is set to 0
- cv2.THRESH_TOZERO_INV Inversion of THRESH_TOZERO
Threshold: global threshold
Fixed threshold vs. automatic threshold
AdaptiveThreshold: local threshold
Local mean binarization and local Gaussian binarization
public static void thresoldforexample()
{
var Src_Images = Cv2.ImRead("lenna.png");
Cv2.CvtColor(Src_Images, Src_Images,ColorConversionCodes.BGR2GRAY);
var Dstimage = new Mat();
//0-Binary 超过thresh阈值置位maxval 否则为0
//1-BinaryInv 与Binary 相反翻转 超过设置为0 否则为maxval
//2-Trunc 大于阈值部分设为阈值,否则不变
//3-Tozero 大于阈值部分不改变,否则设为0
//4-TozeroInv Tozero翻转 大于阈值设置为0 其余部分不改变
//8-OTSU 自动阈值
Cv2.Threshold(Src_Images, Dstimage, 100, 255, ThresholdTypes.Binary);
//var Dstimage2 = new Mat();
//Cv2.InRange(Src_Images,100,200, Dstimage);
var Dstimage1 = new Mat();
//MeanC 阈值是邻近区域的平均值减去常数C
//GaussianC 阈值是邻近区域的高斯加强总和减去常数C
//blocksize 邻近像素的大小,可理解为矩阵。
Cv2.AdaptiveThreshold(Src_Images, Dstimage1, 255, AdaptiveThresholdTypes.MeanC,ThresholdTypes.Binary,5,10);
Cv2.ImShow("1", Src_Images);
Cv2.ImShow("2", Dstimage);
Cv2.ImShow("3", Dstimage1);
}
Dual Threshold - Similar to Halcon. Select the grayscale value between two values
var Dstimage = new Mat();
//双阈值模式
Cv2.InRange(Src_Images,100,200, Dstimage);