Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hough Line Transform - artifacts at 45 degree angle

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();
like image 313
fishishuntingsharks Avatar asked Oct 31 '22 14:10

fishishuntingsharks


1 Answers

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

like image 65
z08840 Avatar answered Nov 15 '22 04:11

z08840