# How to calculate the Hough Transform lenght of the straight line

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 close merge delete

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.

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

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

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-l);
theta2 = (l-l);
hyp = hypot(theta1,theta2);

line( cdst, Point(l, l), Point(l, l), Scalar(255,0,0), 3, CV_AA);

}

imshow("detected lines", cdst);


}

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

Sort by » oldest newest most voted

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 - line) * (line - line) + (line - line) * (line - line)));
}


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 ...

more

Official site

GitHub

Wiki

Documentation