-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathHoughLine.py
67 lines (55 loc) · 2.04 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import cv2
import numpy as np
from tkinter.filedialog import *
import tkinter as tk
window = tk.Tk()
window.title("Line detection using Hough transform")
window.geometry('380x100')
label = tk.Label(window, text="Choose an option").grid(row=0, column=0)
# displaying a menu window for choosing transforms
def hough():
photo = askopenfilename()
img = cv2.imread(photo)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
lines = cv2.HoughLines(edges, 1, np.pi/180, 500)
for line in lines:
rho, theta = line[0]
# defining lines with the parameters returned by HoughLines()
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
# drawing lines on image to mark
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 4)
cv2.imwrite('houghlines.jpg', img)
cv2.imshow("houghlines", img)
cv2.waitKey(5000)
cv2.destroyAllWindows()
def houghP():
photo = askopenfilename()
img = cv2.imread(photo)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100,
minLineLength=100, maxLineGap=10)
for line in lines:
x1, y1, x2, y2 = line[0] # endpoints are returned by HoughLinesP()
# drawing lines by joining those
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 4)
cv2.imwrite('houghlinesP.jpg', img)
cv2.imshow("houghlinesP", img)
cv2.waitKey(5000)
cv2.destroyAllWindows()
rad1 = tk.Radiobutton(window, text='Hough Transform', value=1, command=hough)
rad2 = tk.Radiobutton(
window, text='Probabilistic Hough Transform', value=2, command=houghP)
rad1.grid(row=1, column=0)
rad2.grid(row=2, column=0)
label = tk.Label(window, text="Check the output image in this folder you are working in").grid(
row=3, column=0)
window.mainloop()