I need to scan an image in OpenCV and obtain the lines. For this I use the canny edge detector and then the propabilistic Hough transform, with which I actually get the lines.
The problem now is that I get many short lines instead of a few continuous ones. The image I am analyzing has the size 1024x768 and I am wondering if this could be an error source. Theoretically making the image smaller should lead to fewer lines. Might that help me in my task? Or are there any other factors which I should take into account when extracting lines?
1 Answer
You should play around with the many parameters of the Canny()
and HoughLinesP()
functions before resorting to changing the image size.
The prototype for Canny()
is:
void Canny(InputArray image, OutputArray edges,
double threshold1, double threshold2,
int apertureSize=3, bool L2gradient=false )
You should play with the threshold1
, threshold2
, aperturSize
and L2gradient
parameters until you get an output from Canny()
that accurately marks the pixel involved in the lines in your image. The threshold2
parameter controls the intensity above which a gradient value will be considered definitely part of an edge. Setting it higher will give you fewer edges. The threshold1
parameter controls the intensity above which a pixel will be considered to continue an edge from a neighboring pixel. Setting it lower will give you longer edges. The ratio threshold2
/threshold1
should be at least 2.0 and can be as large as 3.0, and maybe larger. Setting L2gradient
to true
will make the algorithm slightly slower but will use a more accurate gradient calculation from the first derivatives. Setting apertureSize
to 5 or 7 might smooth out some noise in the first derivative calculation.
Only once you have Canny()
accurately finding the edges in your image should you move on to running HoughLinesP()
. HoughLinesP()
also has a bunch of knobs that you need to tune to get decent results.
void HoughLinesP(InputArray image, OutputArray lines,
double rho, double theta,
int threshold,
double minLineLength=0,
double maxLineGap=0 )
rho
and theta
can be set larger and smaller depending on how many lengths and angles you want to allow. Setting rho
and theta
larger will give you fewer line segments, but they will be less accurate. threshold
can be increased to reduce the number of line segments that you get. minLineLength
can be increased to reject short line segments and maxLineGap
can be increased to make it easier for short lines to get connected together.
Setting Canny()
's apertureSize
parameter larger is somewhat like working with a smaller image, because a larger aperture is doing more blurring/smoothing of the details of the image. Setting HoughLinesP()
's rho
and theta
parameters larger is somewhat like working with a smaller image because you are lumping together small differences in angle or length that would be indistinguishable in the smaller image.
The OpenCV documentation for HoughLinesP()
has an example with:
Canny( src, dst, 50, 200, 3 );
...
HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );