title: Detect lines in Python OpenCV without applying Gaussian Blur
body: <p>I am detecting lines in a noiseless, programmatically generated png file. I would normally use Hough Lines, which requires me to first provide edges from a canny detection, but the first step of the canny detection is to apply a gaussian blur to eliminate noise. Is there a way I can do edge detection on my original image without ever intentionally blurring it? I suspect this will yield better results than burring first since the lines are already perfectly clean and high-contrast.</p>
  <p>Here is a simple example using canny detection and an image. The lines in each group start at 5 pixels wide, then the next line is 4, then 3, 2, and 1. As you can see, the canny detection doesn't work perfectly (the 2 pixel lines appear smaller than the 1 pixel ones):</p>
  <p>Original image:</p>
  <p><a href="https://i.stack.imgur.com/56iiV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/56iiV.png" alt="enter image description here" /></a></p>
  <p>Edges (Result of canny detection):</p>
  <p><a href="https://i.stack.imgur.com/XLFi6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XLFi6.png" alt="enter image description here" /></a></p>
  <p>Sample code:</p>
  <pre><code>
  import cv2
  import numpy as np
  import matplotlib.pylab as plot
  
  # img = cv2.imread(&quot;8px_and_2px_lines.png&quot;)
  img = cv2.imread(&quot;5-1px_lines.png&quot;)
  
  crop_size = 520
  img = img[100:crop_size, 100:crop_size]
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  cv2.imwrite(&quot;5-1px_lines_cropped.png&quot;, img)
  cv2.imshow(&quot;start&quot;, img)
  edges = cv2.Canny(gray, 50, 150, apertureSize=3)
  cv2.imshow(&quot;canny&quot;, edges)
  cv2.imwrite(&quot;5-1px_lines_cropped_canny.png&quot;, edges)
  # plot.imshow(edges, cmap=&quot;gray&quot;)
  # plot.show()
  
  lines = cv2.HoughLines(edges, 1, np.pi / 180, 200)
  line_length = 3000
  for line in lines:
      rho, theta = line[0]
      a = np.cos(theta)
      b = np.sin(theta)
      x0 = a * rho
      y0 = b * rho
      x1 = int(x0 + line_length * (-b))
      y1 = int(y0 + line_length * (a))
      x2 = int(x0 - line_length * (-b))
      y2 = int(y0 - line_length * (a))
      cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
  cv2.imshow(&quot;lines&quot;, img)
  cv2.waitKey()
  </code></pre>
  <p>Any ideas on how I can do a better line detection on these images? I think the gaussian blur built into the canny detector is making the lines harder to detect.</p>