【技术分享】C# OpenCvSharp实现图像二值化的终极指南

效果

ffb819538ed97c4de5b14269b243c7bc.png

项目

bfa4a291f52c654f59ae2c11cb301fa5.png

代码

阈值

using OpenCvSharp;
using System;
using System.Windows.Forms;

namespace OpenCvSharp二值化工具
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string imgPath = "";
        Mat mat = new Mat();
        Mat temp = new Mat();
        Mat src_gray = new Mat();
        ThresholdTypes hresholdTypes;
        String CommandText = "";

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            imgPath = ofd.FileName;
            mat = new Mat(imgPath);
            Cv2.CvtColor(mat, src_gray, ColorConversionCodes.BGR2GRAY);
            pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
            Threshold();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //添加项:
            cBoxThresholdTypes.Items.Add(new ListItem("Binary", ThresholdTypes.Binary));
            cBoxThresholdTypes.Items.Add(new ListItem("BinaryInv", ThresholdTypes.BinaryInv));
            cBoxThresholdTypes.Items.Add(new ListItem("Trunc", ThresholdTypes.Trunc));
            cBoxThresholdTypes.Items.Add(new ListItem("Tozero", ThresholdTypes.Tozero));
            cBoxThresholdTypes.Items.Add(new ListItem("TozeroInv", ThresholdTypes.TozeroInv));
            //设置选中项:
            cBoxThresholdTypes.SelectedIndex = 0;    //根据索引

        }

        void ShowImg(Mat mat)
        {

            pictureBox2.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
        }

        private void tBarThreshold_Scroll(object sender, EventArgs e)
        {
            if (tBarMaxval.Value < tBarThresh.Value)
            {
                tBarThresh.Value = tBarMaxval.Value;
            }
            Threshold();
        }

        private void tBarmaxval_Scroll(object sender, EventArgs e)
        {
            if (tBarMaxval.Value < tBarThresh.Value)
            {
                tBarMaxval.Value = tBarThresh.Value;
            }
            Threshold();
        }

        void Threshold()
        {
            if (pictureBox1.Image == null)
            {
                return;
            }

            ListItem li = (ListItem)cBoxThresholdTypes.SelectedItem;
            hresholdTypes = (ThresholdTypes)li.Value;
            String typeText = hresholdTypes.ToString();
            if (chkBoxOtsu.Checked == true)
            {
                hresholdTypes = hresholdTypes | ThresholdTypes.Otsu;
                typeText += "|Otsu";
            }
            if (chkBoxTriangle.Checked == true)
            {
                hresholdTypes = hresholdTypes | ThresholdTypes.Triangle;
                typeText += "|Triangle";
            }
            CommandText = String.Format("Cv2.Threshold(src,dst,{0},{1},{2})", tBarThresh.Value, tBarMaxval.Value, typeText);
            txtCommandText.Text = CommandText;
            Cv2.Threshold(src_gray, temp, tBarThresh.Value, tBarMaxval.Value, hresholdTypes);
            ShowImg(temp);
        }

        private void cBoxThresholdTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            Threshold();
        }

        private void chkBoxOtsu_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBoxOtsu.Checked == true)
            {
                chkBoxTriangle.Checked = false;
            }
            Threshold();
        }

        private void chkBoxTriangle_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBoxTriangle.Checked == true)
            {
                chkBoxOtsu.Checked = false;
            }
            Threshold();
        }
    }
}

自适应阈值

using OpenCvSharp;
using System;
using System.Windows.Forms;

