使用opencv实现车道线检测实战代码
短信预约 -IT技能 免费直播动态提醒
效果
void lane_detection(cv::Mat &class="lazy" data-src, cv::Mat &dst)
{
dst = cv::Mat::zeros(class="lazy" data-src.size(),class="lazy" data-src.type());
cv::Mat grid =cv::Mat::zeros(class="lazy" data-src.size(),class="lazy" data-src.type());
int iStep = 25;
int iNUmsX = class="lazy" data-src.cols / iStep;
int inUmsY = class="lazy" data-src.rows / iStep;
for(int i = 1; i <= inUmsY; i++)
{
int yPos = i * iStep + class="lazy" data-src.cols / 5;
cv::Point2d pt1,pt2;
int iOffset = 10;
pt1.x = 0 + iOffset;
pt1.y = yPos;
pt2.x = class="lazy" data-src.cols - iOffset;
pt2.y = yPos;
cv::line(grid,pt1,pt2,cv::Scalar(255), 1, cv::LINE_4);
}
for(int i = 1; i <= iNUmsX; i++)
int xPos = i * iStep;
pt1.x = xPos;
pt1.y = 0 + iOffset + class="lazy" data-src.rows / 5;
pt2.x = xPos;
pt2.y = class="lazy" data-src.rows - iOffset;
cv::imshow("grid", grid);
cv::Mat bitNot;
cv::bitwise_and(class="lazy" data-src, grid, bitNot);
cv::Mat add = cv::Mat::zeros(bitNot.rows, bitNot.cols,bitNot.type());
int iDiffTh = 200;
QTime timer;
timer.start();
//#pragma omp parallel for num_threads(10)
for (int i = 1; i < bitNot.rows - 1; i++)
{
for (int j = 1; j < bitNot.cols - 1; j++)
{
int iValueX = (int)bitNot.at<uchar>(i, j);
int iValueXPre = (int)bitNot.at<uchar>(i-1, j);
int iValueXNext = (int)bitNot.at<uchar>(i+1, j);
int iValueY = (int)bitNot.at<uchar>(i, j);
int iValueYPre = (int)bitNot.at<uchar>(i, j-1);
int iValueYNext = (int)bitNot.at<uchar>(i, j+1);
if((iValueX - iValueXPre > iDiffTh && iValueX - iValueXNext > iDiffTh) ||
(iValueY - iValueYPre > iDiffTh && iValueY - iValueYNext > iDiffTh))
{
add.at<uchar>(i, j) = 255;
}
}
}
qDebug()<<"process time: "<<timer.elapsed()<<" ms";
//形态学预处理
cv::Mat matDilate;
cv::Mat k33 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(9, 9), cv::Point(-1, -1));
cv::morphologyEx(add, matDilate, cv::MORPH_DILATE, k33, cv::Point(-1, -1), 3);
cv::imshow("matDilate", matDilate);
//cv::bitwise_not(class="lazy" data-src, matDilate, matRoi);
cv::Mat matRoi;
cv::bitwise_and(class="lazy" data-src, matDilate, matRoi);
cv::imshow("matRoi", matRoi);
cv::Mat matThresh;
cv::threshold(matRoi, matThresh, 200, 255,cv::THRESH_BINARY);
cv::imshow("matThresh", matThresh);
//std::vector<std::vector<cv::Point>> contours;
//cv::findContours(matThresh,contours,)
std::vector<std::vector<cv::Point> > contoursDefect;
std::vector<cv::Vec4i> hierarchyDefect;
cv::Mat canves;
cv::cvtColor(class="lazy" data-src, canves,cv::COLOR_RGBA2RGB);
cv::findContours(matThresh, contoursDefect, hierarchyDefect, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
for (size_t i = 0; i < contoursDefect.size(); i++)
{
cv::Mat contour(contoursDefect.at(i));//第i个轮廓
double area = contourArea(contour);
if (area >= 50)
cv::Moments moment;//矩
moment = moments(contour, false);
cv::Point2d pt1;
double m00 = moment.m00 + 0.01;
pt1.x = moment.m10 / m00;//计算重心横坐标
pt1.y = moment.m01 / m00;//计算重心纵坐标
cv::drawContours(canves, contoursDefect, i, cv::Scalar(255, 255, 0), -1);
}
cv::imshow("canves", canves);
cv::waitKey(0);
}
void test_lane_detection()
int i = 0;
while(1)
cv::Mat class="lazy" data-src;
QString dir("D:\\QtProject\\Opencv_Example\\gen_grid_region\\scene_");
QString path;
if(i>9) path = QString("%1%2%3").arg(dir).arg(i++).arg(".png");
else path = QString("%1%2%3%4").arg(dir).arg("0").arg(i++).arg(".png");
cout<<path.toStdString();
class="lazy" data-src = cv::imread(path.toStdString(), cv::IMREAD_GRAYSCALE);
if (class="lazy" data-src.empty()) {
cout << "Cannot load image" << endl;
return;
}
cv::imshow("class="lazy" data-src", class="lazy" data-src);
cv::Mat dst;
lane_detection(class="lazy" data-src, dst);
到此这篇关于opencv车道线检测实战的文章就介绍到这了,更多相关opencv车道线检测内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341