Giulia Neri
Giulia Neri

Reputation: 1

Line Detect with HoughLinesP

With OpenCV and C++ I'm trying to detect the lines of a street from an input video. I'm using HoughLinesP and I'd like to detect ONLY the lines that delimit the street, so not horizontal or vertical for example.
Using HoughLinesP(dst, lines, 1, CV_PI/180, 8, 80, 3), I detect all the lines so I changed double theta (CV_PI/180) to this HoughLinesP(dst, lines, 10*CV_PI/180<=theta<=80*CV_PI/180 & 110*CV_PI/180<=theta<=170*CV_PI/180, 8, 80, 3); But it doesn't work because the console display only the video without any type of lines.

Upvotes: 0

Views: 1112

Answers (1)

marol
marol

Reputation: 4074

The fourth argument to HoughLinesP is not an angle value that tell opencv to detect only lines which are oriented from the OX axis (ie in polar coordinates). Rather than, the angle value passed in tells opencv algorithm to iterate from 0 to PI (or 2*PI, depends how algorithm is implemented) having this angle as iteration step, e.g. iterating from 0 to PI by PI/180 will take 180 iterations in HoughLinesP trying to find a line for a given (r,alpha).

The solution to find lines having polar coords with given angle range (not the most robust one) could be to detect all lines with HoughLinesP and then iterate over them, calculate angle coorinate and filter out those which are having polar angle coord in a given range.

EDIT (a draft of the algorithm using C++11):

  vector<Vec4i> detectedLines;
  HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );
  vector<Vec4i> filteredLines(detectedLines.size());
  const float downAngleRange = 30*CV_PI/180;
  const float upAngleRange = 60*CV_PI/180;
  auto it = copy_if(detectedLines.begin(), detectedLines.end(),
    filteredLines.begin(),
    [](const Vec4i &v) {
       float angle = calculateAnglePolarCord(v);
       return angle <= upAngleRange && angle >= downAngleRange;
    });
   filteredLInes.resize(std::distance(filteredLines.begin(),it));  

Where calculateAnglePolarCord is a method which on given line calculats its second (angle) polar coordinate.

Remember to implement good float comparison technique.

Upvotes: 1

Related Questions