namespace OpenCvSharp二值化工具
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string imgPath = "";
        Mat mat = new Mat();
        Mat temp = new Mat();
        Mat src_gray = new Mat();
        ThresholdTypes hresholdTypes;
        String CommandText = "";

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            imgPath = ofd.FileName;
            mat = new Mat(imgPath);
            Cv2.CvtColor(mat, src_gray, ColorConversionCodes.BGR2GRAY);
            pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
            Threshold();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //添加项:
            cBoxThresholdTypes.Items.Add(new ListItem("Binary", ThresholdTypes.Binary));
            cBoxThresholdTypes.Items.Add(new ListItem("BinaryInv", ThresholdTypes.BinaryInv));
            cBoxThresholdTypes.Items.Add(new ListItem("Trunc", ThresholdTypes.Trunc));
            cBoxThresholdTypes.Items.Add(new ListItem("Tozero", ThresholdTypes.Tozero));
            cBoxThresholdTypes.Items.Add(new ListItem("TozeroInv", ThresholdTypes.TozeroInv));
            //设置选中项:
            cBoxThresholdTypes.SelectedIndex = 0;    //根据索引

            cboxAdaptiveThresholdTypes.Items.Add(new ListItem("GaussianC", AdaptiveThresholdTypes.GaussianC));
            cboxAdaptiveThresholdTypes.Items.Add(new ListItem("MeanC", AdaptiveThresholdTypes.MeanC));
            cboxAdaptiveThresholdTypes.SelectedIndex = 0;
        }

        void Threshold()
        {
            if (pictureBox1.Image == null)
            {
                return;
            }

            ListItem li = (ListItem)cBoxThresholdTypes.SelectedItem;
            ListItem li2 = (ListItem)cboxAdaptiveThresholdTypes.SelectedItem;

            CommandText = String.Format("Cv2.AdaptiveThreshold(src,dst,{0},{1},{2},{3},{4})"
                                    , tBarMaxval.Value
                                    , ((AdaptiveThresholdTypes)li2.Value).ToString()
                                    , ((ThresholdTypes)li.Value).ToString()
                                    , tBarBlockSize.Value
                                    , tBarC.Value);

            txtCommandText.Text = CommandText;

            Cv2.AdaptiveThreshold(src_gray, temp
                                , tBarMaxval.Value
                                , (AdaptiveThresholdTypes)li2.Value
                                , (ThresholdTypes)li.Value
                                , tBarBlockSize.Value
                                , tBarC.Value);

            ShowImg(temp);
        }

        void ShowImg(Mat mat)
        {
            pictureBox2.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
        }

        private void cboxAdaptiveThresholdTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            Threshold();
        }

        private void cBoxThresholdTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            Threshold();
        }

        private void tBarBlockSize_Scroll(object sender, EventArgs e)
        {

            if (tBarBlockSize.Value % 2 != 1)
            {
                tBarBlockSize.Value++;
            }
            Threshold();
        }

        private void tBarC_Scroll(object sender, EventArgs e)
        {
            Threshold();
        }

        private void tBarMaxval_Scroll(object sender, EventArgs e)
        {
            Threshold();
        }

    }
}

局部阈值

using OpenCvSharp;
using OpenCvSharp.XImgProc;
using System;
using System.Windows.Forms;

namespace OpenCvSharp二值化工具
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string imgPath = "";
        Mat mat = new Mat();
        Mat temp = new Mat();
        Mat src_gray = new Mat();
        ThresholdTypes hresholdTypes;
        String CommandText = "";

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            imgPath = ofd.FileName;
            mat = new Mat(imgPath);
            Cv2.CvtColor(mat, src_gray, ColorConversionCodes.BGR2GRAY);
            pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
            Threshold();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            //添加项:
            cBoxThresholdTypes.Items.Add(new ListItem("Binary", ThresholdTypes.Binary));
            cBoxThresholdTypes.Items.Add(new ListItem("BinaryInv", ThresholdTypes.BinaryInv));
            cBoxThresholdTypes.Items.Add(new ListItem("Trunc", ThresholdTypes.Trunc));
            cBoxThresholdTypes.Items.Add(new ListItem("Tozero", ThresholdTypes.Tozero));
            cBoxThresholdTypes.Items.Add(new ListItem("TozeroInv", ThresholdTypes.TozeroInv));
            //设置选中项:
            cBoxThresholdTypes.SelectedIndex = 0;    //根据索引

        }

        void Threshold()
        {
            if (pictureBox1.Image == null)
            {
                return;
            }

            ListItem li = (ListItem)cBoxThresholdTypes.SelectedItem;

            double delta=tBarDelta.Value/10000.0D;

            CommandText = String.Format("CvXImgProc.NiblackThreshold(src,dst,{0},{1},{2},{3})"
                                    , tBarMaxval.Value
                                    , ((ThresholdTypes)li.Value).ToString()
                                    , tBarBlockSize.Value
                                    , delta);

            txtCommandText.Text = CommandText;

            CvXImgProc.NiblackThreshold(src_gray, temp, tBarMaxval.Value, (ThresholdTypes)li.Value
                ,  tBarBlockSize.Value
                ,  delta);


            ShowImg(temp);
          
        }

        void ShowImg(Mat mat)
        {
            pictureBox2.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
        }

        private void cBoxThresholdTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            Threshold();
        }

        private void tBarBlockSize_Scroll(object sender, EventArgs e)
        {
            if (tBarBlockSize.Value % 2 != 1)
            {
                tBarBlockSize.Value++;
            }
            Threshold();
        }

        private void tBarDelta_Scroll(object sender, EventArgs e)
        {
            Threshold();
        }

        private void tBarMaxval_Scroll(object sender, EventArgs e)
        {
            Threshold();
        }
    }
}

