title: Hough Line Transform - artifacts at 45 degree angle
body: <p>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.
  <a href="http://imgur.com/NDtMn6S" rel="noreferrer">http://imgur.com/NDtMn6S</a></p>
  
  <p>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:
  <a href="http://i.stack.imgur.com/Oxr5j.png" rel="noreferrer">Detected lines for medium threshold</a></p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>This is the code I used with the OpenCV Hough Lines method:</p>
  
  <pre><code>// 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&lt;Vec2f&gt; lines;
  HoughLines(mag, lines, 1, CV_PI / 180, 240);
  for( size_t i = 0; i &lt; 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();
  </code></pre>