赞
踩
其中每个部分的原理相信在各种书籍和网站上都容易找到,这里是C#下用Emgucv实现以上相关操作的源代码全部,转载请注明http://write.blog.csdn.net/postlist
http://blog.csdn.net/yimingsilence/article/details/50382295
http://blog.csdn.net/yimingsilence/article/details/50382295
http://blog.csdn.net/yimingsilence/article/details/50382295
IntPtr original_ptr = CvInvoke.cvLoadImage(filename, Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_ANYCOLOR);
if (original_ptr == null)
{
MessageBox.Show("没有此图像");
return;
}
/*图像灰度化*/
IntPtr gray_ptr = CvInvoke.cvCreateImage(CvInvoke.cvGetSize(original_ptr), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);
CvInvoke.cvCvtColor(original_ptr, gray_ptr, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_BGR2GRAY);
/*灰度图片二值化*/
IntPtr byte_ptr = CvInvoke.cvCreateImage(CvInvoke.cvGetSize(gray_ptr), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);
CvInvoke.cvAdaptiveThreshold(gray_ptr, byte_ptr, 255, Emgu.CV.CvEnum.ADAPTIVE_THRESHOLD_TYPE.CV_ADAPTIVE_THRESH_GAUSSIAN_C, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY, 23, 5);
/*图像显示*/
MIplImage mipimage1 = (MIplImage)Marshal.PtrToStructure(byte_ptr, typeof(MIplImage));
Image<Gray, Byte> image = new Image<Gray, Byte>(mipimage1.width, mipimage1.height, mipimage1.widthStep, mipimage1.imageData);
//this.pictureBox1.Image = image.ToBitmap();
///*检测直线坐标集*/
IntPtr midImage = CvInvoke.cvCreateImage(CvInvoke.cvGetSize(byte_ptr), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);
//边缘检测和膨胀及腐蚀
CvInvoke.cvCanny(byte_ptr, midImage, 90, 120, 3);
CvInvoke.cvDilate(midImage, midImage, IntPtr.Zero, 1);
CvInvoke.cvErode(midImage, midImage, IntPtr.Zero, 1);
//霍夫变换
MemStorage storage = new MemStorage();
IntPtr intPtrHoughLines = CvInvoke.cvHoughLines2(midImage, storage, HOUGH_TYPE.CV_HOUGH_PROBABILISTIC, 1, Math.PI / 180, 50, 100, 10);
Seq<LineSegment2D> lineSegment = new Seq<LineSegment2D>(intPtrHoughLines, storage);
//画出检测结果
unsafe
{
for (int i = 0; i < lineSegment.Total; ++i)
{
Point* point = (Point*)CvInvoke.cvGetSeqElem(intPtrHoughLines, i);
CvInvoke.cvLine(original_ptr, point[0], point[1], new MCvScalar(255, 0, 0), 2, LINE_TYPE.CV_AA, 0);//在原图像中画线
}
}
// MIplImage resultHough = (MIplImage)Marshal.PtrToStructure(original_ptr, typeof(MIplImage));
CvInvoke.cvNamedWindow("result");
CvInvoke.cvShowImage("result", original_ptr);
关于opencv在不同语言(c,c++,c#)中的各种函数及类型的对应关系见:
http://blog.csdn.net/yimingsilence/article/details/50382349
http://blog.csdn.net/yimingsilence/article/details/50382349
提一点基础问题,要在C#中使用指针需要在项目的->属性->生成中选上 允许不安全代码,然后用指针的代码段要写在unsafe{}里面
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。