opencv 图片拼接;OpenCvSharp图片拼接;C# 版opencv 图片拼接;C# 图片拼接

opencv 图片拼接;OpenCvSharp图片拼接

先看效果图:

opencv 图片拼接;OpenCvSharp图片拼接;C# 版opencv 图片拼接;C# 图片拼接_第1张图片

核心代码:

            

//普通拼接:
Mat srcImg1 = new Mat(strImg1);
            Mat srcImg2 = new Mat(strImg2);

            Mat ret =new Mat();
            if (type == 1)
            {//上下拼接
                Cv2.VConcat(srcImg1, srcImg2, ret);
            }
            else
            {//左右拼接
                Cv2.HConcat(srcImg1, srcImg2, ret);
            }


stitch函数进行拼接:
 Mat srcImg1 = new Mat(strImg1);
            Mat srcImg2 = new Mat(strImg2);

            Mat[] images = new Mat[] { srcImg1, srcImg2 };
            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;
            }

全部的文件:https://download.csdn.net/download/TangLingBo/12581728

全部代码:

Form1:

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace opencv图片拼接
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSelectImg1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFi = new OpenFileDialog();
            openFi.Filter = "图像文件(JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png| JPeg 图像文件(*.jpg;*.jpeg)"
              + "|*.jpg;*.jpeg |GIF 图像文件(*.gif)|*.gif |BMP图像文件(*.bmp)|*.bmp|Tiff图像文件(*.tif;*.tiff)|*.tif;*.tiff|Png图像文件(*.png)"
              + "| *.png |所有文件(*.*)|*.*";
            if (openFi.ShowDialog() == DialogResult.OK)
            {
                txtImg1.Text = openFi.FileName;
                pbImg1.BackgroundImage = Image.FromFile(openFi.FileName);
            }
        }

        private void btnSelectImg2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFi = new OpenFileDialog();
            openFi.Filter = "图像文件(JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png| JPeg 图像文件(*.jpg;*.jpeg)"
              + "|*.jpg;*.jpeg |GIF 图像文件(*.gif)|*.gif |BMP图像文件(*.bmp)|*.bmp|Tiff图像文件(*.tif;*.tiff)|*.tif;*.tiff|Png图像文件(*.png)"
              + "| *.png |所有文件(*.*)|*.*";
            if (openFi.ShowDialog() == DialogResult.OK)
            {
                txtImg2.Text = openFi.FileName;
                pbImg2.BackgroundImage = Image.FromFile(openFi.FileName);
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            string strImg1 = txtImg1.Text.Trim();
            string strImg2 = txtImg2.Text.Trim();
            if (string.IsNullOrEmpty(strImg1))
            {
                MessageBox.Show("请选择图片1");
                return;
            }
            if (string.IsNullOrEmpty(strImg2))
            {
                MessageBox.Show("请选择图片2");
                return;
            }
           
            Image image1 = Image.FromFile(strImg1);
            Image image2 = Image.FromFile(strImg2);

            pbResult.BackgroundImage =null;

            Mat srcImg1 = new Mat(strImg1);
            Mat srcImg2 = new Mat(strImg2);

            Mat[] images = new Mat[] { srcImg1, srcImg2 };
            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;
            }
            pbResult.BackgroundImage = BitmapConverter.ToBitmap(pano);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strImg1 = txtImg1.Text.Trim();
            string strImg2 = txtImg2.Text.Trim();
            if (string.IsNullOrEmpty(strImg1))
            {
                MessageBox.Show("请选择图片1");
                return;
            }
            if (string.IsNullOrEmpty(strImg2))
            {
                MessageBox.Show("请选择图片2");
                return;
            }
            int type = rcbType1.Checked ? 1 : 0;//1=上下拼接,0=左右拼接
            Image image1 = Image.FromFile(strImg1);
            Image image2 = Image.FromFile(strImg2);
            if (type == 1)
            {
                if (image1.Width != image2.Width)
                {
                    MessageBox.Show("图片宽度不一致");
                    return;
                }
            }
            else
            {
                if (image1.Height != image2.Height)
                {
                    MessageBox.Show("图片高度不一致");
                    return;
                }
            }
            pbResult.BackgroundImage = null;

            Mat srcImg1 = new Mat(strImg1);
            Mat srcImg2 = new Mat(strImg2);

            Mat ret =new Mat();
            if (type == 1)
            {//上下拼接
                Cv2.VConcat(srcImg1, srcImg2, ret);
            }
            else
            {//左右拼接
                Cv2.HConcat(srcImg1, srcImg2, ret);
            }
            pbResult.BackgroundImage = BitmapConverter.ToBitmap(ret);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (pbResult.BackgroundImage==null)
            {
                MessageBox.Show("结果图为空");
                return;
            }

            SaveFileDialog savedialog = new SaveFileDialog();
            savedialog.Filter = "Jpg 图片|*.jpg|Bmp 图片|*.bmp|Png 图片|*.png";
            savedialog.FilterIndex = 0;
            savedialog.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
            savedialog.CheckPathExists = true;//检查目录
            savedialog.FileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + "-"; ;//设置默认文件名
            if (savedialog.ShowDialog() == DialogResult.OK)
            {
                pbResult.BackgroundImage.Save(savedialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);// image为要保存的图片
                MessageBox.Show(this, "图片保存成功!", "信息提示");
            }
        }

 
    }
}

