基于图像的车道偏离预警,基于openCV

来源:互联网 发布:工业英语电子词典软件 编辑:程序博客网 时间:2024/05/09 23:35

前言:

        算法自己写,只用到openCV的IO函数等。之前的毕业设计,现在很闲,拿出来改改,发出来。

目标:

        检测出视频中的车道线

思路:

       1.摄像机拍摄的视频有透视投影变换,所以先进行反透视变换。因为车道线是平行的,两条车道线之间的距离和平行信息是很重要的鲁棒性信息

       2.道路是曲线的,要将图像分成多个片段,每个片段内近似认为是直线,最后将直线拟合成曲线

       3.使用Hough变换进行直线检测,同时建立感兴趣区域(上帧图片中车道线附近的区域)以提高算法的性能。

       4.使用kalman滤波进行车道线预测

       5.由于没有硬件,全部使用电脑模拟,开了3个线程用来进行图像预处理,开了1个线程用来检测车道线,大约1s能处理16帧。

       6.图像需要预处理,这部分不写了

效果:

      原图:


反透视:


先腐蚀,后膨胀:


边缘检测:


感兴趣区域,hough变换,kalman滤波:




int lineHough(cv::Mat &I,HoughLine &lines,InterestPoint &int_point,double theta_min,double theta_max,double theta_step,double p_step){std::vector<double> rho,theta;//used to generate the gridsstd::vector<int> votes_of_chosen_line;//save the votes of each linecv::Mat vote;//used to save the votesunsigned int i=0,j=0;//used to index the hough lines int row=0,col=0;//used to index the interest areaint half_size_of_rho=0;//value of half size of rho arrayint rho_index=0;//used to index rho arrayint max_vote=0;//used to save the maximum value of votedouble rho_max=0,rho_min=0,rho_tmp=0,theta_tmp=0.0;//used to generate rho and theta griddouble cos_tmp=0.0,sin_tmp=0.0;//sin and cos valueVec2d point_tmp;// tmp linebool l_line_repeat=false;//if the line is repeat int thresh=0;//used to filter the hough peaks//check if the input image is valid, only 8 depth and 1 channels image are okassert(I.depth()==CV_8U);assert(I.channels()==1);//generate the grids of theta and rhotheta_tmp=theta_min;do{theta.push_back(theta_tmp);theta_tmp+=theta_step;}while(theta_tmp<theta_max);rho_max=(int)(1.0*sqrt(pow(I.rows,2.0)+pow(I.cols,2.0))+0.5);rho_min=-rho_max;rho_tmp=rho_min;do{rho.push_back(rho_tmp);rho_tmp+=p_step;} while (rho_tmp<rho_max);//cout<<"theta and rho grids are generated."<<endl;half_size_of_rho=(int)(rho.size()/2);//create the vote matrixvote=cv::Mat::zeros(rho.size(),theta.size(),CV_32SC1);//generate the hough peaksmax_vote=0;for (i=0;i<theta.size();++i){cos_tmp=cos(theta.at(i));sin_tmp=sin(theta.at(i));for (j=0;j<int_point.size();++j){rho_index=(int)((int_point.at(j)[1]*cos_tmp+int_point.at(j)[0]*sin_tmp)/p_step+half_size_of_rho);if((rho_index>=0)&&(rho_index<vote.rows)){vote.at<int>(rho_index,i)+=1;//get the maximum voteif(vote.at<int>(rho_index,i)>max_vote){max_vote=vote.at<int>(rho_index,i);}}}}//filter the peaks by threshthresh=(int)(VOTE_RATIO*max_vote);thresh=thresh>VOTE_MIN?thresh:VOTE_MIN;for (row=0;row<vote.rows;++row){for (col=0;col<vote.cols;++col){if(vote.at<int>(row,col)>thresh){//cout<<"votes at (rho="<<rho.at(row)<<",theta="<<theta.at(col)<<"):"<<vote.at<int>(row,col)<<"row="<<row<<",col="<<col<<"\n";point_tmp[0]=rho.at(row);point_tmp[1]=theta.at(col);lines.push_back(point_tmp);}}}//cout<<"hough lines detection done.\n";return 0;}


1 0
原创粉丝点击