Emgu-WPF學習使用-閾值化
阿新 • • 發佈:2018-08-14
back wpf alt its color ESS 灰度化 設置 route 原文:Emgu-WPF學習使用-閾值化
環境:Win8 64位 Vs2015
Emgu 版本:emgucv-windesktop 3.2.0.2682
上圖為常用閾值化處理效果。不同閾值設置可呈現不同處理效果。
private void InitSourceFile(object sender, RoutedEventArgs e) { string sFile = ""; if (!String.IsNullOrEmpty(AppConstUtils.GDefaultFile) && File.Exists(AppConstUtils.GDefaultFile)) sFile = AppConstUtils.GDefaultFile; else sFile = GlobalVar.DATAS_PATH + "Samples/Test3.png"; BitmapImage oOriginBitSrc = new BitmapImage(new Uri(sFile)); this.ImgOrigin1Zm.Source = oOriginBitSrc; System.Drawing.Image oImgOrigin = System.Drawing.Image.FromFile(sFile); System.Drawing.Bitmap oBitmap = new System.Drawing.Bitmap(oImgOrigin); Image<Bgr, byte> imgSrc = new Image<Bgr, byte>(oBitmap); oBitmap.Dispose(); this.Func1(imgSrc); this.Func2(imgSrc); this.Func3(imgSrc); this.Func4(imgSrc); this.Func5(imgSrc); } private void Func1(Image<Bgr, byte> imgSrc) { // 灰度化 Image<Gray, byte> imgGray = new Image<Gray, byte>(imgSrc.Size); CvInvoke.CvtColor(imgSrc, imgGray, ColorConversion.Bgr2Gray); AppUtils.ShowGrayImage(this.ImgFun1Result1Zm, imgGray);// 轉換為BitmapSource呈現 // 二進制閾值化 Image<Gray, byte> imgThresholdBinary = new Image<Gray, byte>(imgGray.Size); //90為閾值,可調整,255為最大值 CvInvoke.Threshold(imgGray, imgThresholdBinary, 90, 255, ThresholdType.Binary); AppUtils.ShowGrayImage(this.ImgFun1Result2Zm, imgThresholdBinary); //反向二進制閾值化 Image<Gray, byte> imgThresholdBinaryInv = new Image<Gray, byte>(imgGray.Size); CvInvoke.Threshold(imgGray, imgThresholdBinaryInv, 90, 255, ThresholdType.BinaryInv); AppUtils.ShowGrayImage(this.ImgFun1Result3Zm, imgThresholdBinaryInv); //截斷閾值化 Image<Gray, byte> imgThresholdTrunc = new Image<Gray, byte>(imgGray.Size); CvInvoke.Threshold(imgGray, imgThresholdTrunc, 90, 255, ThresholdType.Trunc); AppUtils.ShowGrayImage(this.ImgFun1Result4Zm, imgThresholdTrunc); //超閾值歸零化 Image<Gray, byte> imgThresholdToZero = new Image<Gray, byte>(imgGray.Size); CvInvoke.Threshold(imgGray, imgThresholdToZero, 90, 255, ThresholdType.ToZero); AppUtils.ShowGrayImage(this.ImgFun1Result5Zm, imgThresholdToZero); //低於閾值歸零化 Image<Gray, byte> imgThresholdToZeroInv = new Image<Gray, byte>(imgGray.Size); CvInvoke.Threshold(imgGray, imgThresholdToZeroInv, 150, 255, ThresholdType.ToZeroInv); AppUtils.ShowGrayImage(this.ImgFun1Result6Zm, imgThresholdToZeroInv); //Mask Image<Gray, byte> imgThresholdMask = new Image<Gray, byte>(imgGray.Size); CvInvoke.Threshold(imgGray, imgThresholdMask, 90, 255, ThresholdType.Mask); AppUtils.ShowGrayImage(this.ImgFun1Result7Zm, imgThresholdMask); //Otsu Image<Gray, byte> imgThresholdOtsu = new Image<Gray, byte>(imgGray.Size); CvInvoke.Threshold(imgGray, imgThresholdOtsu, 150, 255, ThresholdType.Otsu); AppUtils.ShowGrayImage(this.ImgFun1Result8Zm, imgThresholdOtsu); } private void Func2(Image<Bgr, byte> imgSrc) { // 灰度化 Image<Gray, byte> imgGray = new Image<Gray, byte>(imgSrc.Size); CvInvoke.CvtColor(imgSrc, imgGray, ColorConversion.Bgr2Gray); // 自適應閾值 Image<Gray, byte> imgAdapativeThresholdMeanC = imgGray.ThresholdAdaptive(new Gray(255), AdaptiveThresholdType.MeanC, ThresholdType.Binary, 9, new Gray(5)); AppUtils.ShowGrayImage(this.ImgFun2Result1Zm, imgAdapativeThresholdMeanC); } private void Func3(Image<Bgr, byte> imgSrc) { // 灰度化 Image<Gray, byte> imgGray = new Image<Gray, byte>(imgSrc.Size); CvInvoke.CvtColor(imgSrc, imgGray, ColorConversion.Bgr2Gray); // 自適應閾值 Image<Gray, byte> imgAdapativeThresholdGaussianC = imgGray.ThresholdAdaptive(new Gray(255), AdaptiveThresholdType.GaussianC, ThresholdType.Binary, 9, new Gray(5)); AppUtils.ShowGrayImage(this.ImgFun2Result2Zm, imgAdapativeThresholdGaussianC); } private void Func4(Image<Bgr, byte> imgSrc) { // 灰度化 Image<Gray, byte> imgGray = new Image<Gray, byte>(imgSrc.Size); CvInvoke.CvtColor(imgSrc, imgGray, ColorConversion.Bgr2Gray); // 自適應閾值 Image<Gray, byte> imgAdapativeThresholdMeanC = imgGray.ThresholdAdaptive(new Gray(255), AdaptiveThresholdType.MeanC, ThresholdType.BinaryInv, 9, new Gray(5)); AppUtils.ShowGrayImage(this.ImgFun2Result3Zm, imgAdapativeThresholdMeanC); } private void Func5(Image<Bgr, byte> imgSrc) { // 灰度化 Image<Gray, byte> imgGray = new Image<Gray, byte>(imgSrc.Size); CvInvoke.CvtColor(imgSrc, imgGray, ColorConversion.Bgr2Gray); // 自適應閾值 Image<Gray, byte> imgAdapativeThresholdGaussianC = imgGray.ThresholdAdaptive(new Gray(255), AdaptiveThresholdType.GaussianC, ThresholdType.BinaryInv, 9, new Gray(5)); AppUtils.ShowGrayImage(this.ImgFun2Result4Zm, imgAdapativeThresholdGaussianC); }
另外:
AppUtils.ShowGrayImage(Image oImg, Image<Bgr, byte> imgSrc); 在我的上一篇博客中有實現。
?點擊打開鏈接? http://blog.csdn.net/u013224722/article/details/79613445Emgu-WPF學習使用-閾值化