|
2 | 2 | import tkinter as tk
|
3 | 3 | import cv2
|
4 | 4 |
|
5 |
| -def select_image(): # selecting image and thresholding it to binary format |
| 5 | + |
| 6 | +def select_image(): # selecting image and thresholding it to binary format |
6 | 7 | photo = askopenfilename()
|
7 | 8 | global img, thresh
|
8 | 9 | img = cv2.imread(photo)
|
9 | 10 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
10 |
| - threshold_value = 125 # this value needs to be adjusted for every image |
| 11 | + threshold_value = 125 # this value needs to be adjusted for every image |
11 | 12 | ret, thresh = cv2.threshold(gray, threshold_value, 255, 0)
|
12 | 13 |
|
| 14 | + |
13 | 15 | def erosion():
|
14 |
| - kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) |
| 16 | + kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) |
15 | 17 | eroded = cv2.erode(thresh, kernel, iterations=1)
|
16 |
| - eroded = cv2.resize(eroded,(300,300)) |
| 18 | + eroded = cv2.resize(eroded, (300, 300)) |
17 | 19 | cv2.imshow("Erosion", eroded)
|
18 | 20 | cv2.waitKey(0)
|
19 | 21 | cv2.destroyAllWindows()
|
20 | 22 |
|
| 23 | + |
21 | 24 | def dilation():
|
22 |
| - kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) # elliptic kernel |
| 25 | + kernel = cv2.getStructuringElement( |
| 26 | + cv2.MORPH_ELLIPSE, (5, 5)) # elliptic kernel |
23 | 27 | dilated = cv2.dilate(thresh, kernel, iterations=1)
|
24 |
| - dilated = cv2.resize(dilated,(300,300)) |
| 28 | + dilated = cv2.resize(dilated, (300, 300)) |
25 | 29 | cv2.imshow("Dilation", dilated)
|
26 | 30 | cv2.waitKey(0)
|
27 | 31 | cv2.destroyAllWindows()
|
28 | 32 |
|
| 33 | + |
29 | 34 | def opening():
|
30 |
| - kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) |
| 35 | + kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) |
31 | 36 | opened = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
|
32 |
| - opened = cv2.resize(opened,(300,300)) |
| 37 | + opened = cv2.resize(opened, (300, 300)) |
33 | 38 | cv2.imshow("Opening", opened)
|
34 | 39 | cv2.waitKey(0)
|
35 | 40 | cv2.destroyAllWindows()
|
36 | 41 |
|
| 42 | + |
37 | 43 | def closing_opn():
|
38 |
| - kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) |
| 44 | + kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) |
39 | 45 | closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
|
40 |
| - closed = cv2.resize(closed,(300,300)) |
| 46 | + closed = cv2.resize(closed, (300, 300)) |
41 | 47 | cv2.imshow("Closing", closed)
|
42 | 48 | cv2.waitKey(0)
|
43 | 49 | cv2.destroyAllWindows()
|
44 | 50 |
|
| 51 | + |
45 | 52 | def morph_grad():
|
46 |
| - kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) |
| 53 | + kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) |
47 | 54 | grad = cv2.morphologyEx(thresh, cv2.MORPH_GRADIENT, kernel)
|
48 |
| - grad = cv2.resize(grad,(300,300)) |
| 55 | + grad = cv2.resize(grad, (300, 300)) |
49 | 56 | cv2.imshow("Morph gradient", grad)
|
50 | 57 | cv2.waitKey(0)
|
51 | 58 | cv2.destroyAllWindows()
|
52 | 59 |
|
| 60 | + |
53 | 61 | def top_hat():
|
54 |
| - kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) |
| 62 | + kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) |
55 | 63 | tophat = cv2.morphologyEx(thresh, cv2.MORPH_TOPHAT, kernel)
|
56 |
| - tophat = cv2.resize(tophat,(300,300)) |
| 64 | + tophat = cv2.resize(tophat, (300, 300)) |
57 | 65 | cv2.imshow("Top hat", tophat)
|
58 | 66 | cv2.waitKey(0)
|
59 | 67 | cv2.destroyAllWindows()
|
60 | 68 |
|
| 69 | + |
61 | 70 | def black_hat():
|
62 |
| - kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) |
| 71 | + kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) |
63 | 72 | blackhat = cv2.morphologyEx(thresh, cv2.MORPH_BLACKHAT, kernel)
|
64 |
| - blackhat = cv2.resize(blackhat,(300,300)) |
| 73 | + blackhat = cv2.resize(blackhat, (300, 300)) |
65 | 74 | cv2.imshow("Black hat", blackhat)
|
66 | 75 | cv2.waitKey(0)
|
67 | 76 | cv2.destroyAllWindows()
|
68 | 77 |
|
| 78 | + |
69 | 79 | window = tk.Tk()
|
70 | 80 | window.title("Morphological transformations")
|
71 | 81 | window.geometry('320x220')
|
72 |
| -label = tk.Label(window, text="Select an image and then choose an option").grid(row=0, column=0) |
73 |
| -b = tk.Button(window, text="Select image", command=select_image).grid(row=1,column=0) |
| 82 | +label = tk.Label(window, text="Select an image and then choose an option").grid( |
| 83 | + row=0, column=0) |
| 84 | +b = tk.Button(window, text="Select image", |
| 85 | + command=select_image).grid(row=1, column=0) |
74 | 86 |
|
75 | 87 |
|
76 | 88 | rad1 = tk.Radiobutton(window, text='erosion', value=1, command=erosion)
|
77 | 89 | rad2 = tk.Radiobutton(window, text='dilation', value=2, command=dilation)
|
78 | 90 | rad3 = tk.Radiobutton(window, text='opening', value=3, command=opening)
|
79 | 91 | rad4 = tk.Radiobutton(window, text='closing', value=4, command=closing_opn)
|
80 |
| -rad5 = tk.Radiobutton(window, text='morph gradient', value=5, command=morph_grad) |
| 92 | +rad5 = tk.Radiobutton(window, text='morph gradient', |
| 93 | + value=5, command=morph_grad) |
81 | 94 | rad6 = tk.Radiobutton(window, text='top hat', value=6, command=top_hat)
|
82 | 95 | rad7 = tk.Radiobutton(window, text='black hat', value=7, command=black_hat)
|
83 | 96 |
|
|
0 commit comments