怎么使用opencv实现车道线检测(opencv,开发技术)

时间:2024-05-02 19:10:48 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

效果

怎么使用opencv实现车道线检测

voidlane_detection(cv::Mat&src,cv::Mat&dst){dst=cv::Mat::zeros(src.size(),src.type());cv::Matgrid=cv::Mat::zeros(src.size(),src.type());intiStep=25;intiNUmsX=src.cols/iStep;intinUmsY=src.rows/iStep;for(inti=1;i<=inUmsY;i++){intyPos=i*iStep+src.cols/5;cv::Point2dpt1,pt2;intiOffset=10;pt1.x=0+iOffset;pt1.y=yPos;pt2.x=src.cols-iOffset;pt2.y=yPos;cv::line(grid,pt1,pt2,cv::Scalar(255),1,cv::LINE_4);}for(inti=1;i<=iNUmsX;i++)intxPos=i*iStep;pt1.x=xPos;pt1.y=0+iOffset+src.rows/5;pt2.x=xPos;pt2.y=src.rows-iOffset;cv::imshow("grid",grid);cv::MatbitNot;cv::bitwise_and(src,grid,bitNot);cv::Matadd=cv::Mat::zeros(bitNot.rows,bitNot.cols,bitNot.type());intiDiffTh=200;QTimetimer;timer.start();//#pragmaompparallelfornum_threads(10)for(inti=1;i<bitNot.rows-1;i++){for(intj=1;j<bitNot.cols-1;j++){intiValueX=(int)bitNot.at<uchar>(i,j);intiValueXPre=(int)bitNot.at<uchar>(i-1,j);intiValueXNext=(int)bitNot.at<uchar>(i+1,j);intiValueY=(int)bitNot.at<uchar>(i,j);intiValueYPre=(int)bitNot.at<uchar>(i,j-1);intiValueYNext=(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()<<"processtime:"<<timer.elapsed()<<"ms";//形态学预处理cv::MatmatDilate;cv::Matk33=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(src,matDilate,matRoi);cv::MatmatRoi;cv::bitwise_and(src,matDilate,matRoi);cv::imshow("matRoi",matRoi);cv::MatmatThresh;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::Matcanves;cv::cvtColor(src,canves,cv::COLOR_RGBA2RGB);cv::findContours(matThresh,contoursDefect,hierarchyDefect,cv::RETR_EXTERNAL,cv::CHAIN_APPROX_NONE);for(size_ti=0;i<contoursDefect.size();i++){cv::Matcontour(contoursDefect.at(i));//第i个轮廓doublearea=contourArea(contour);if(area>=50)cv::Momentsmoment;//矩moment=moments(contour,false);cv::Point2dpt1;doublem00=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);}voidtest_lane_detection()inti=0;while(1)cv::Matsrc;QStringdir("D:\\QtProject\\Opencv_Example\\gen_grid_region\\scene_");QStringpath;if(i>9)path=QString("%1%2%3").arg(dir).arg(i++).arg(".png");elsepath=QString("%1%2%3%4").arg(dir).arg("0").arg(i++).arg(".png");cout<<path.toStdString();src=cv::imread(path.toStdString(),cv::IMREAD_GRAYSCALE);if(src.empty()){cout<<"Cannotloadimage"<<endl;return;}cv::imshow("src",src);cv::Matdst;lane_detection(src,dst);
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:怎么使用opencv实现车道线检测的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:如何使用controller传boolean形式值下一篇:

3 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18