33from tkinter .filedialog import *
44import tkinter as tk
55
6- window = tk .Tk ()
6+ window = tk .Tk ()
77window .title ("Line detection using Hough transform" )
88window .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 )
1010# displaying a menu window for choosing transforms
1111
12+
1213def hough ():
1314 photo = askopenfilename ()
1415 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 )
1920 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 )
2425 x0 = a * rho
2526 y0 = b * rho
2627 x1 = int (x0 + 1000 * (- b ))
2728 y1 = int (y0 + 1000 * (a ))
2829 x2 = int (x0 - 1000 * (- b ))
2930 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 )
3335 cv2 .waitKey (5000 )
3436 cv2 .destroyAllWindows ()
3537
38+
3639def houghP ():
3740 photo = askopenfilename ()
3841 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 )
4144
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 )
4347 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 )
4853 cv2 .waitKey (5000 )
49- cv2 .destroyAllWindows ()
54+ cv2 .destroyAllWindows ()
5055
5156
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 )
5460
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 )
5763
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 )
5966
60- window .mainloop ()
67+ window .mainloop ()
0 commit comments