6. Simple stitching of OpenCvSharp images--c#OpenCvSharp study notes

0. Project overview

The project realizes the simple stitching of two images based on OpenCvSharp, displays the result on the picturebox, and finally saves the image to the local disk.
Source code and test pictures: https://download.csdn.net/download/sunsoldeir1/86738201
insert image description here

1 Basic steps and interface design

Refer to the previous blog: https://blog.csdn.net/sunsoldeir1/article/details/126073489
1.1 Reference using OpenCvSharp; using OpenCvSharp.Extensions;
1.2 Layout, rename, adjust shape and Fonts, forming the following interface:
insert image description here

2 function realization

2.1 Initialize variables

        Mat Img1;//用 Mat类定义图片1
        Mat Img2;//用 Mat类定义图片2
        Mat Img3;//用 Mat类定义图片3
        Bitmap bitmap;//Bitmap类定义picturebox3要显示的图片

2.2 Open the picture
Double-click respectively: two Button controls
insert image description here
Deploy the following code in button1_Click to display picture 1 in Picturebox1 after clicking "Open Picture 1":

  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();//显示通用对话框
            string pathname;//定义图片打开路径
            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.3 Select the display mode shown in Figure 3.
Left-click the combobox1 control, click the ellipsis button in the "Items" of "Data", open the string collection editor, and enter: Normal, StretchImage, AutoSize, CenterImage, Zoom, and then click "OK ".

注:Normal(常规模式)
StretchImage(完全填充)、AutoSize(正常大小显示)、CenterImage(显示图像的中心部分)、Zoom(缩小或放大完全显示)
不改变图片大小,原始图片多大,显示就多大。
StretchImage、Zoom都会改变图片大小以适应picturebox,但是zoom感觉会被调整的更小
AutoSize 则是控件适应图片大小
CenterImage则是控件显示图片中部内容

insert image description here
insert image description here
Set comboBox1_SelectedIndexChanged to trigger the function after clicking the combobox1 control

insert image description here

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
    
    
            pictureBox3.SizeMode = (PictureBoxSizeMode)comboBox1.SelectedIndex;
            //将 pictureBox3的SizeMode设置为comboBox1选择标签的ID        
            }

2.4 Left and right splicing Double
-click the button of "left and right splicing operation", first of all, it is necessary to judge whether "picture 1" and "picture 2" are loaded with pictures, and whether the height or number of lines of the two pictures are equal. If they are not equal, return and give a message prompt .

 if (pictureBox1.Image == null || pictureBox2.Image == null )//判断图片或下拉栏目是否为空
            {
    
    
                MessageBox.Show("没有打开图片1或打开图片2或选择运算符");
                return;
            }
            else
            {
    
    


                if (Img1.Rows== Img2.Rows)
                {
    
    

                    Cv2.HConcat(Img1, Img2, Img3);
                }

                else
                {
    
    
                    MessageBox.Show("图片1和图片2要有相同的长度");
                    return;
                }


                bitmap = BitmapConverter.ToBitmap(Img3); //把Mat格式的图片转换成Bitmap
                pictureBox3.Image = bitmap;

            }

insert image description here
2.5 Up and down splicing Double
-click the button of "left and right splicing operation", first judge whether "picture 1" and "picture 2" are loaded with pictures, and whether the width or number of columns of the two pictures are equal, if not, return and give a message prompt .

if (pictureBox1.Image == null || pictureBox2.Image == null)//Determine whether the picture or the drop-down column is empty
{ MessageBox.Show("There is no opening picture 1 or opening picture 2 or selection operator"); return; } else {




            if (Img1.Cols == Img2.Cols)
            {

                Cv2.VConcat(Img1, Img2, Img3);
            }

            else
            {
                MessageBox.Show("图片1和图片2要有相同的宽度");
                return;
            }


            bitmap = BitmapConverter.ToBitmap(Img3); //把Mat格式的图片转换成Bitmap
            pictureBox3.Image = bitmap;

insert image description here
2.6Stitch function splicing Double
-click the button of "Stitch function splicing", first judge whether "Picture 1" and "Picture 2" have loaded pictures, and then judge whether Stitch is successfully executed, if not, return and give a message prompt.

Mat[] images = new Mat[] {
    
     Img1, Img2 };
            Stitcher stitcher = Stitcher.Create(Stitcher.Mode.Scans);
            Mat pano = new Mat();
            var status = stitcher.Stitch(images, pano);
            if (status != Stitcher.Status.OK)
            {
    
    
                MessageBox.Show("失败:" + status.ToString());
                return;
            }
            else 
            {
    
    
                bitmap = BitmapConverter.ToBitmap(pano); //把Mat格式的图片转换成Bitmap
                pictureBox3.Image = bitmap;

            }

insert image description here

2.7 Save pictures
Use the SaveFileDialog class to achieve.

insert image description here

 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);
                            pictureBox3.Image.Save(fileName, imgformat);
                        }
                        catch
                        {
    
    
                            MessageBox.Show("图片保存失败!");
                        }
                    }
                }
            }
        }

Guess you like

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