简体   繁体   English

带OpenCV的车道检测器分隔线c ++

[英]Lane Detector divider lines c ++ with OpenCV

Now I have been working on the analysis of images with OpenCV, what I'm trying to do is recognize the lane dividing lines, what I do is the following: 现在,我一直在使用OpenCV进行图像分析,我想做的就是识别车道分隔线,我要做的是:

1.I receive a image,
2. Then transform it to grayscale
3.I apply the GaussianBlur
4.After I place me in the ROI
5.I apply the canny
6.then I look for lines with hough transform Lines 
7.Draw the lines obtained from hough

But I've run into a problem which is: that recognizes no dividing lines both rail and neither recognizes the yellow lines. 但是我遇到了一个问题:无法识别两条分界线,也不能识别黄线。

I hope to help me solve this problem, you will thank a lot. 希望能帮助我解决这个问题,非常感谢。 Then I put the code 然后我把代码

#include "opencv2/highgui/highgui.hpp"
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>
#include <stdio.h>
#include "linefinder.h"

using namespace cv;

int main(int argc, char* argv[]) {
int houghVote = 200;
string arg = argv[1];
Mat image;
image = imread(argv[1]);    
    Mat gray;
    cvtColor(image,gray,CV_RGB2GRAY);
   GaussianBlur( gray, gray, Size( 5, 5 ), 0, 0 );
    vector<string> codes;
    Mat corners;
    findDataMatrix(gray, codes, corners);
    drawDataMatrixCodes(image, codes, corners);
 //Mat image = imread("");
    //Rect region_of_interest = Rect(x, y, w, h);
    //Mat image_roi = image(region_of_interest);
 std::cout << image.cols << "\n";
 std::cout << image.rows << "\n";
 Rect roi(0,290,640,190);// set the ROI for the image
 Mat imgROI = image(roi);
 // Display the image
 imwrite("original.bmp", imgROI);
// Canny algorithm
Mat contours;
Canny(imgROI, contours, 120, 300, 3); 
imwrite("canny.bmp", contours);
Mat contoursInv;
threshold(contours,contoursInv,128,255,THRESH_BINARY_INV);
// Display Canny image
imwrite("contours.bmp", contoursInv);

/* 
Hough tranform for line detection with feedback
Increase by 25 for the next frame if we found some lines.  
This is so we don't miss other lines that may crop up in the next frame
but at the same time we don't want to start the feed back loop from scratch. 
*/
std::vector<Vec2f> lines;
if (houghVote < 1 or lines.size() > 2){ // we lost all lines. reset 
    houghVote = 200; 
}else{ 
    houghVote += 25;
} 
while(lines.size() < 5 && houghVote > 0){
    HoughLines(contours,lines,1,PI/180, houghVote);
    houghVote -= 5;
}
std::cout << houghVote << "\n";
Mat result(imgROI.size(),CV_8U,Scalar(255));
imgROI.copyTo(result);
// Draw the limes
std::vector<Vec2f>::const_iterator it= lines.begin();
Mat hough(imgROI.size(),CV_8U,Scalar(0));
while (it!=lines.end()) {
    float rho= (*it)[0];   // first element is distance rho
    float theta= (*it)[1]; // second element is angle theta
    if ( theta > 0.09 && theta < 1.48 || theta < 3.14 && theta > 1.66 ) { 
    // filter to remove    vertical and horizontal lines
        // point of intersection of the line with first row
        Point pt1(rho/cos(theta),0);        
        // point of intersection of the line with last row
        Point pt2((rho-result.rows*sin(theta))/cos(theta),result.rows);
        // draw a white line
        line( result, pt1, pt2, Scalar(255), 8); 
        line( hough, pt1, pt2, Scalar(255), 8);
    }
    ++it;
   }

   // Display the detected line image
   std::cout << "line image:"<< "\n";
   namedWindow("Detected Lines with Hough");
   imwrite("hough.bmp", result);

   // Create LineFinder instance
   LineFinder ld;

  // Set probabilistic Hough parameters
   ld.setLineLengthAndGap(60,10);
   ld.setMinVote(4);

  // Detect lines
  std::vector<Vec4i> li= ld.findLines(contours);
  Mat houghP(imgROI.size(),CV_8U,Scalar(0));
  ld.setShift(0);
  ld.drawDetectedLines(houghP);
  std::cout << "First Hough" << "\n";
  imwrite("houghP.bmp", houghP);

  // bitwise AND of the two hough images
  bitwise_and(houghP,hough,houghP);
  Mat houghPinv(imgROI.size(),CV_8U,Scalar(0));
  Mat dst(imgROI.size(),CV_8U,Scalar(0));
  threshold(houghP,houghPinv,150,255,THRESH_BINARY_INV); // threshold and invert to black lines
  namedWindow("Detected Lines with Bitwise");
  imshow("Detected Lines with Bitwise", houghPinv);

  Canny(houghPinv,contours,100,350);
  li= ld.findLines(contours);
 // Display Canny image
 imwrite("contours.bmp", contoursInv);

 // Set probabilistic Hough parameters
  ld.setLineLengthAndGap(5,2);
  ld.setMinVote(1);
  ld.setShift(image.cols/3);
  ld.drawDetectedLines(image);

  std::stringstream stream;
  stream << "Lines Segments: " << lines.size();

  putText(image, stream.str(), Point(10,image.rows-10), 2, 0.8, Scalar(0,0,255),0); 
  imwrite("processed.bmp", image);

  char key = (char) waitKey(10);
  lines.clear();
  }

The following are the input images respectively: 以下分别是输入图像: 输入1输入2

Here I show two photos one that recognizes the white line and another that does not recognize the yellow line, what I require is to recognize the dividing lines because I monitor the lane, but is complicated to me and it does not recognize the presence of all dividing lines, I hope help me because I have honestly tried everything but I have not had good results. 在这里,我显示了两张照片,一张可以识别白线,另一张不能识别黄线,我需要的是识别分界线,因为我监视车道,但对我来说很复杂,并且不能识别所有的存在分界线,希望对我有所帮助,因为我已经诚实地尝试了所有事情,但效果并不理想。

车道2巷

I think it's because you are doing a bitwise addition of both probabilistic hough and regular hough transforms. 我认为这是因为您正在对概率霍夫变换和常规霍夫变换进行按位加法。 This means that the outputted image will only contain lines that appear in both of these transforms. 这意味着输出的图像将只包含出现在这两个变换中的线条。 I'm pretty sure in the regular transform the line is not detected but in the probabilistic hough output the line is detected. 我很确定在常规转换中未检测到该行,但在概率霍夫输出中已检测到该行。 You're best bet is to output both transforms separately and debug. 最好的选择是分别输出两个转换并进行调试。 I'm doing a similar project, I imagine you could include a separate ROI to exclude from the bitwise addition and that area would be along the centrum of the lane markings. 我正在做一个类似的项目,我想您可以包括一个单独的ROI,以从按位添加中排除,并且该区域将沿着车道标记的中心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM