Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 66af624

Browse files
morphological transformations
1 parent c0b68a7 commit 66af624

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

‎Morphological_transforms/ReadMe.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Morphological transformations
2+
This python script will allow us to apply different morphological transformations on an image.
3+
4+
## Setup Instructions
5+
### Install python3
6+
sudo apt-get install python3
7+
### Install pip (package installer for python)
8+
sudo apt-get install python3-pip
9+
### Install OpenCV library with pip
10+
pip3 install opencv-python
11+
### Install tkinter library
12+
sudo apt-get install python3-tk
13+
14+
## Details/Output
15+
A dialog box appears with option to select an image and choice of morphological operation.The result is shown in the window.
16+
The input image is first converted into binary format before applying any morphological transformation and hence the output is in black-white format.
17+
18+
## Author
19+
Github: invigorzz313
5.95 KB
Loading[フレーム]

‎Morphological_transforms/Sample2.png‎

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /