车道线检测算法经典编程

车道线检测算法经典编程

  1. 车道线曲线拟合算法编程

计算经过(50,50),(90,120),(70,200)三点的Catmull_Rom样条曲线。

车道线检测算法经典编程_第1张图片

  1. IplImage* img = cvCreateImage(cvSize(300,300), 8, 1);

  2. for (int i = 0; i < img->height; ++i)

  3. {

  4.    for (int j = 0; j < img->width; ++j)  
    
  5.    {  
    
  6.        ((char *)(img->imageData + img->widthStep * (i)))[j] = 0;  
    
  7.    }  
    
  8. }

  9. CvPoint point0,point1,point2,point3,point4;//3个控制点来做

  10. point1.x = 50;

  11. point1.y = 50;

  12. point2.x = 90;

  13. point2.y = 120;

  14. point3.x = 70;

  15. point3.y = 200;

  16. point0.x = point1.x+(point1.x-point2.x);

  17. point0.y = point1.y+(point1.y-point2.y);

  18. point4.x = point3.x+(point3.x-point2.x);

  19. point4.y = point3.y+(point3.y-point2.y);

  20. ((char *)(img->imageData + img->widthStep * (point1.y)))[point1.x] = 255;

  21. ((char *)(img->imageData + img->widthStep * (point2.y)))[point2.x] = 255;

  22. ((char *)(img->imageData + img->widthStep * (point3.y)))[point3.x] = 255;

  23. for (int i = 1; i < 500 ; i++) {

  24.          float t = (float) i * (1.0f / (float) 500);  
    
  25.          float tt = t * t;  
    
  26.          float ttt = tt * t;  
    
  27.       CvPoint pi;  
    
  28.       pi.x = 0.5 * (2*point1.x+(point2.x-point0.x)*t + (2*point0.x-5*point1.x+4*point2.x-point3.x)*tt + (3*point1.x-point0.x-3*point2.x+point3.x)*ttt);  
    
  29.          pi.y = 0.5 * (2*point1.y+(point2.y-point0.y)*t + (2*point0.y-5*point1.y+4*point2.y-point3.y)*tt + (3*point1.y-point0.y-3*point2.y+point3.y)*ttt);  
    
  30.       ((char *)(img->imageData + img->widthStep * (pi.y)))[pi.x] = 255;  
    
  31. }

  32. for (int i = 1; i < 500 ; i++) {

  33.          float t = (float) i * (1.0f / (float) 500);  
    
  34.          float tt = t * t;  
    
  35.          float ttt = tt * t;  
    
  36.       CvPoint pi;  
    
  37.       pi.x = 0.5 * (2*point2.x+(point3.x-point1.x)*t + (2*point1.x-5*point2.x+4*point3.x-point4.x)*tt + (3*point2.x-point1.x-3*point3.x+point4.x)*ttt);  
    
  38.          pi.y = 0.5 * (2*point2.y+(point3.y-point1.y)*t + (2*point1.y-5*point2.y+4*point3.y-point4.y)*tt + (3*point2.y-point1.y-3*point3.y+point4.y)*ttt);  
    
  39.       ((char *)(img->imageData + img->widthStep * (pi.y)))[pi.x] = 255;  
    
  40. }

  41. cvShowImage(“scr”, img);

  42. cvWaitKey(0);

  43. return 0;

  44. 检测出Hough空间的曲线,规定如下搜索方法:

霍夫空间中相交的曲线越多,交点表示的线在笛卡尔坐标系对应的点越多。我们在霍夫空间中定义交点的最小阈值来检测线。霍夫变换跟踪了帧中的每个点的霍夫空间交点。如果交点数量超过了阈值就确定一条对应参数 θ 和 d的线。
车道线检测算法经典编程_第2张图片

1>起始搜索点为A点

2>向右搜索,直到退出,退出点为B

3>从B点向左搜索,直到退出

4>起始搜索点回到A点

5>向左搜索,直到退出,退出点为C

6>从C点向右搜索,直到退出

结束

车道线检测算法经典编程_第3张图片

部分代码如下:

向左搜索:

  1. CvPoint searchRight(int HoughArea, IplImage hough, CvPoint point, Lines line)

  2. {//找当前点的上、右上、右、右下、下

  3.    //循环搜索,直到两个方向搜索均无结果,则退出开始下一步判断  
    
  4.    CvPoint start;//搜索的初始点  
    
  5.    start = point;//先将point作为当前的初始点  
    
  6.    int Max = 0;//找局部最大值  
    
  7.    CvPoint last = start;//记录上一次最大值,本次搜索排除当前点和上一次点  
    
  8.    int mistakes = 0;//引入一个容错因子  
    
  9.    //****************************搜索上,右上,右,右下,下*****************************8  
    
  10.   while(1)  
    
  11.   {  
    
  12.       CvPoint temp;  
    
  13.       if(start.x >= hough->width-1 || start.y <= 0||start.y >= hough->height-1)//超出搜索范围,退出搜索  
    
  14.           break;  
    
  15.       //cout << "curve1" << endl;  
    
  16.       for(int xx = 0;xx < 2; xx++)  
    
  17.       {  
    
  18.           for(int yy = -1;yy < 2;yy++)  
    
  19.           {  
    
  20.               if((xx == 0 && yy == 0)||(start.y + yy == last.y && start.x + xx == last.x))//不包括当前点  
    
  21.                   continue;  
    
  22.               //cout << HoughArea[start.y + yy][start.x + xx] << endl;  
    
  23.               if(HoughArea[start.y + yy][start.x + xx] > Max)  
    
  24.               {  
    
  25.                   Max = HoughArea[start.y + yy][start.x + xx];  
    
  26.                   temp.x = start.x + xx;  
    
  27.                   temp.y = start.y + yy;  
    
  28.               }   
    
  29.           }  
    
  30.       }  
    
  31.       if(Max > Threshold)  
    
  32.       {  
    
  33.           //cout << Max << "  ";  
    
  34.           mistakes = 0;//容错清零  
    
  35.           cout << "find1:"<< Max << endl;  
    
  36.           Max = 0;  
    
  37.           (*line).houghpoint.push_back(temp);  
    
  38.           last = start;//保存上一次最大值  
    
  39.           start = temp;//找到的点作为当前点继续搜索  
    
  40.           CvScalar s;      
    
  41.           s.val[0]=0;  
    
  42.           s.val[1]=255;  
    
  43.           s.val[2]=0;  
    
  44.           //cvSet2D(hough,temp.y,temp.x,s);//set the (i,j) pixel value  
    
  45.       }  
    
  46.       else if(mistakes <= 5)//容错,三次  
    
  47.       {  
    
  48.           mistakes++;  
    
  49.           Max = 0;  
    
  50.           start.x = start.x + 1;  
    
  51.       }  
    
  52.       else//退出条件  
    
  53.           break;//没有找到符合条件的点,退出搜索  
    
  54.   }  
    
  55.   //*******************************************************************************  
    
  56.   CvPoint final;  
    
  57.   final.x = start.x - mistakes;  
    
  58.   final.y = start.y;  
    
  59.   return final;  
    
  60. }

  61. 导入包含的库文件

#include

#include

#include

#include

#include

using namespace
std;

using namespace cv;

  1. 读取图片

    //reading
    image
    *****

    Mat image;

    image = imread("/home/project1/test1.jpg");

    if(image.empty()){

     cout <<"reading error"<

}

  1. 灰度变换

//gray image****

Mat image_gray;

cvtColor(image,image_gray,
CV_BGR2GRAY);

  1. canny边缘检测
    车道线检测算法经典编程_第4张图片

//canny***

Mat image_canny;

Canny(image_gau,
image_canny,100, 200, 3);

你可能感兴趣的:(目标检测,目标检测数据集,车道线检测)