博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用opencv3 检测车道线
阅读量:2394 次
发布时间:2019-05-10

本文共 4134 字,大约阅读时间需要 13 分钟。

#include 
#include
#include "opencv2/core/core.hpp"#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/highgui/highgui.hpp"using namespace std;using namespace cv;int main(int argc, char **argv) {
Mat img, gray, blur_img, edges, mask, masked_edges; img = imread("img.jpg",-1); if(img.empty()) return -1; cvtColor(img, gray, cv::COLOR_BGR2GRAY); GaussianBlur(gray, blur_img, Size(5,5), 0); Canny(blur_img, edges, 100, 250); //ROI mask = Mat::zeros(img.size(),CV_8UC1); vector
> contour; vector
pts; pts.push_back(Point(0,img.rows)); pts.push_back(Point(img.cols/2.0-20,img.rows*0.6)); pts.push_back(Point(img.cols/2.0+20,img.rows*0.6)); pts.push_back(Point(img.cols,img.rows)); contour.push_back(pts); drawContours(mask,contour,0,Scalar::all(255),-1); edges.copyTo(masked_edges, mask); vector
lines, left_lines, right_lines; // Vec4i -> lines[i][0-3] HoughLinesP(masked_edges, lines, 2, CV_PI/180, 30, 10, 30); //lines' coordinate vector
left_slope, right_slope; vector
left_centers, right_centers; //Point2d -> left_centers[i].x list
list_lines; double slo; Point2d center; int i = 0; for(auto v:lines) // vector -> list list_lines.push_back(v); for(auto iter = list_lines.begin(); iter != list_lines.end(); i++) //list to erase { slo = double((lines[i][3]-lines[i][1]))/double((lines[i][2]-lines[i][0])) ; center = Point( double(lines[i][2]+lines[i][0])/2 , double(lines[i][3]+lines[i][1])/2 ); if(fabs(slo)<0.35) { iter = list_lines.erase(iter); continue; } if(slo > 0) { right_lines.push_back(lines[i]); right_centers.push_back(center); right_slope.push_back(slo); } else { left_lines.push_back(lines[i]); left_centers.push_back(center); left_slope.push_back(slo); } *iter = lines[i]; iter++; } //left to mean slope and center double left_len, total_left_len=0, total_left_slope=0; Point2d total_left_center(0,0); for(int i = 0; i< left_lines.size(); i++) { left_len = sqrt( (left_lines[i][2]-left_lines[i][0])*(left_lines[i][2]-left_lines[i][0]) + (left_lines[i][3]-left_lines[i][1])*(left_lines[i][3]-left_lines[i][1]) ); total_left_slope = total_left_slope + left_len * left_slope[i]; total_left_len = total_left_len + left_len; total_left_center = Point(total_left_center.x + left_len * left_centers[i].x , total_left_center.y + left_len * left_centers[i].y ); } double mean_left_slope; Point2d mean_left_center; mean_left_slope = total_left_slope/ total_left_len; mean_left_center = Point( total_left_center.x/total_left_len, total_left_center.y/total_left_len); //right to mean slope and center double right_len, total_right_len=0, total_right_slope=0; Point2d total_right_center(0,0); for(int i = 0; i< right_lines.size(); i++) { right_len = sqrt( (right_lines[i][2]-right_lines[i][0])*(right_lines[i][2]-right_lines[i][0]) + (right_lines[i][3]-right_lines[i][1])*(right_lines[i][3]-right_lines[i][1]) ); total_right_slope = total_right_slope + right_len * right_slope[i]; total_right_len = total_right_len + right_len; total_right_center = Point(total_right_center.x + right_len * right_centers[i].x , total_right_center.y + right_len * right_centers[i].y ); } double mean_right_slope; Point2d mean_right_center; mean_right_slope = total_right_slope/ total_right_len; mean_right_center = Point( total_right_center.x/total_right_len, total_right_center.y/total_right_len); double start_y = img.rows * 0.6; double end_y = img.rows; double left_start_x = (start_y - mean_left_center.y)/mean_left_slope + mean_left_center.x; double left_end_x = (end_y - mean_left_center.y)/mean_left_slope + mean_left_center.x; double right_start_x = (start_y - mean_right_center.y)/mean_right_slope + mean_right_center.x; double right_end_x = (end_y - mean_right_center.y)/mean_right_slope + mean_right_center.x; line(img, Point(left_start_x, start_y), Point(left_end_x, end_y), Scalar(0,0,255), 3, 8); line(img, Point(right_start_x, start_y), Point(right_end_x, end_y), Scalar(0,0,255), 3, 8); // vector
pro_lines; //list(after erase) -> vector// for(auto v:list_lines)// pro_lines.push_back(v); // for(int i=0; i

在这里插入图片描述

转载地址:http://cuwob.baihongyu.com/

你可能感兴趣的文章
JSP技术的学习总结
查看>>
JavaBean的初步认知
查看>>
重识java反射
查看>>
Spring的核心中IOC、DI
查看>>
Spring中注解的使用
查看>>
Spring的认识
查看>>
maven项目出现如下错误,求指点;CoreException: Could not calculate build plan:
查看>>
理解Paxos算法的证明过程
查看>>
详解 JVM Garbage First(G1) 垃圾收集器
查看>>
Java 8 函数式编程入门之Lambda
查看>>
用高阶函数轻松实现Java对象的深度遍历
查看>>
WindowsApi+Easyx图形库的透明时钟
查看>>
Eclipse LUNA配置TomCat(非j2ee版本)
查看>>
树莓派安装mysql-srver报错 404 not found!
查看>>
Ubuntu 14.04LTS 下安装.net框架
查看>>
Eclipse 配置Groovy语言环境 && Java工程运行Groovy
查看>>
人工智能术语表
查看>>
Tensorflow Python API 翻译(sparse_ops)
查看>>
Tensorflow Python API 翻译(math_ops)(第一部分)
查看>>
Tensorflow Python API 翻译(math_ops)(第二部分)
查看>>