3
3
from tkinter .filedialog import *
4
4
import tkinter as tk
5
5
6
- window = tk .Tk ()
6
+ window = tk .Tk ()
7
7
window .title ("Line detection using Hough transform" )
8
8
window .geometry ('380x100' )
9
- label = tk .Label (window , text = "Choose an option" ).grid (row = 0 ,column = 0 )
9
+ label = tk .Label (window , text = "Choose an option" ).grid (row = 0 , column = 0 )
10
10
# displaying a menu window for choosing transforms
11
11
12
+
12
13
def hough ():
13
14
photo = askopenfilename ()
14
15
img = cv2 .imread (photo )
15
- gray = cv2 .cvtColor (img ,cv2 .COLOR_BGR2GRAY )
16
- edges = cv2 .Canny (gray ,50 ,150 ,apertureSize = 3 )
17
-
18
- lines = cv2 .HoughLines (edges ,1 , np .pi / 180 ,500 )
16
+ gray = cv2 .cvtColor (img , cv2 .COLOR_BGR2GRAY )
17
+ edges = cv2 .Canny (gray , 50 , 150 , apertureSize = 3 )
18
+
19
+ lines = cv2 .HoughLines (edges , 1 , np .pi / 180 , 500 )
19
20
for line in lines :
20
- rho ,theta = line [0 ]
21
- # defining lines with the parameters returned by HoughLines()
22
- a = np .cos (theta )
23
- b = np .sin (theta )
21
+ rho , theta = line [0 ]
22
+ # defining lines with the parameters returned by HoughLines()
23
+ a = np .cos (theta )
24
+ b = np .sin (theta )
24
25
x0 = a * rho
25
26
y0 = b * rho
26
27
x1 = int (x0 + 1000 * (- b ))
27
28
y1 = int (y0 + 1000 * (a ))
28
29
x2 = int (x0 - 1000 * (- b ))
29
30
y2 = int (y0 - 1000 * (a ))
30
- cv2 .line (img ,(x1 ,y1 ),(x2 ,y2 ),(0 ,0 ,255 ),4 ) # drawing lines on image to mark
31
- cv2 .imwrite ('houghlines.jpg' ,img )
32
- cv2 .imshow ("houghlines" ,img )
31
+ # drawing lines on image to mark
32
+ cv2 .line (img , (x1 , y1 ), (x2 , y2 ), (0 , 0 , 255 ), 4 )
33
+ cv2 .imwrite ('houghlines.jpg' , img )
34
+ cv2 .imshow ("houghlines" , img )
33
35
cv2 .waitKey (5000 )
34
36
cv2 .destroyAllWindows ()
35
37
38
+
36
39
def houghP ():
37
40
photo = askopenfilename ()
38
41
img = cv2 .imread (photo )
39
- gray = cv2 .cvtColor (img ,cv2 .COLOR_BGR2GRAY )
40
- edges = cv2 .Canny (gray ,50 ,150 ,apertureSize = 3 )
42
+ gray = cv2 .cvtColor (img , cv2 .COLOR_BGR2GRAY )
43
+ edges = cv2 .Canny (gray , 50 , 150 , apertureSize = 3 )
41
44
42
- lines = cv2 .HoughLinesP (edges ,1 ,np .pi / 180 ,100 ,minLineLength = 100 ,maxLineGap = 10 )
45
+ lines = cv2 .HoughLinesP (edges , 1 , np .pi / 180 , 100 ,
46
+ minLineLength = 100 , maxLineGap = 10 )
43
47
for line in lines :
44
- x1 ,y1 ,x2 ,y2 = line [0 ] # endpoints are returned by HoughLinesP()
45
- cv2 .line (img ,(x1 ,y1 ),(x2 ,y2 ),(0 ,0 ,255 ),4 ) #drawing lines by joining those
46
- cv2 .imwrite ('houghlinesP.jpg' ,img )
47
- cv2 .imshow ("houghlinesP" ,img )
48
+ x1 , y1 , x2 , y2 = line [0 ] # endpoints are returned by HoughLinesP()
49
+ # drawing lines by joining those
50
+ cv2 .line (img , (x1 , y1 ), (x2 , y2 ), (0 , 0 , 255 ), 4 )
51
+ cv2 .imwrite ('houghlinesP.jpg' , img )
52
+ cv2 .imshow ("houghlinesP" , img )
48
53
cv2 .waitKey (5000 )
49
- cv2 .destroyAllWindows ()
54
+ cv2 .destroyAllWindows ()
50
55
51
56
52
- rad1 = tk .Radiobutton (window ,text = 'Hough Transform' , value = 1 , command = hough )
53
- rad2 = tk .Radiobutton (window ,text = 'Probabilistic Hough Transform' , value = 2 , command = houghP )
57
+ rad1 = tk .Radiobutton (window , text = 'Hough Transform' , value = 1 , command = hough )
58
+ rad2 = tk .Radiobutton (
59
+ window , text = 'Probabilistic Hough Transform' , value = 2 , command = houghP )
54
60
55
- rad1 .grid (row = 1 ,column = 0 )
56
- rad2 .grid (row = 2 ,column = 0 )
61
+ rad1 .grid (row = 1 , column = 0 )
62
+ rad2 .grid (row = 2 , column = 0 )
57
63
58
- label = tk .Label (window , text = "Check the output image in this folder you are working in" ).grid (row = 3 ,column = 0 )
64
+ label = tk .Label (window , text = "Check the output image in this folder you are working in" ).grid (
65
+ row = 3 , column = 0 )
59
66
60
- window .mainloop ()
67
+ window .mainloop ()
0 commit comments