How to count number of lines (Hough Trasnform) in OpenCV
I am successfully able to detect hair strands in an image as lines. I see that the output image detects each hair as line. I use cvHou开发者_JAVA技巧ghLines2()
with method parameter as CV_HOUGH_PROBABILISTIC
.
Now I want to count these lines. The output image shows 1 or 2 line over each hair. I see that each line is composed of small line segments. And so it is difficult to directly get their total number. Any suggestions on this?
Thanks, Pradeep
You may actually be better off using the Canny edge detector. From your description, it seems to have a few key advantages over the Hough transform:
- responds to contours of arbitrary shape, not just straight lines;
- performs noise reduction and non-maximum suppression to minimize the response; and
- does a second detection with looser threshold to "trace" or "solidify" edges.
Given the right parameters, it may be able to uniquely detect each hair with a single, continuous line.
This is implemented in OpenCV in the cvCanny()
function. Play around with the value of the threshold1
and threshold2
parameters (3rd and 4th arguments) to experiment with detection and tracing.
As for actually counting the lines, I'm not sure how best to approach this (Canny output is a somewhat different form from Hough transform output), but you might find it easier to work with unique, solid edges than with fragmented sets of line parameters.
The hough function should return a vector in OpenCV2 or a CvSeq in OpenCV1. You can easily get their length and iterate through them. See the sample code in the OpenCV documentation for more details.
精彩评论