5
votes

I implemented the Hough Lines Transform in OpenCV (c++) and I get strange artifacts in the Hough Space. The following picture shows the Hough Space. The distance rho is depicted in the rows while the 180 columns represent the angle from 0 to 179 degree. If you zoom in on column 45 and 135 you see a vertical line with alternating dark and bright pixels. http://imgur.com/NDtMn6S

For higher thresholds the lines of the fence are detected fine but when I lower the threshold the artifacts can be seen as 45° or 135° rotated lines in the final picture: Detected lines for medium threshold

At first I thought it was a mistake in my implementation of the Hough Lines method but get similar lines for medium thresholds using OpenCV's Hough Line method. I also encounter the same problem when using Canny instead of Sobel.

So the question is: why do I get these artifacts and how can I get rid of them? I wasn't able to find anything about this and any help would be appreciated.

This is the code I used with the OpenCV Hough Lines method:

// read in input image and convert to grayscale
Mat frame = imread("fence.png", CV_LOAD_IMAGE_COLOR);
Mat final_out;
frame.copyTo(final_out);

Mat img, gx, gy, mag, angle;
cvtColor(frame, img, CV_BGR2GRAY);

// get the thresholded maggnitude image
Sobel(img, gx, CV_64F, 1, 0);
Sobel(img, gy, CV_64F, 0, 1);
cartToPolar(gx, gy, mag, angle);

normalize(mag, mag, 0, 255, NORM_MINMAX);
mag.convertTo(mag, CV_8U);
threshold(mag, mag, 55, 255.0, THRESH_BINARY);

// apply the hough lines transform and draw the lines
vector<Vec2f> lines;
HoughLines(mag, lines, 1, CV_PI / 180, 240);
for( size_t i = 0; i < lines.size(); i++ )
{
    float rho = lines[i][0], theta = lines[i][1];
    Point pt1, pt2;

    pt1.x = 0;
    pt1.y = (rho - pt1.x * cos(theta))/sin(theta);
    pt2.x = mag.cols;
    pt2.y = (rho - pt2.x * cos(theta))/sin(theta);

    line(final_out, pt1, pt2, Scalar(0,0,255), 1, CV_AA);
}

// show the image
imshow("final_image", final_out);
cvWaitKey();
2
can you post your thresholded gradient magnitude image?Micka
do you observe the same behaviour for different images? In general, 45 and 135 degree lines have the most space available in landscape oriented images so chance of higher accumulator values is bigger even if there are no lines present. So lowering the threshold should first introduce noise lines in those angles, I guess. Or is there some kind of accumulator normalization in Hough?Micka
I used a different image now that is square and with easy lines and again for medium thresholds the 45 and 135 degree lines appear. thresholded magnitude and final output image As far as I know there is no accumulator normalization in Hough and in general I observed this pattern for various images.fishishuntingsharks

2 Answers

2
votes

Answering the question - you can't get rid of such artifact - it's mathematical by nature due to discrete nature of image and pixels' grid orthogonality. The only way is to exclude exact 45 degree from the analysis.

I found the source - the bright pixels of anomaly are produced by the next issue:

  • Red dots - exactly 45 degree bright anomaly - you can see they are doubled making stairs pattern - which doubles number of pixels involved in accumulation.
  • Blue dots - exactly 45 degree dim anomaly - making chess-board pattern
  • Green dots - 44 degree line - you can see it's alternate doubling and chess patterns - which mediates anomaly.

If you look on whole picture of Hough transform matrix you will see how brightness slowly shifting across whole picture - representing slowly changing this alternation ratio depending on angle. However, due to nature of the pixel grid, at exactly 45 degree it makes this anomaly very acute. I don't know how to deal with it yet...

0
votes

Stumbled across this and maybe useful to future people.

The image is inverted; the algorithm is accumulating the white pixels which obviously there are more of along the diagonals of the image. The lines you are looking for are black, which means they are zero valued and not considered.