Posts List

道路识别demo

最近做的道路识别一开始终于弄懂了点东西,一开始在网上找到了一个简单的道路识别的opencvsharp的版本。我觉得opencvsharp真的是一个很好的东西,它封装了比opencv更多的数据结构和库,而且得益于.net平台的强大,使用起来也非常的便捷。唯一的缺点就是目前关于这方面的资料还是少之又少,后来我还是想一想把这个demo转换成cpp版本,也是一个非常简单的demo。 opencvsharp版本 using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using OpenCvSharp; namespace LaneDetection { class Program { [STAThread] static void Main() { CvCapture cap = CvCapture.FromFile("test1.mp4"); CvWindow w = new CvWindow("Lane Detection"); CvWindow canny = new CvWindow("Canny"); IplImage src, gray, dstCanny, halfFrame, smallImg; CvMemStorage storage = new CvMemStorage(); CvSeq lines; while (CvWindow.WaitKey(10) < 0) { src = cap.QueryFrame(); halfFrame = new IplImage(new CvSize(src.Size.Width / 2, src.Size.Height / 2), BitDepth.U8, 3); Cv.PyrDown(src, halfFrame, CvFilter.Gaussian5x5); gray = new IplImage(src.Size, BitDepth.U8, 1); dstCanny = new IplImage(src.Size, BitDepth.U8, 1); storage.Clear(); // Crop off top half of image since we're only interested in the lower portion of the video int halfWidth = src.Width / 2; int halfHeight = src.Height / 2; int startX = halfWidth - (halfWidth / 2); src.SetROI(new CvRect(0, halfHeight - 0, src.Width - 1, src.Height - 1)); gray.SetROI(src.GetROI()); dstCanny.SetROI(src.GetROI()); src.CvtColor(gray, ColorConversion.BgrToGray); Cv.Smooth(gray, gray, SmoothType.Gaussian, 5, 5); Cv.Canny(gray, dstCanny, 50, 200, ApertureSize.Size3); canny.Image = dstCanny; storage.Clear(); lines = dstCanny.HoughLines2(storage, HoughLinesMethod.Probabilistic, 1, Math.PI / 180, 50, 50, 100); for (int i = 0; i < lines.Total; i++) { CvLineSegmentPoint elem = lines.GetSeqElem<CvLineSegmentPoint>(i).Value; int dx = elem.P2.X - elem.P1.X; int dy = elem.P2.Y - elem.P1.Y; double angle = Math.Atan2(dy, dx) * 180 / Math.PI; if (Math.Abs(angle) <= 10) continue; if (elem.P1.Y > elem.P2.Y + 50 || elem.P1.Y < elem.P2.Y -50 ) { src.Line(elem.P1, elem.P2, CvColor.Red, 2, LineType.AntiAlias, 0); } } src.ResetROI(); storage.Clear(); w.Image = src; } } } } opencv版本 #include "stdafx.h" #include <highgui.h> #include <cv.h> #include <math.h> using namespace cv; using namespace std; #define INF 99999999 CvCapture* g_capture = NULL; int g_slider_pos = 0 ; int frame_count = 0; CvSeq* lines; int main(int argc,char* argv[]) { cvNamedWindow( "show"); g_capture=cvCreateFileCapture( "D:\\road.avi"); IplImage* frame; while(1) { CvMemStorage* storage = cvCreateMemStorage(); frame=cvQueryFrame(g_capture); //set the ROI of the original image int x = 0,y = frame->height/2; int width = frame->width,height = frame->height/2; if(!frame) break; cvSetImageROI(frame,cvRect(x,y,width,height)); IplImage* gray = cvCreateImage(cvGetSize(frame),8,1); cvCvtColor(frame,gray,CV_BGR2GRAY); cvCanny(gray,gray,50,100); cvShowImage("canny",gray); cvSmooth(gray,gray,CV_GAUSSIAN,3,1,0); //Hough lines = cvHoughLines2(gray,storage,CV_HOUGH_PROBABILISTIC,1,CV_PI/180,50,90,50); //select approprivate lines acoording to the slope for (int i = 0;i < lines->total;i ++) { double k =INF; CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i); int dx = line[1].x - line[0].x; int dy = line[1].x - line[0].y; double angle = atan2(dy,dx) * 180 /CV_PI; if (abs(angle) <= 10) continue; if (line[0].y > line[1].y + 50 || line[0].y < line[1].y - 50) { cvLine(frame,line[0],line[1],CV_RGB(255,0,0),2,CV_AA); } } cvResetImageROI(frame); cvShowImage( "show",frame); char c = cvWaitKey(33); if(c==27) break; } cvReleaseCapture(&g_capture); cvDestroyWindow( "show"); return 0; } 非常希望有弄这方面的人能和我讨论一下,若转载请注明出处,谢谢。

道路识别

我们老板希望我能在道路识别这个方面做一些东西,这方面的东西一直在看,但是一直都是模模糊糊,我希望自己能够用一个合适的方式总结一下道路识别的问题。 道路识别问题其实也正正恰好是识别问题中的一个方面,所以道路识别问题的关键也是在于如何选取一个特征来进行识别。道路识别问题可以使用不同的方法来解决,我打算从下面这些方面来阐述一下我自己对道路识别的一个理解: 我们要解决什么情况下的道路问题 其实我觉得这点挺重要的,因为很多计算机视觉的问题在不同的要求之下有各种各样的方法。我觉得很有必要弄清楚自己需要的目标,我是要解决什么样的问题。道路识别问题可以分为结构化的道路和非结构化的道路。结构化的道路就是比较标准的道路,有着清晰地道路线,高速公路,城市公路,这些都是典型的结构化道路。而非结构化道路就是往往没有规则,没有明显的道路线或者根本没有道路线,显然,这样的道路识别比较复杂。因为我自己还是一个菜鸟,所以我一直以来看的问题都是针对结构化的道路。 我们用什么样的特征来识别道路 我们道路识别是通过道路的特征来识别道路,要么或者道路线,或者区域什么的。我们通常所使用的方法就是检测道路线,比如道路之间的道路线,或者道路边缘。特征,我觉得就可以分为多个特征,包括边缘特征,纹理特征,颜色特征等等。道路线的检测,往往相当于一个边缘检测的问题,往往是等同于直线的检测。所以霍夫变换经常是应用于道路检测中。 我们要以什么样的方法来解决道路识别问题 设想我们在汽车上设置一个摄像头,在不停的获取道路的实时数据。我们如何通过对道路视频进行实时的相应处理从而帮助我们驾驶。道路识别现如今已经慢慢应用到辅助驾驶系统中,在国外已经有一些不少的成熟应用,但只是至今还未达到高度商业化的地步。而且这个问题在未来的无人驾驶肯定也是有着一席之地。我所了解的基本都是基于对视频图像的处理从而来解决道路是别的问题。