Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pknowledge/62ad0d100d6d4df756c0374dee501131 to your computer and use it in GitHub Desktop.
Save pknowledge/62ad0d100d6d4df756c0374dee501131 to your computer and use it in GitHub Desktop.
Hough Line Transform opencv python
import cv2
import numpy as np
img = cv2.imread('sudoku.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
cv2.imshow('edges', edges)
lines = cv2.HoughLines(edges, 1, np.pi / 180, 200)
for line in lines:
rho,theta = line[0]
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
# x1 stores the rounded off value of (r * cos(theta) - 1000 * sin(theta))
x1 = int(x0 + 1000 * (-b))
# y1 stores the rounded off value of (r * sin(theta)+ 1000 * cos(theta))
y1 = int(y0 + 1000 * (a))
# x2 stores the rounded off value of (r * cos(theta)+ 1000 * sin(theta))
x2 = int(x0 - 1000 * (-b))
# y2 stores the rounded off value of (r * sin(theta)- 1000 * cos(theta))
y2 = int(y0 - 1000 * (a))
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.imshow('image', img)
k = cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment