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

opencv 图片拼接;OpenCvSharp图片拼接

先看效果图:

核心代码:

            

//普通拼接:
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
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

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

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

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        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;
    }
}

 

  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
一、主要内容:OpenCV能够实现强大丰富的图像处理,但是它缺少一个能够支持它运行的界面。Csharp经过多年的发展,得益于它的“所见及所得”能力,非常方便编写界面。这两者如果能够“双剑合璧”,将有效帮助实际工作产出。本课着重推荐GOCW采用“Csharp基于CLR直接调用Opencv编写的算法库”方法,能够将最新的OpenCV技术引入进来,同时保证生成程序的最小化。    为了进一步说明Csharp和OpenCV的结合使用,首先一个较为完整的基于winform实现答题卡识别的例子,相比较之前的实现,本次进一步贴近生产实际、内涵丰富,对算法也进行了进一步提炼。同时我们对WPF下对OpenCV函数的调用、OpenCV.js的调用进行相关教授。       二、课程结构1、 EmguCV、OpenCVSharp和GOCW之间进行比较(方便代码编写、能够融入最新的算法、速度有保障、方便调试找错、拒绝黑箱化);2、视频采集模块的构建,视频采集和图像处理之间的关系;3、视频采集专用的SDK和“陪练”系统的介绍;4、在视频增强类项目中和图像处理项目中,算法的选择;5、Csharp界面设计、图片的存储和其他构建设计;6、较为完整的答题卡识别例子,兼顾界面设计和算法分析;8、WPF基于GOCW也同样可以基于GOCW实现算法调用;webForm虽然也可以通过类似方法调用,但是OpenCV.JS的方法更现代高效。9、关于软件部署的相关要点和窍门。       三、知识要点:1、基本环境构建和程序框架;2、CLR基本原理和应用方法;3、接入、采集、模拟输入;4、图像处理,通过构建循环采集图片;5、增强和实时处理;6、基于投影等技术的答题卡识别算法;7、存储、转换;8、部署交付。        课程能够帮助你掌握Csharp调用Opencv的基本方法,获得相应框架代码和指导;从而进一步提升实现“基于图像处理”的解决方案能力。  
C是计算机科学中的一门编程语言。它是由美国贝尔实验室的丹尼斯·里奇于20世纪70年代初开发的。C语言以其简洁、高效和可移植性而闻名,成为了许多计算机操作系统和应用程序的首选开发语言。 C语言的设计目标是提供一种底层的、通用的编程语言,能够运行在多种计算机平台上。它的语法与底层计算机硬件密切相关,允许程序员直接访问内存和硬件资源。这使得C语言非常适合开发系统软件和嵌入式系统。 C语言的语法简洁但功能强大。它支持多种数据类型,包括整数、浮点数、字符和指针等。同时,C语言具有丰富的控制结构,如条件语句、循环语句和函数等,使得程序员能够精确地控制程序的执行流程。 C语言的可移植性也是其重要特点之一。由于它与底层硬件密切相关,编写的C程序可以在不同的操作系统和计算机架构上运行,只需要进行一些简单的调整和编译。这种特性使得C语言成为了跨平台开发的首选语言。 虽然C语言的语法相对较低级,但它为程序员提供了良好的抽象能力和灵活性,使得程序开发更加高效。此外,C语言还提供了丰富的库函数,方便程序员开发各种应用程序。 总的来说,C语言在计算机科学中有着重要的地位,它是一门功能强大且广泛使用的编程语言。无论是操作系统、应用程序还是嵌入式系统的开发,C语言都是一种理想的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值