界面控件:

namespace opencv图片拼接
{
    partial class Form1
    {
        /// 
        /// 必需的设计器变量。
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// 清理所有正在使用的资源。
        /// 
        /// 如果应释放托管资源,为 true;否则为 false。
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// 
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// 
        private void InitializeComponent()
        {
            this.btnSelectImg1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.txtImg1 = new System.Windows.Forms.TextBox();
            this.txtImg2 = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.btnSelectImg2 = new System.Windows.Forms.Button();
            this.pbImg1 = new System.Windows.Forms.PictureBox();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.pbImg2 = new System.Windows.Forms.PictureBox();
            this.pbResult = new System.Windows.Forms.PictureBox();
            this.label5 = new System.Windows.Forms.Label();
            this.btnStart1 = new System.Windows.Forms.Button();
            this.label6 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.rcbType1 = new System.Windows.Forms.RadioButton();
            this.rcbType2 = new System.Windows.Forms.RadioButton();
            this.btnSave = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.pbImg1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbImg2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbResult)).BeginInit();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // btnSelectImg1
            // 
            this.btnSelectImg1.Location = new System.Drawing.Point(905, 7);
            this.btnSelectImg1.Name = "btnSelectImg1";
            this.btnSelectImg1.Size = new System.Drawing.Size(75, 23);
            this.btnSelectImg1.TabIndex = 0;
            this.btnSelectImg1.Text = "选择";
            this.btnSelectImg1.UseVisualStyleBackColor = true;
            this.btnSelectImg1.Click += new System.EventHandler(this.btnSelectImg1_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(13, 12);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(47, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "图片1:";
            // 
            // txtImg1
            // 
            this.txtImg1.Location = new System.Drawing.Point(66, 9);
            this.txtImg1.Name = "txtImg1";
            this.txtImg1.ReadOnly = true;
            this.txtImg1.Size = new System.Drawing.Size(833, 21);
            this.txtImg1.TabIndex = 2;
            // 
            // txtImg2
            // 
            this.txtImg2.Location = new System.Drawing.Point(66, 36);
            this.txtImg2.Name = "txtImg2";
            this.txtImg2.ReadOnly = true;
            this.txtImg2.Size = new System.Drawing.Size(833, 21);
            this.txtImg2.TabIndex = 5;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(13, 39);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(47, 12);
            this.label2.TabIndex = 4;
            this.label2.Text = "图片2:";
            // 
            // btnSelectImg2
            // 
            this.btnSelectImg2.Location = new System.Drawing.Point(905, 34);
            this.btnSelectImg2.Name = "btnSelectImg2";
            this.btnSelectImg2.Size = new System.Drawing.Size(75, 23);
            this.btnSelectImg2.TabIndex = 3;
            this.btnSelectImg2.Text = "选择";
            this.btnSelectImg2.UseVisualStyleBackColor = true;
            this.btnSelectImg2.Click += new System.EventHandler(this.btnSelectImg2_Click);
            // 
            // pbImg1
            // 
            this.pbImg1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
            this.pbImg1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pbImg1.Location = new System.Drawing.Point(12, 96);
            this.pbImg1.Name = "pbImg1";
            this.pbImg1.Size = new System.Drawing.Size(292, 264);
            this.pbImg1.TabIndex = 6;
            this.pbImg1.TabStop = false;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(131, 368);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(35, 12);
            this.label3.TabIndex = 7;
            this.label3.Text = "图片1";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(131, 708);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(35, 12);
            this.label4.TabIndex = 9;
            this.label4.Text = "图片1";
            // 
            // pbImg2
            // 
            this.pbImg2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
            this.pbImg2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pbImg2.Location = new System.Drawing.Point(12, 441);
            this.pbImg2.Name = "pbImg2";
            this.pbImg2.Size = new System.Drawing.Size(292, 264);
            this.pbImg2.TabIndex = 8;
            this.pbImg2.TabStop = false;
            // 
            // pbResult
            // 
            this.pbResult.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
            this.pbResult.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pbResult.Location = new System.Drawing.Point(335, 96);
            this.pbResult.Name = "pbResult";
            this.pbResult.Size = new System.Drawing.Size(645, 630);
            this.pbResult.TabIndex = 10;
            this.pbResult.TabStop = false;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(639, 732);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(41, 12);
            this.label5.TabIndex = 11;
            this.label5.Text = "结果图";
            // 
            // btnStart1
            // 
            this.btnStart1.Location = new System.Drawing.Point(358, 63);
            this.btnStart1.Name = "btnStart1";
            this.btnStart1.Size = new System.Drawing.Size(134, 23);
            this.btnStart1.TabIndex = 12;
            this.btnStart1.Text = "stitch函数进行拼接";
            this.btnStart1.UseVisualStyleBackColor = true;
            this.btnStart1.Click += new System.EventHandler(this.btnStart_Click);
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(10, 72);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(65, 12);
            this.label6.TabIndex = 13;
            this.label6.Text = "拼接方向:";
            // 
            // panel1
            // 
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Controls.Add(this.rcbType2);
            this.panel1.Controls.Add(this.rcbType1);
            this.panel1.Location = new System.Drawing.Point(79, 63);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(106, 27);
            this.panel1.TabIndex = 14;
            // 
            // rcbType1
            // 
            this.rcbType1.AutoSize = true;
            this.rcbType1.Checked = true;
            this.rcbType1.Location = new System.Drawing.Point(3, 4);
            this.rcbType1.Name = "rcbType1";
            this.rcbType1.Size = new System.Drawing.Size(47, 16);
            this.rcbType1.TabIndex = 0;
            this.rcbType1.TabStop = true;
            this.rcbType1.Text = "上下";
            this.rcbType1.UseVisualStyleBackColor = true;
            // 
            // rcbType2
            // 
            this.rcbType2.AutoSize = true;
            this.rcbType2.Location = new System.Drawing.Point(56, 4);
            this.rcbType2.Name = "rcbType2";
            this.rcbType2.Size = new System.Drawing.Size(47, 16);
            this.rcbType2.TabIndex = 1;
            this.rcbType2.Text = "左右";
            this.rcbType2.UseVisualStyleBackColor = true;
            // 
            // btnSave
            // 
            this.btnSave.Location = new System.Drawing.Point(641, 65);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(75, 23);
            this.btnSave.TabIndex = 15;
            this.btnSave.Text = "保存结果图";
            this.btnSave.UseVisualStyleBackColor = true;
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(202, 63);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(88, 23);
            this.button1.TabIndex = 16;
            this.button1.Text = "普通拼接";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(992, 750);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.btnSave);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.btnStart1);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.pbResult);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.pbImg2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.pbImg1);
            this.Controls.Add(this.txtImg2);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.btnSelectImg2);
            this.Controls.Add(this.txtImg1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.btnSelectImg1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "拼接图片";
            ((System.ComponentModel.ISupportInitialize)(this.pbImg1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbImg2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbResult)).EndInit();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button btnSelectImg1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox txtImg1;
        private System.Windows.Forms.TextBox txtImg2;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button btnSelectImg2;
        private System.Windows.Forms.PictureBox pbImg1;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.PictureBox pbImg2;
        private System.Windows.Forms.PictureBox pbResult;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Button btnStart1;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.RadioButton rcbType2;
        private System.Windows.Forms.RadioButton rcbType1;
        private System.Windows.Forms.Button btnSave;
        private System.Windows.Forms.Button button1;
    }
}

 

你可能感兴趣的:(opencv,OpenCvSharp,opencv,图片拼接,OpenCvSharp图片拼接,图片拼接,C#,opencv,图片拼接,C#,图片拼接)