基于Opencv车道线检测

  1. 二值化:先变化为灰度图,然后设定阈值直接变成二值化图像。
  2. 形态学滤波:对二值化图像进行腐蚀,去除噪点,然后对图像进行膨胀,弥补对车道线的腐蚀。
  3. ROI提取:提取感兴趣的ROI
  4. 边缘线检测:canny变化、sobel变化和laplacian变化中选择了效果比较好的canny变化,三者在代码中均可以使用,canny变化效果稍微好一点。
  5. 直线检测:Hough直线检测
   
#include
#include
#include
#include
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include   
#include   
#include 
#include 
#include 

using namespace std;
using namespace cv;
using namespace face;

int main()
{
	VideoCapture capture;
	Mat frame,frame_gray;
	Mat edges;

	vector lines;
	Point point1, point2;

	bool stop = false;

	VideoCapture cap("D:\\pics\\video_1.mp4");    //打开默认摄像头  
	if (!cap.isOpened())
	{
		return -1;
	}

	//获得帧宽度和高度
	int w = static_cast(cap.get(CV_CAP_PROP_FRAME_WIDTH));
	int h = static_cast(cap.get(CV_CAP_PROP_FRAME_HEIGHT));
	//获得帧率
	double r = cap.get(CV_CAP_PROP_FPS);
	cout << "帧宽:" << w << "\n帧高:" << h << "\n速率" << r;
	//frame = imread("D:\\pics\\line2.jpg");

	while (!stop)
	{

	cap >> frame;

	if (frame.empty()) break;

	// 矩阵转置
	//transpose(frame, frame);
	//0: 沿X轴翻转; >0: 沿Y轴翻转; <0: 沿X轴和Y轴翻转
	//flip(frame, frame, 1);
	
	//【1】进行灰度图转换和Canny线检测
	cvtColor(frame, frame_gray, CV_BGR2GRAY);
    //imshow("灰度图", frame_gray);
	
	GaussianBlur(frame_gray,frame_gray,Size(5,5),0,0);
	//imshow("Gauss图", frame_gray);

	Canny(frame_gray, edges, 50, 150);
	imshow("canny线检测图",edges);
 	threshold(edges, edges, 128, 255, THRESH_BINARY);


	//【2】提取ROI
	//初始化掩模矩阵
	/***
	Rect rect;
	rect.x = 100;
	rect.y = 190;
	rect.width = 150;
	rect.height = 90;

	//设置矩形掩模
	Mat mask = Mat::zeros(edges.size(), CV_8UC1);

	mask(rect).setTo(255);

	Mat img2;
	edges.copyTo(img2, mask);
	imshow("mask", mask);
	imshow("img2", img2);
	***/
	/** 创建一些点 */

	Mat mask = Mat::zeros(edges.size(), CV_8UC1);

	Point rook_points[1][4];
	rook_points[0][0] = Point(0, edges.rows);
	rook_points[0][1] = Point(460,325);
	rook_points[0][2] = Point(520, 325);
	rook_points[0][3] = Point(edges.cols, edges.rows);

	const Point* ppt[1] = { rook_points[0] };
	int npt[] = { 4 };

	fillPoly(mask,
		ppt,
		npt,
		1,
		Scalar(255, 255, 255),8);

	imshow("mask", mask);

	Mat dstImg;
	edges.copyTo(dstImg, mask);

	imshow("dstImg",dstImg);
 
	//【3】进行霍夫线变换
	vector lines;//定义一个矢量结构lines用于存放得到的线段矢量集合
	HoughLinesP(dstImg, lines, 1, CV_PI / 180, 15, 15, 100);


	vector left_points_b;
	vector left_points_e;
	vector right_points_b;
	vector right_points_e;
   

	//【4】依次在图中绘制出每条线段
	for (size_t i = 0; i <10; i++)
	{
		Vec4i l = lines[i];
		double x1 = l[0];
		double y1 = l[1];
		double x2 = l[2];
		double y2 = l[3];
		double slope = (y2 - y1) / (x2 - x1);
		if (slope>0.2)
			line(frame, Point(x1,y1), Point(x2, y2), Scalar(0, 0, 255), 3, CV_AA);
			left_points_b.push_back(Point(x1, y1));
			left_points_b.push_back(Point(x2, y2));
			
		if (slope < -0.2)
			line(frame, Point(x1, y1), Point(x2, y2), Scalar(0, 255,0), 3, CV_AA);
			right_points_b.push_back(Point(x1, y1));
			right_points_b.push_back(Point(x2, y2));
	}
	cout << endl << "left_point" << left_points_b;

	cout << endl << "right_point" << right_points_b;
	

	imshow("detected lines [CPU]", frame);

	if (waitKey(10) >= 0)
	   stop = true;
	}

	return 0;


}


/***
Mat roi_mask(Mat img, Mat vertices) 
{

	Rect rect;
	rect.x = 300;
	rect.y = 300;
	rect.width = 500;
	rect.height = 200;


	//初始化掩模矩阵
	Mat mask = Mat::zeros(img.size(), CV_8UC1);
	    
	fillPoly(mask,vertices,255);

	Mat masked_img = bitwise_and(img, mask);
	return masked_img

}
****/

基于Opencv车道线检测_第1张图片
 
  

你可能感兴趣的:(基于Opencv车道线检测)