码迷,mamicode.com
首页 > Windows程序 > 详细

Emgu-WPF学习使用-阈值化

时间:2018-08-14 11:22:56      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:byte   link   eth   space   .net   put   gray   var   UNC   

原文: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/79613445


Emgu-WPF学习使用-阈值化

标签:byte   link   eth   space   .net   put   gray   var   UNC   

原文地址:https://www.cnblogs.com/lonelyxmas/p/9472686.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!