Question

I want to detect small straight lines in a noisy image. For this I

- find contours in an image, 
- fit an ellipse on it 
- and get the height by width ratio. 
- If this ratio exceeds a certain threshold, I declare it as line segment. 

This method works fine most of the time. But in certain cases (according to some documentation, when the contours self-intersect), the fitEllipse gives wrong results. It there any other method, I can determine the longevity of the contours? I do not intend to use Hough Lines.

Was it helpful?

Solution

One way to grow a line from contour points is to track pixels on the contour while checking for line straightness. At each new pixel you estimate the longest distance from the covered contour and the line connecting contour's beginning and the current contour point. When this distance exceeds a threshold you stop growing a line. In the picture below, the line is red, contour is black and the distance is green.

In your case (since you aren't growing the line) all you need to do is to connect two contour terminals and then scan the contour to get all the distances to the line and select the largest one. Compare it to your threshold to decide on the linearity of the segment.

enter image description here

Here is how to find the distance from point x, y to the line ax+by+c=0 or to the line defined by two points as in your case: link

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top