5. OpenCvSharp's binarization function and trackBar threshold adjustment--c#OpenCvSharp study notes

OpenCvSharp's binarization function and trackBar threshold adjustment

0. Project overview

Use the binary image processing function in OpenCvSharp, use the trackBar control to achieve threshold adjustment, and display the results on the picturebox, and finally save the picture to the local disk.

Source code: https://download.csdn.net/download/sunsoldeir1/86513261

1 basic steps

To install and reference OpenCvSharp, OpenCvSharp.Extensions refer to the previous blog: https://blog.csdn.net/sunsoldeir1/article/details/126073489 .

2 interface design

2.1 In the toolbox on the left, search for Picturebox, Label, Button, groupBox, trackBar and other controls respectively and drag them into row layout, rename, adjust shape and font to form the following interface: 2.2 trackBar and other controls: adjust the Maximun of trackBar to
insert image description here
255
.
insert image description here
Adjust the Value of the trackBar to 127.
insert image description here

2.3 Open the picture and display it on picturebox1, use the following code to achieve:

 private void button1_Click(object sender, EventArgs e)//打开图片
        {
    
    
            OpenFileDialog file = new OpenFileDialog();//OpenFileDialog是一个类,实例化此类可以设置弹出一个文件对话框
            file.Filter = "JPG(*.JPG;*.JPEG);PNG文件(*.PNG);bmp文件(*.BMP);gif文件(*.GIF)|*.jpg;*.jpeg;*.png;*.bmp;*.gif";//文件类型过滤,只可选择图片的类型
            file.ShowDialog();//显示通用对话框
            if (file.FileName != string.Empty)
            {
    
    
                try
                {
    
    
                    pathname = file.FileName;
                    Img1 = Cv2.ImRead(pathname);//读取路径下的图片
                    pictureBox1.Load(pathname); //pictureBox1直接加载
             
                }
                catch (Exception ex)
                {
    
    
                    MessageBox.Show(ex.Message);
                }
            }
        }

2.4 Save the picture on picturebox2, use the following code to achieve:

 private void button3_Click(object sender, EventArgs e) //保存图片
        {
    
    
            SaveFileDialog saveImageDialog = new SaveFileDialog();
            saveImageDialog.Title = "图片保存";
            saveImageDialog.Filter = "jpg图片|*.JPG|gif图片|*.GIF|png图片|*.PNG|jpeg图片|*.JPEG|BMP图片|*.BMP";//文件类型过滤,只可选择图片的类型
            saveImageDialog.FilterIndex = 1;//设置默认文件类型显示顺序 
            saveImageDialog.FileName = "图片保存"; //设置默认文件名,可为空
            saveImageDialog.RestoreDirectory = true; //OpenFileDialog与SaveFileDialog都有RestoreDirectory属性,这个属性默认是false,打开一个文件后,那么系统默认目录就会指向刚才打开的文件。如果设为true就会使用系统默认目录
            if (saveImageDialog.ShowDialog() == DialogResult.OK)
            {
    
    
                string fileName = saveImageDialog.FileName.ToString();
                if (fileName != "" && fileName != null)
                {
    
    
                    string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString();
                    System.Drawing.Imaging.ImageFormat imgformat = null;
                    if (fileExtName != "")
                    {
    
    
                        switch (fileExtName)
                        {
    
    
                            case "jpg":
                                imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                                break;
                            case "png":
                                imgformat = System.Drawing.Imaging.ImageFormat.Png;
                                break;
                            case "gif":
                                imgformat = System.Drawing.Imaging.ImageFormat.Gif;
                                break;
                            case "bmp":
                                imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
                                break;
                            default:
                                imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                                break;
                        }
                        try
                        {
    
    
                            MessageBox.Show("保存路径:" + fileName, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            pictureBox2.Image.Save(fileName, imgformat);
                        }
                        catch
                        {
    
    
                            MessageBox.Show("图片保存失败!");
                        }
                    }
                }
            }
        }

3 function realization

3.1 Initialize variables

  Mat Img1 = new Mat();//用 Mat类定义图片
  Mat Img2 = new Mat();//用 Mat类定义图片
  Mat  ImgCvt = new Mat();//用 Mat类定义图片
  Bitmap bitmap;//Bitmap类定义picturebox2要显示的图片

3.2 Binarization adjustment

 label2.Text = trackBar1.Value.ToString();
            if (pictureBox2.Image == null)//判断图片是否已打开
            {
    
    
                MessageBox.Show("没有打开图片");
                return;
            }
            Img2 = ImgCvt.Threshold(trackBar1.Value, 255, ThresholdTypes.Binary);
            bitmap = BitmapConverter.ToBitmap(Img2); //把Mat格式的图片转换成Bitmap
            pictureBox2.Image = bitmap;

3.3 Realize the effect

insert image description here

Guess you like

Origin blog.csdn.net/sunsoldeir1/article/details/126797549