Ask Your Question

How to calculate the Hough Transform lenght of the straight line

asked 2015-04-23 00:39:56 -0500

zms gravatar image

Hi, I'm currently doing the HT process. The information that I received from the library is the straight line. However, how can we measure the straight lines? In what unit is preferable?

vector<vec4i> lines; HoughLinesP(dst, lines, 1, CV_PI/180, 50, 20, 10 );

edit retag flag offensive close merge delete



Could explain a bit better what you want to do? HoughLinesP gives you the end-points of the lines, so that you can simply get the length as the euclidean distance between these points.

FooBar gravatar imageFooBar ( 2015-04-23 01:00:30 -0500 )edit

from the codes, I had received a lot of expected straight lines. The prob is, I want to have only the longest straight lines from it. From the Euclidean distance, I had get the value.. (it is in pixel saiz right?) . And based on the lines that I had received, how can I manage to filter and select only the longest line?


zms gravatar imagezms ( 2015-04-23 03:02:50 -0500 )edit

If you measure it in the image you have, then for sure you shall measure it in pixels. I am not sure, but I suppose that the points coordinate in the vec4i (x1,y1,x2,y2) are the two limit points of the detected segment, so, you can apply Euclidean distance

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-04-23 03:04:28 -0500 )edit

Hi, OK, here is the code that I worked on the Euclidean Distance and it returns me a lot of straight lines. Would I able to filter only the longest one?

      HoughLinesP(dst, lines, 1, CV_PI/180, 50, 20, 10 );

      for( size_t i = 0; i < lines.size(); i++ )
        Vec4i l = lines[i];
        double theta1,theta2, hyp, result;

        theta1 = (l[3]-l[1]);
        theta2 = (l[2]-l[0]);
        hyp = hypot(theta1,theta2);

        line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,0,0), 3, CV_AA);


      imshow("detected lines", cdst);


zms gravatar imagezms ( 2015-04-23 03:39:18 -0500 )edit

Then you shall create the function that I have posted in the answer and apply it on your lies for filtering them.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-04-23 06:18:44 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-04-23 03:28:25 -0500

thdrksdfthmn gravatar image

updated 2015-04-23 06:26:06 -0500

So you have the lengths of the detected segments. Now you need to filter them. For example you can create a function that for an input vector<vec4i> it returns another vector<vec4i> that has just the lines longer than a parameter:

std::vector<cv::Vec4i> filterLinesByLen(const std::vector<cv::Vec4i>& lines, double minLen)
     std::vector<cv::vec4i> longLines;
     for (cv::Vec4i line : lines)
     {   if (euclidDist(line) >= minLen) { longLines.push_back(line); }    }
     return longLines;

the eucldian distance shoud be something like:

double euclidDist(const cv::Vec4i& line)
    return std::sqtr(static_cast<double>((line[0] - line[2]) * (line[0] - line[2]) + (line[1] - line[3]) * (line[1] - line[3])));

In your code you shall do something like this

std::vector<cv::Vec4i> longLines = filterLinesByLen(lines, 80); // you can replace 80 by a value that you think is good, or create a function that returns the mean of the lenghts ...
edit flag offensive delete link more


zms gravatar imagezms ( 2015-04-24 03:23:03 -0500 )edit

Question Tools

1 follower


Asked: 2015-04-23 00:39:56 -0500

Seen: 2,738 times

Last updated: Apr 23 '15