InRange

using OpenCvSharp;
using System;
using System.Windows.Forms;

namespace OpenCvSharp二值化工具
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string imgPath = "";
        Mat mat = new Mat();
        Mat temp = new Mat();
        Mat src_hsv = new Mat();
        String CommandText = "";

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            imgPath = ofd.FileName;
            mat = new Mat(imgPath);
            Cv2.CvtColor(mat, src_hsv, ColorConversionCodes.BGR2HSV);
            pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
            Threshold();
        }

        void Threshold()
        {
            if (pictureBox1.Image == null)
            {
                return;
            }

            Scalar low = new Scalar(tBarHLow.Value, tBarSLow.Value, tBarVLow.Value);
            Scalar hight = new Scalar(tBarHHight.Value, tBarSHight.Value, tBarVHight.Value);
            Cv2.InRange(src_hsv, low, hight, temp);

            CommandText = String.Format("Cv2.InRange(src,{0},{1},dst)"
                                    , String.Format("new Scalar({0},{1},{2})", tBarHLow.Value, tBarSLow.Value, tBarVLow.Value)
                                    , String.Format("new Scalar({0},{1},{2})", tBarHHight.Value, tBarSHight.Value, tBarVHight.Value)
                                    );

            txtCommandText.Text = CommandText;

            ShowImg(temp);
        }

        void ShowImg(Mat mat)
        {
            pictureBox2.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
        }

        private void tBarHLow_Scroll(object sender, EventArgs e)
        {
            if (tBarHLow.Value>tBarHHight.Value)
            {
                tBarHLow.Value = tBarHHight.Value;
            }
            Threshold();
        }

        private void tBarHHight_Scroll(object sender, EventArgs e)
        {
            if (tBarHLow.Value > tBarHHight.Value)
            {
                tBarHHight.Value = tBarHLow.Value;
            }
            Threshold();
        }

        private void tBarSLow_Scroll(object sender, EventArgs e)
        {
            if (tBarSLow.Value > tBarSHight.Value)
            {
                tBarSLow.Value = tBarSHight.Value;
            }
            Threshold();
        }

        private void tBarSHight_Scroll(object sender, EventArgs e)
        {
            if (tBarSLow.Value > tBarSHight.Value)
            {
                tBarSHight.Value = tBarSLow.Value;
            }
            Threshold();
        }

        private void tBarVLow_Scroll(object sender, EventArgs e)
        {
            if (tBarVLow.Value > tBarVHight.Value)
            {
                tBarVLow.Value = tBarVHight.Value;
            }
            Threshold();
        }

        private void tBarVHight_Scroll(object sender, EventArgs e)
        {
            if (tBarVLow.Value > tBarVHight.Value)
            {
                tBarVHight.Value = tBarVLow.Value;
            }
            Threshold();
        }

    }
}

964edc28bf02539e82c3e2c29c0380d0.gif

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值