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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DuelCode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值