Ask Your Question
1

Detected lanes with Hough in OpenCV

asked 2015-01-16 00:48:33 -0600

Ztar03 gravatar image

updated 2020-10-21 13:12:43 -0600

Now I've 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:

  1. I receive an image,
  2. Then grayscale transform
  3. I applying GaussianBlur
  4. After I get in the ROI
  5. I apply the wily
  6. After looking lines with lines Hough transform
  7. Draw lines obtained from the Hough

But I've run into a problem which is: it does not recognize dotted boundaries, nor does not always recognize the yellow lines. Someone give me an idea I've tried everything but can not get anything ...

I hope you help me solve this problem, you will thank a lot. I attached code and some sample images.

#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"

#define PI 3.1415926

using namespace cv;
using namespace std;

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,200,640,206);
  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 ...
(more)
edit retag flag offensive close merge delete

Comments

4

Hmm you know that your lanes are actually not straight so this task will be quite hard. First guess, your Houghparameters are incorrect. You are probably looking for to long sized lines and in the incorrect orientation.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-01-17 06:21:01 -0600 )edit

I modified the code as you indicated to me and now I got a picture like the one attached.

Ztar03 gravatar imageZtar03 ( 2015-01-18 15:41:06 -0600 )edit

I would say that your result is correct. As far as I see all lines go to the infinity vantage point, which is correctly. It is now your task to define which lines are in the ground plane.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-01-19 03:06:53 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-01-27 10:46:38 -0600

Ztar03 gravatar image

Thank you very much for the help you've resolved by adjusting the angle at which the Hough works and have gone a little further segmentation only now emerged a new problem I hope you can help me here I leave the link http://answers.opencv.org/question/53...

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-01-16 00:48:33 -0600

Seen: 1,017 times

Last updated: Jan 18 '15