车道线检测+车道线跟踪+车道线识别【持续更新】

时间:2022-06-15 15:16:56

原本打算用深度学习的,但是....各种原因。于是想着随便先实现以一下,发现效果还不错,还节约成本。美滋滋


现成代码美滋滋,先上个链接

http://blog.csdn.net/chongshangyunxiao321/article/details/50999212

再上图

车道线检测+车道线跟踪+车道线识别【持续更新】


用上面博客的代码,再改改参数,美滋滋。

但是会出现丢帧的情况。这对追求完美的我。。。。。。。。。的老板来说不行啊。


于是想用深度学习做一做。

怎么做?表急,一步一步来。。。。。

先看一下逆透视变换。这里是用的opencv的四点变换。

车道线检测+车道线跟踪+车道线识别【持续更新】

车道线检测+车道线跟踪+车道线识别【持续更新】

先上个链接,链接关了。直接上代码吧。

#include <cv.h>
#include <highgui.h>
using namespace std;
int main()
{
CvPoint2D32f srcQuad[4], dstQuad[4];
CvMat *warp_matrix = cvCreateMat(3, 3, CV_32FC1);
IplImage *src, *dst;
if (((src = cvLoadImage("D:\\路径\\3.png", 1)) != 0))
{
dst = cvCloneImage(src);
dst->origin = src->origin;//确定起点位置为座顶角
cvZero(dst);


srcQuad[0].x = 0;
srcQuad[0].y = 0;
srcQuad[1].x = src->width - 1.;
srcQuad[1].y = 0;
srcQuad[2].x = 0;
srcQuad[2].y = src->height - 1;
srcQuad[3].x = src->width - 1;
srcQuad[3].y = src->height - 1;


//dstQuad[0].x = src->width*0.05;
//dstQuad[0].y = src->height*0.33;
dstQuad[0].x = srcQuad[0].x;
dstQuad[0].y = srcQuad[0].y;
dstQuad[1].x = src->width;
dstQuad[1].y = 0;
dstQuad[2].x = src->width*0.45;
dstQuad[2].y = src->height;
dstQuad[3].x = src->width*0.55;
dstQuad[3].y = src->height;


//计算透视映射矩阵
cvGetPerspectiveTransform(srcQuad, dstQuad, warp_matrix);
//密集透视变换
cvWarpPerspective(src, dst, warp_matrix);
cvNamedWindow("Perspective_Warp", 1);
cvShowImage("Perspective_Warp", dst);
cvSaveImage("result3.png", dst);
cvWaitKey();
}
cvReleaseImage(&dst);
cvReleaseMat(&warp_matrix);
return 0;
}

//华丽丽的分割线,上面是车道线检测,下面是车道线跟踪

车道线检测+车道线跟踪+车道线识别【持续更新】

为什么要做跟踪,直接从上图中可以看出在车道检测(绿色)算法没有很好的检测出正确的车道线的时候,车道线跟踪(红色)能够很好的规避这种错误,达到理想效果。

咱这里用的kalman,我的代码太乱了,不好意思贴了。哇咔咔。这里贴一个kalman的鼠标跟踪代码。

#include <opencv/cv.h>  
#include <opencv/highgui.h>

#include <iostream>

using namespace cv;
using namespace std;

const int winWidth = 800;
const int winHeight = 600;

Point mousePosition = Point(winWidth>>1, winHeight>>1);

//mouse call back
void mouseEvent(int event, int x, int y, int flags, void *param)
{
if(event==CV_EVENT_MOUSEMOVE)
{
mousePosition=Point(x,y);
}
}

int main()
{
//1.kalman filter setup
const int stateNum=4;
const int measureNum=2;

KalmanFilter KF(stateNum, measureNum, 0);
Mat state (stateNum, 1, CV_32FC1); //state(x,y,detaX,detaY)
Mat processNoise(stateNum, 1, CV_32F);
Mat measurement = Mat::zeros(measureNum, 1, CV_32F); //measurement(x,y)


randn( state, Scalar::all(0), Scalar::all(0.1) ); //随机生成一个矩阵,期望是0,标准差为0.1;
KF.transitionMatrix = *(Mat_<float>(4, 4) <<
1,0,1,0,
0,1,0,1,
0,0,1,0,
0,0,0,1 );//元素导入矩阵,按行;

//setIdentity: 缩放的单位对角矩阵;
//!< measurement matrix (H) 观测模型
setIdentity(KF.measurementMatrix);

//!< process noise covariance matrix (Q)
// wk 是过程噪声,并假定其符合均值为零,协方差矩阵为Qk(Q)的多元正态分布;
setIdentity(KF.processNoiseCov, Scalar::all(1e-5));

//!< measurement noise covariance matrix (R)
//vk 是观测噪声,其均值为零,协方差矩阵为Rk,且服从正态分布;
setIdentity(KF.measurementNoiseCov, Scalar::all(1e-1));

//!< priori error estimate covariance matrix (P'(k)): P'(k)=A*P(k-1)*At + Q)*/ A代表F: transitionMatrix
//预测估计协方差矩阵;
setIdentity(KF.errorCovPost, Scalar::all(1));


//!< corrected state (x(k)): x(k)=x'(k)+K(k)*(z(k)-H*x'(k))
//initialize post state of kalman filter at random
randn(KF.statePost, Scalar::all(0), Scalar::all(0.1));
Mat showImg(winWidth, winHeight,CV_8UC3);

for(;;)
{
setMouseCallback("Kalman", mouseEvent);
showImg.setTo(0);

Point statePt = Point( (int)KF.statePost.at<float>(0), (int)KF.statePost.at<float>(1));

//2.kalman prediction
Mat prediction = KF.predict();
Point predictPt = Point( (int)prediction.at<float>(0), (int)prediction.at<float>(1));

//3.update measurement
measurement.at<float>(0)= (float)mousePosition.x;
measurement.at<float>(1) = (float)mousePosition.y;

//4.update
KF.correct(measurement);

//randn( processNoise, Scalar(0), Scalar::all(sqrt(KF.processNoiseCov.at<float>(0, 0))));
//state = KF.transitionMatrix*state + processNoise;
//draw
circle(showImg, statePt, 5, CV_RGB(255,0,0),1);//former point
circle(showImg, predictPt, 5, CV_RGB(0,255,0),1);//predict point
circle(showImg, mousePosition, 5, CV_RGB(0,0,255),1);//ture point


// CvFont font;//字体
// cvInitFont(&font, CV_FONT_HERSHEY_SCRIPT_COMPLEX, 0.5f, 0.5f, 0, 1, 8);
putText(showImg, "Red: Former Point", cvPoint(10,30), FONT_HERSHEY_SIMPLEX, 1 ,Scalar :: all(255));
putText(showImg, "Green: Predict Point", cvPoint(10,60), FONT_HERSHEY_SIMPLEX, 1 ,Scalar :: all(255));
putText(showImg, "Blue: Ture Point", cvPoint(10,90), FONT_HERSHEY_SIMPLEX, 1 ,Scalar :: all(255));

imshow( "Kalman", showImg );
int key = waitKey(3);
if (key == 27)
{
break;
}
}
}
车道线检测+车道线跟踪+车道线识别【持续更新】



第三部分 车道线的识别。

车道线检测+车道线跟踪+车道线识别【持续更新】

随便弄个分类器做做分类,等有闲暇时间上文字描述。