且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Emgu-WPF学习使用-阈值化

更新时间:2022-09-18 13:17:30

原文:Emgu-WPF学习使用-阈值化

环境:Win8 64位 Vs2015

Emgu 版本:emgucv-windesktop 3.2.0.2682

Emgu-WPF学习使用-阈值化

上图为常用阈值化处理效果。不同阈值设置可呈现不同处理效果。

       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