C#中Emgu的问题?

1.先是在程序中图像的导入,我是根据图像路径实现,其中path是string类型,是图像路径。 IntPtr img=CvInvoke.cvLoadImage(path, Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_ANYCOLOR); 2.图像灰度化处理,先创建一幅尺寸大小为为原图的8位图像GrayImg1: Rectangle cr = CvInvoke.cvGetImageROI(img1); int width = cr.Width; int height = cr.Height; IntPtr GrayImg1 = CvInvoke.cvCreateImage(cr.Size, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1); 现在就能使用cvCvtColor函数实现灰度化: CvInvoke.cvCvtColor(img1, GrayImg1, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_BGR2GRAY); 3.直方图的创建,并获取数据 int[] hist_size = new int[1] { 256 };//建一个数组来存放直方图数据 IntPtr HistImg=CvInvoke.cvCreateHist(1, hist_size, Emgu.CV.CvEnum.HIST_TYPE.CV_HIST_ARRAY, null, 1);//创建了一个空的直方图 CvInvoke.cvCalcHist(inPtr1, HistImg,false,System.IntPtr.Zero);//计算inPtr1指向图像的数据,并传入Histimg中,其中IntPtr[] inPtr1 = new IntPtr[1] { SubImg}。 现在要获取Histimg中的具体数据: for (int i = 0; i < 256; i++) { temphist[i] = CvInvoke.cvQueryHistValue_1D(histImg, i); } 这样在数组temphist中保存了直方图数据。 4.对第一步中由cvLoadImage导入的图像进行像素点的操作。由于img 是IntPtr类型无法直接进行操作,所以首先要进行格式的转化,把IntPtr型转换成MIplImage: Emgu.CV.Structure.MIplImage MIpImg = (Emgu.CV.Structure.MIplImage)System.Runtime.InteropServices.Marshal.PtrToStructure(img, typeof(Emgu.CV.Structure.MIplImage)); 然后再C#中使用unsafe中指针操作:npixel = (int)((byte*)img.imageData + img.widthStep * i)[j]; 5.在二值话的图像,对不为零的区域经行检测。 IntPtr Dyncontour = new IntPtr();//存放检测到的图像块的首地址 IntPtr Dynstorage = CvInvoke.cvCreateMemStorage(0);开辟内存区域 int n= CvInvoke.cvFindContours(tempimg, Dynstorage, ref Dyncontour, StructSize.MCvContour, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_CCOMP,Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, new Point(0, 0)); n表示检测到不为零区域的个数。 6.对第五步检测到的区域绘制轮廓 for(;DyncontourTemp!=null&&DyncontourTemp.Ptr.ToInt32()!=0;DyncontourTemp=DyncontourTemp.HNext) { CvInvoke.cvDrawContours(tempContImg, DyncontourTemp,new MCvScalar(255, 255, 255),new MCvScalar(255, 255, 255), 0, 1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, new Point(0, 0)); } 其中的DyncontourTemp为 Seq<Point> DyncontourTemp1= new Seq<Point>(Dyncontour, null);//方便对IntPtr类型进行操作 Seq<Point> DyncontourTemp=DyncontourTemp1; 7.对第五步检测出的区域的坐标提取,通过cvFindContours函数的调用在 Dyncontour中存放的是不为零区域坐标的值存储在内存中的首地址指针。 seq<Point> DyncontourTemp1= new Seq<Point>(Dyncontour, null); //方便对IntPtr类型进行操作 int total=contourImg.Total;//contourImg包含的元素的总数 int TempX = 0; int TempY = 0;int[,] contourArray = new int[2,total]; //获得轮廓的坐标值 for (int i = 0; i < total;i++ ) { contourArray[0,i]=contourImg[i].X; contourArray[1,i]=contourImg[i].Y; } http://www.oncoding.cn/2010/opencv-ios-programming/ http://www.opencv.org.cn/index.php/%E9%A6%96%E9%A1%B5 openCV 是一个开源图形图像识别库 吐血推荐以下在C#下开发图像处理应用和研究能用的开源的库,还有很多更强悍的功能库,但是没有源代码的不一一列出,一下库都经过测试,我正在使用,欢迎交流pypzengqs@126.com,QQ:1024343803 http://www.emgu.com/wiki/index.php/Tutorial#Emgu,这个库封装了OpenCV,可以在C#中使用OpenCV http://research.microsoft.com/en-us/um/cambridge/projects/infernet/default.aspx 这个库提供推理机和概率统计的功能,微软的产品 http://mathnet.opensourcedotnet.info/ 超级强悍的数学运算库,包含线性代数和信号处理以及符号运算模块 http://code.google.com/p/aforge/ C#图像处理库,吐血推荐 http://www.matthewajohnson.org/software/svm.html SVM.NET库支持支持向量机运算是libSVM http://www.csie.ntu.edu.tw/~cjlin/libsvm/ 的C#版本,该站点还提供VISION.NET库支持视频处理。 OPENCV相信搞图像的都知道,绝大部分的图像处理算法里面都实现了。
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();
}
}
}
}
}
}点击按钮,选择几张图片(我选了四张图片如下),自动拼接,效果如最后一张图只是将所用心得写上,有不足之处望指正

我要回帖

更多关于 当前线程不在单线程单元中 的文章

 

随机推荐