c#Emgu.cv用Stitching类做图片拼接
本人刚刚学习c#的图片拼接技术,通过网上找的相关资料,自己整合一下
系统环境为WIN10,软件开发环境为VISIO2019的C#,Emgu.cv版本4.3.0.3890
在visio开发软件中找到工具——NuGet包管理器,下载最新的Emgu4.3.0.3890。
(声明:本内容参照的是EMGU自带的例子,如有雷同,实属巧合)
同时找到emgu的安装位置如:emgucv-windesktop 4.3.0.3890\libs里的X86和X64文件放到项目所在的bin\debug下,否则会报错。
用windows窗体创建一个FORM,添加:
一个Button命名为”selectImagesButton“,
一个ImageBox,命名为”resultImageBox“,
一个DataGridView,命名为”sourceImageDataGridView“
代码如下:
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.Stitching;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using System.Diagnostics;
using Emgu.CV.UI;
namespace TESST_EMGU
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void selectImagesButton_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.CheckFileExists = true;
dlg.Multiselect = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
sourceImageDataGridView.Rows.Clear();
Image<Bgr, byte>[] sourceImages = new Image<Bgr, byte>[dlg.FileNames.Length];
for (int i = 0; i < sourceImages.Length; i++)
{
sourceImages[i] = new Image<Bgr, byte>(dlg.FileNames[i]);
using (Image<Bgr, byte> thumbnail = sourceImages[i].Resize(200, 200, Emgu.CV.CvEnum.Inter.Cubic, true))
{
DataGridViewRow row = sourceImageDataGridView.Rows[sourceImageDataGridView.Rows.Add()];
row.Cells["FileNameColumn"].Value = dlg.FileNames[i];
row.Cells["ThumbnailColumn"].Value = thumbnail.ToBitmap();
row.Height = 200;
}
}
try
{
//only use GPU if you have build the native binary from code and enabled "NON_FREE"
using (Stitcher stitcher = new Stitcher())
using (Emgu.CV.Features2D.AKAZE finder = new Emgu.CV.Features2D.AKAZE())
using (Emgu.CV.Stitching.WarperCreator warper = new SphericalWarper())
{
stitcher.SetFeaturesFinder(finder);
stitcher.SetWarper(warper);
using (VectorOfMat vm = new VectorOfMat())
{
Mat result = new Mat();
vm.Push(sourceImages);
Stopwatch watch = Stopwatch.StartNew();
this.Text = "Stitching";
Stitcher.Status stitchStatus = stitcher.Stitch(vm, result);
watch.Stop();
if (stitchStatus == Stitcher.Status.Ok)
{
resultImageBox.Image = result;
this.Text = String.Format("Stitched in {0} milliseconds.", watch.ElapsedMilliseconds);
}
else
{
MessageBox.Show(this, String.Format("Stiching Error: {0}", stitchStatus));
resultImageBox.Image = null;
}
}
}
}
finally
{
foreach (Image<Bgr, Byte> img in sourceImages)
{
img.Dispose();
}
}
}
}
}
}
点击按钮,选择几张图片(我选了四张图片如下),自动拼接,效果如最后一张图
只是将所用心得写上,有不足之处望指正