基于opencv计算机视觉的车道检测

  • x0_565600
    了解作者
  • 6.1KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-17 05:00
    上传日期
能够实现简单的车道检测
laneDetection.rar
  • laneDetection
  • lane.cpp
    15KB
  • utils.h
    1.4KB
  • utils.cpp
    2KB
内容介绍
/************************************************************************* > File Name: lane.cpp > Author: onerhao > Mail: haodu@hustunique.com > Created Time: Thu 07 Mar 2013 02:44:22 PM CST ************************************************************************/ #include <iostream> #include <algorithm rel='nofollow' onclick='return false;'> #include <cv.h> #include "utils.h" #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/core/core_c.h> #include <stdlib.h> #include <stdio.h> class Vehicle { CvPoint bmin,bmax; int symmetryX; bool valid; unsigned int lastUpdata; }; enum { LINE_REJECT_DEGREES=60, //in degrees CANNY_MIN_THRESHOLD=1,//edge detector mininum hysteresis threshold CANNY_MAX_THRESHOLD=100,//edge detector maximum hysteresis threshold HOUGH_THRESHOLD=30, //line approval vote threshold HOUGH_MIN_LINE_LENGTH=50, //remove lines shorter than this threshold HOUGH_MAX_LINE_GAP=100, //join lines LINE_LENGTH_DIFF=10, //accepted diffenrence of length of lines, LANE_DYNAMPARAMS=2,//lane state vector dimension LANE_MEASUREPARAMS=2,//lane state vector dimension LANE_CONTROLPARAMS=0,//lane state vector dimension VEHICLE_DYNAMPARAMS=2,//vehicle state vector dimension VEHICLE_MEASUREPARAMS=2,//vehicle measurement dimension VEHICLE_CONTROLPARAMS=0,//vehicle control vector MAX_LOST_FRAME=30//maximum number of lost frames }; int nframe=0; bool sort_line_length(Line l0,Line l1) { return l0.length < l1.length; } struct sort_line {//near vertical line bool operator()(Line l0,Line l1) { return (l0.length*l0.length/fabsf(l0.angle-CV_PI/2) > l1.length*l1.length/fabsf(l1.angle-CV_PI/2)); } }sort_line_object; cv::Mat frame,vmeandist,gray,blur,thrs,dil,ero,canny,dst; int VMeanDist(cv::Mat src,cv::Mat &dist) { cv::Size srcSize=src.size(); int rows=srcSize.height,cols=srcSize.width; cv::Mat row(1,cols,CV_8UC1);//store a row dist=cv::Mat(1,rows,CV_8UC1);//the target distribution matrix int i,j,mean; //std::cout<<src.size()<<"size.height "<<srcSize.height<<std::endl; for(i=0;i<rows;i++) {//average row by row row=src.row(i); mean=0; for(j=0;j<row.cols;j++) { mean+=row.data[j]; } mean/=row.cols; //std::cout<<"\nmean= "<<mean<<std::endl; dist.data[i]=mean; } /*for(i=0;i<dist.cols;i++) { std::cout<<i<<","<<(int)dist.data[i]<<";"; }*/ return 0; } int findHorizon(cv::Mat dist) { int key=dist.data[0],i,lsum;//lsum is the local sum. for(i=1;i<dist.cols;i++) { if(dist.data[i]>key && i>1 && key<=10) return i; key=dist.data[i]; } return i; } void filterLines(std::vector<Line> &lines,bool right) { std::vector<int> eraselist; int i,a=-2*right+1; float angle,anglediff; if(!lines.size()) return; for(i=0;i<(signed)lines.size();i++) { angle=lines[i].angle,anglediff=angle-M_PI_2; //reject lines of wrong slope angle if(anglediff*a<0||fabsf(anglediff)*180/CV_PI > LINE_REJECT_DEGREES) { lines.erase(lines.begin()+i); i--; //std::cout<<"near horizon"<<std::endl; continue; } } sort(lines.begin(),lines.end(),sort_line_object); //std::cout<<"size: "<<lines.size()<<std::endl; //sort the lines by degrees near vertical line /* if(lines.size()) { sort(lines.begin(),lines.end(),sort_line_object); angle=lines[0].angle,lengc,th=lines[0].length; for(i=1;i<lines.size();i++) { if(fabsf(angle-lines[i].angle) < LINE_ANGLE_DIFF && fabsf(length-lines[i].length) > LINE_LENGTH_DIFF) { length=lines[i].length; angle=lines[i].angle; } } */ } void processlines(std::vector<cv::Vec4i> lines, cv::InputArray _edges, cv::OutputArray _dst) { std::vector<Line>left,right; cv::Mat dst=_dst.getMat(); unsigned int i; char str[128]; for(i=0;i<lines.size();i++) { cv::Vec4i l=lines[i]; CvPoint p0=cvPoint(l[0],l[1]),p1=cvPoint(l[2],l[3]); //assuming that the vanishing point is close to the image horizontal //center,calculate line parameters in form:y = kx + b; //decide line's side based on its midpoint position int midx=(l[0]+l[2])/2; if(midx<dst.cols/2) { left.push_back(Line(cvPoint(l[0],l[1]), cvPoint(l[2],l[3]))); } else if(midx>dst.cols/2) { right.push_back(Line(cvPoint(l[0],l[1]), cvPoint(l[2],l[3]))); } } for(int i=0;i<(int)left.size();i++) { cv::line(dst,left[i].p0,left[i].p1,CV_RGB(255,0,0),1); //char str[20]; sprintf(str,"%dth line,%f",i,calLength(left[i].p0,left[i].p1)); cv::putText(dst,str,left[i].p0,1,1,1,1); } for(int i=0;i<(int)right.size();i++) { //char str[20]; sprintf(str,"%dth line,%f",i,right[i].angle); cv::putText(dst,str,right[i].p0,1,1,1,1); cv::line(dst,right[i].p0,right[i].p1,CV_RGB(0,255,0),1); if(i==right.size()-1) { //cv::line(dst,right[i].p0,right[i].p1,CV_RGB(23,21,10),5); } } filterLines(left,false); filterLines(right,true); if(left.size() && right.size()) { cv::line(dst,left[left.size()-1].p0,left[left.size()-1].p1,CV_RGB(23,21,10),5); cv::line(dst,right[right.size()-1].p0,right[right.size()-1].p1,CV_RGB(23,21,10),5); } cv::line(dst,cvPoint(dst.cols/2,0),cvPoint(dst.cols/2,dst.rows),CV_RGB(0,0,0),1); //draw selected lanes int x1=dst.cols * 0.55f; int x2=dst.cols; //cv::line(frame,cvPoint(x1,laneR.k.get()*x1+laneR.b.get()), // cvPoint(x2,laneR.k.get()*x2+laneR.b.get()),CV_RGB(255,0,255),3); //return 0; } int wait(int k,int delay) { if (k ==cv::waitKey(delay)) return 1; return 0; } int detectLane(cv::Mat &frame,std::vector<cv::Vec4i>lines) { int element_shape=cv::MORPH_RECT,an=1; int thrs1=0,thrs2=4000; double rho=1,theta=CV_PI/180; cv::Scalar color; int y,i,j; //cv::vector <cv::Vec2f> lines; //cv::vector <cv::Vec4i> lines; VMeanDist(frame,vmeandist); //std::cout<<vmeandist.data<<std::endl; y=findHorizon(vmeandist); for(i=0;i<y;i++) { for(j=0;j<canny.cols;j++) { frame.data[ero.cols*i+j]=0; } } cv::Canny(frame,canny,CANNY_MIN_THRESHOLD,CANNY_MAX_THRESHOLD,3); cv::imshow("canny",canny); cv::HoughLinesP(canny,lines,rho,theta, HOUGH_THRESHOLD,HOUGH_MIN_LINE_LENGTH,HOUGH_MAX_LINE_GAP); //cv::HoughLines(canny,lines,rho,theta,HOUGH_THRESHOLD,3,8); processlines(lines,canny,dst); /*for(i=0;i<lines.size();i++) { cv::line(dst,cvPoint(lines[i][0],lines[i][1]), cvPoint(lines[i][2],lines[i][3]),cv::Scalar(0,255,0),4,8); }*/ //std::cout<<"\n"<<"y: "<<y<<std::endl; cv::line(dst,cvPoint(0,y),cvPoint(750,y),cv::Scalar(0,0,0),1,8,0); return 0; } int detectvehicle(cv::Mat &frame,std::vector<cv::Rect> &rects, std::string cascade_name) {//return the number of detected vehicles cv::CascadeClassifier vehicle(cascade_name); if(vehicle.empty()) { std::cout<<"unable to load the classifier"<<std::endl; return -1; } vehicle.detectMultiScale(frame,rects,1.1, 2,0,cv::Size(80,80)); cv::Rect p; //for(int i=0;i<rects.size();i++) /*for(int i=0;i<rects.size()&&i<1;i++) { p=rects[i]; cv::rectangle(frame,cvPoint(p.x,p.y),cvPoint(p.x+p.width,p.y+p.height), cv::Scalar(0,255,0),1,8,0); }*/ return rects.size(); } int trackline(cv::Mat& frame, std::vector<cv::Vec4i>& l
评论
    相关推荐
    • opencv 计算机视觉 编程攻略
      计算机视觉编程攻略 opencv 结构清晰,介绍详细
    • OpenCV3计算机视觉
      对于计算机视觉的人来说是一本福利,很值得入手此书。
    • opencv计算机视觉——鼠标响应
      1、OpenCV 2 计算机视觉编程手册_中文版(必学 ) 2、OpenCV 2 Computer.Vision.Application.Programming. 3、OpenCV实例精解(必学) 4、学习OpenCV(中文版) 5、基于OpenCV计算机视觉技术实现 6、深入理解OpenCV...
    • 基于OpenCV计算机视觉技术实现
      《基于OpenCV计算机视觉技术实现》介绍了大约200多个典型的技术问题,覆盖了基于OpenCV基础编程的主要内容,利用大量生动有趣的编程案例和编程技巧,从解决问题和答疑解惑入手,以因特网上最新资料为蓝本,深入浅...
    • 基于OpenCV计算机视觉技术实现
      这是包含《基于OpenCV计算机视觉技术实现》的原书电子版(分章节),和工程代码,资料很全,一般的都是只有代码,没有书,我是把两份集合打包分享给大家。希望对你有帮助。
    • opencv3计算机视觉leaningopcvPracticalIntroduction
      A Practical Introduction to Computer Vision with OpenCV,OpenCV 3计算机视觉 第2版,LearningOpenCV3(第3版)经过大量的筛选,找出三本学习opencv技术最佳图书组合。pdf格式,清晰有目录,打包下载。
    • 基于opencv计算机视觉实现
      是基于opencv计算机视觉实现这本书中的所有源代码
    • OpenCV 2 计算机视觉编程手册
      OpenCV 2 计算机视觉编程手册 源代码
    • 基于OpenCV计算机视觉技术实现
      基于OpenCV计算机视觉技术实现,可以学学,很不错哦,基于OpenCV计算机视觉技术实现,可以学学,很不错哦
    • OpenCV计算机视觉常用测试图
      OpenCV计算机视觉的常用测试图,里面包含baboon、man、monarch、sailboat、soccer、lena、tiffany等!里面还有标准测试图