Threshold
代码如下
static void Main(String[] args) { Mat img = CvInvoke.Imread(@"C:UsersdellPicturesfacesGray.png", ImreadModes.Grayscale); Mat dst = new Mat(); double thresholdValue = 122; double max = 255; CvInvoke.Threshold(img, dst, thresholdValue, max, ThresholdType.Binary); CvInvoke.Imshow("src", img); CvInvoke.Imshow("Binary", dst); CvInvoke.Threshold(img, dst, thresholdValue, max, ThresholdType.BinaryInv); CvInvoke.Imshow("BinaryInv", dst); CvInvoke.Threshold(img, dst, thresholdValue, max, ThresholdType.Otsu); CvInvoke.Imshow("Otsu", dst); CvInvoke.Threshold(img, dst, thresholdValue, max, ThresholdType.ToZero); CvInvoke.Imshow("ToZero", dst); CvInvoke.Threshold(img, dst, thresholdValue, max, ThresholdType.ToZeroInv); CvInvoke.Imshow("ToZeroInv", dst); CvInvoke.Threshold(img, dst, thresholdValue, max, ThresholdType.Trunc); CvInvoke.Imshow("Trunc", dst); CvInvoke.WaitKey(0); }
例子
class Program { static void Main(String[] args) { Mat img = CvInvoke.Imread(@"C:UsersdellPicturesfaces.png"); Mat dst = new Mat(); Mat weighted = new Mat(); weighted = sum_rgb(img, dst).Clone(); CvInvoke.Imshow("src", img); CvInvoke.Imshow("weighted", weighted); CvInvoke.Imshow("dst", dst); CvInvoke.WaitKey(0); } static Mat sum_rgb(Mat src,Mat dst) { int ch = src.NumberOfChannels; VectorOfMat vMat = new VectorOfMat(ch); CvInvoke.Split(src, vMat); Mat b = vMat[0]; Mat g = vMat[1]; Mat r= vMat[2]; Mat s = new Mat(); CvInvoke.AddWeighted(r, 1.0 / 3, g, 1.0 / 3, 0.0,s); CvInvoke.AddWeighted(s, 1.0, b, 1.0 / 3, 0.0, s); CvInvoke.Threshold(s, dst, 100, 100, ThresholdType.Trunc); return s; } }
自适应Threshold
代码
static void Main(String[] args) { Mat img = CvInvoke.Imread(@"C:UsersdellPicturesfaces.png",0); Mat Gaussian = new Mat(); Mat Mean = new Mat(); CvInvoke.AdaptiveThreshold(img, Gaussian, 255, AdaptiveThresholdType.GaussianC, ThresholdType.Binary, 11, 5); CvInvoke.AdaptiveThreshold(img, Mean, 255, AdaptiveThresholdType.MeanC, ThresholdType.Binary, 11, 5); CvInvoke.Imshow("src", img); CvInvoke.Imshow("Gaussian", Gaussian); CvInvoke.Imshow("Mean", Mean); CvInvoke.WaitKey(0); }
代码
static void Main(String[] args) { Mat img = CvInvoke.Imread(@"C:UsersdellPicturesfaces.png"); Mat dst = new Mat(); CvInvoke.BoxFilter(img, dst, DepthType.Default, new Size(3, 3), new Point(-1, -1)); CvInvoke.Imshow("src", img); CvInvoke.Imshow("boxFilter", dst); CvInvoke.Blur(img, dst, new Size(5, 5), new Point(-1, -1)); CvInvoke.Imshow("blur", dst); CvInvoke.GaussianBlur(img, dst, new Size(5, 5), 1); CvInvoke.Imshow("Gaussian", dst); CvInvoke.MedianBlur(img, dst, 3); CvInvoke.Imshow("median", dst); CvInvoke.BilateralFilter(img, dst, 5, 30.0, 2.0); CvInvoke.Imshow("Bilateral", dst); CvInvoke.WaitKey(0); }
效果如下