长春理工大学本科毕业论文
附录1
车道线识别程序源代码,编译要求:Linux Kernel2.6或以上,Glibc2.13或以上,OpenCV2.1或以上,Gcc4.7或以上版本。 #include
#include
#define RED cvScalar(0,0,255,0) #define GREEN cvScalar(0,255,0,0) #define BLUE cvScalar(255,0,0,0)
#define ROI cvRect(0,(int)src->height / 3,src->width,src->height) #define LOW_THRESH 80 #define HIGH_THRESH 255 #define MAXDISTANCE 10
//img为加载的源图像,src为处理过程中所用的灰度图像 //temp为临时需要时的中转图像,out为最后输出的三通道图像 IplImage *img,*src,*temp,*out; CvMemStorage *storage; CvSeq *lines; CvSeq *contours; typedef struct {
float a; float b; float c;
29
长春理工大学本科毕业论文
int is_vertical; }Line;
Line *get_tangent(CvSeq *seq,int n); Line get_line_func(CvPoint p1,CvPoint p2);
CvPoint *get_join(CvPoint *p1,Line l1,CvPoint *p2,Line l2); CvPoint get_center(CvPoint p1,CvPoint p2); double points_distance(CvPoint p1,CvPoint p2); int is_ellipse(CvSeq *); int main(int argc,char *argv[]) {
//图像的加载过程 if (argc != 2) { puts(\ exit(1);
}
if ((img = cvLoadImage(argv[1],CV_LOAD_IMAGE_UNCHANGED)) NULL)
{ printf(\ exit(1);
}
src = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1); temp = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1); out = cvCreateImage(cvGetSize(img),img->depth,3); if (img->nChannels >= 3) { cvCvtColor(img,src,CV_RGB2GRAY); cvCopy(img,out,NULL);
}
30
== 长春理工大学本科毕业论文
else { cvCopy(img,src,0);
cvCvtColor(img,out,CV_GRAY2RGB);
}
storage = cvCreateMemStorage(0); //设置ROI
cvSetImageROI(src,ROI); //滤波 cvSmooth(src,src,CV_MEDIAN,5,5,0,0);
cvSmooth(src,src,CV_GAUSSIAN,5,5,0,0);
//大津法阈值分割
cvThreshold(src,src,LOW_THRESH,HIGH_THRESH,CV_THRESH_OTSU);
//边缘检测 cvSetImageROI(temp,ROI);
cvCanny(src,temp,LOW_THRESH,HIGH_THRESH,3); cvResetImageROI(src); cvResetImageROI(temp); cvCopy(temp,src,0);
cvSetImageROI(src,ROI);
//hough直线检测
lines=cvHoughLines2(src,storage,CV_HOUGH_PROBABILISTIC,0.5,CV_PI
180,5,10,5); //绘制检测到的直线 int i;
cvSetImageROI(out,ROI); for (i = 0;i < lines->total;i++) {
CvPoint *endpoints = (CvPoint *)cvGetSeqElem(lines,i);
31
/
长春理工大学本科毕业论文
}
cvLine(out,endpoints[0],endpoints[1],RED,2,8,0);
//边缘提取 int sum_of_area = 0;
sum_of_area=cvFindContours(src,storage,&contours,sizeof(CvContour),CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0));
for (i = 0;i < sum_of_area;contours = contours->h_next,i++) { if (contours->total <= 50) continue;
if (is_ellipse(contours)) {
puts(\ cvDrawContours(out,contours,BLUE,GREEN,LOW_THRESH,2,8,cvPoint(0,0));
}
}
cvResetImageROI(out);
cvRectangle(out,cvPoint(0,(int)out->height 3),cvPoint(out->width,out->height),GREEN,2,8,0);
cvNamedWindow(\ cvNamedWindow(\ cvShowImage(\ cvShowImage(\ cvWaitKey(0); return 0;
}
Line *get_tangent(CvSeq *seq,int n) { Line *result = (Line *)malloc(sizeof(Line)); if (n >= seq->total || n < 4)
return (Line *)NULL;
32
/

