-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHoughLine.py
50 lines (33 loc) · 1 KB
/
HoughLine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#HIGH PASS FILTERS can perform these actions:
# edge detection --> using Hough Line Transform
import cv2
import numpy as np
import matplotlib.pyplot as plt
def main():
windowName = 'Hough Line Transform Method'
cap = cv2.VideoCapture(0)
if cap.isOpened():
ret, frame = cap.read()
else:
ret = False
while ret:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 250, apertureSize=3, L2gradient=True)
lines = cv2.HoughLines(edges, 1, np.pi/180, 150)
if lines is not None:
for rho, theta in lines[0]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
pts1 = (int(x0 + 1000*(-b)), int(y0 + 1000*(a)))
pts2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))
cv2.line(frame, pts1, pts2, (0, 255, 0), 3)
cv2.imshow(windowName, frame)
if cv2.waitKey(1)==27: # Exit on ESC
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()