编程技术 · 2018 年 12 月 06 日 0

C#调用Emgu完成图像拼接

用VS2015,Emgu 3.0.0版 完成多张图像拼接
(默认前提:读者已安装好VS和Emgu,并配置好开发环境,熟悉VS和C#)
—————————————————————————————————–

第一步: 打开VS,新建一个C#的WinForm项目并命名。
第二步: 给项目添加引用,添加Emgu.CV, Emgu.CV.Stitching , Emgu.CV.UI, Emgu.Util
第三步: 给项目添加组件。点击 工具—>选择工具箱项—–> .NET Framework组件—>浏览—>选择Emgu的bin文件夹下的Emgu.CV.UI, 添加进来—–>确认
第四步: 在form1.cs中引用头文件,以方便程序调用。using Emgu.CV; using Emgu.CV.CvEnum; using Emgu.CV.Structure; using Emgu.CV.Stitching; using Emgu.CV.Util;
第五步: 在设计窗口中添加四个PictureBox,分别命名为PictureBox1,PictureBox2,PictureBox3,PictureBox4。再添加一个TextBox命名为ShowResult。
第六步: 在Form1.cs中添加以下代码:

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;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Stitching;
using Emgu.CV.Util;

namespace Image_mosaicking
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Image<Bgr, byte>[] sources;
            OpenFileDialog openImage = new OpenFileDialog();
            openImage.CheckFileExists = true;
            openImage.Multiselect = true;
            openImage.Filter = "open files|*.jpg";
            openImage.ShowDialog();
            sources = new Image<Bgr, byte>[openImage.FileNames.Length];  //图像集初始化,确定了图像集的页数。
            //接着,确定图像集中每一页图像的内容。
            for (int i = 0; i < openImage.FileNames.Length; i++)
            {
                sources[i] = new Image<Bgr, byte>(openImage.FileNames[i]);

            }

            Stitcher stitcher = new Stitcher(true);  //true代表使用GPU,false代表不使用GPU
            Mat panoramic_img = new Mat();
            bool ok = true;  //定义并初始化ok,用来标识是否拼接成功
            try
            {
                ok = stitcher.Stitch(new VectorOfMat(new Mat[] { sources[0].Mat, sources[1].Mat, sources[2].Mat }), panoramic_img);
            }  //ok为真,则拼接成功;ok为假,则拼接失败。
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            if (ok)//如果拼接成功,则把前三幅图像展示在前三个相框中,把拼接好的图像展示在第四个相框中。并在文本框中显示"Succeed"。
            {
                pictureBox1.Image = sources[0].Bitmap;
                pictureBox2.Image = sources[1].Bitmap;
                pictureBox3.Image = sources[2].Bitmap;
                pictureBox4.Image = panoramic_img.Bitmap;
                ShowResult.Text = "Succeed!";
            }
            else
            {
                ShowResult.Text = "Failed!";//如果拼接失败,则在本文框中显示“Failed”。
            }
        }
    }
}

第七步: 生成解决方案,并进行调试。如果调试出错,查看它的详细信息。

详细信息中显示:”无法加载 DLL“cvextern”: 找不到指定的模块。读者可以在Emgu的bin文件下的x86或者x64中找到 cvextern.dll文件。并将其复制到本项目中的bin文件下的debug文件中。再进行调试,拼接成功!

———————
作者:天空之外
来源:CSDN
原文:https://blog.csdn.net/xiaoshijie2016/article/details/80812079
版权声明:本文为博主原创文章,转载请附上博文链接!