First time here? Check out the FAQ!

Ask Your Question
1

How to calculate the Hough Transform lenght of the straight line

asked Apr 23 '15

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 );

Preview: (hide)

Comments

3

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 (Apr 23 '15)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?

TQ

zms gravatar imagezms (Apr 23 '15)edit
1

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 (Apr 23 '15)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 (Apr 23 '15)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 (Apr 23 '15)edit

1 answer

Sort by » oldest newest most voted
2

answered Apr 23 '15

thdrksdfthmn gravatar image

updated Apr 23 '15

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 ...
Preview: (hide)

Comments

zms gravatar imagezms (Apr 24 '15)edit

Question Tools

1 follower

Stats

Asked: Apr 23 '15

Seen: 3,350 times

Last updated: Apr 23 '15