-
Notifications
You must be signed in to change notification settings - Fork 0
/
morfological_op.py
61 lines (43 loc) · 1.35 KB
/
morfological_op.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
import cv2
import numpy as np
def erode(img, kernel):
img = cv2.erode(img, kernel, iterations=1)
return img
def dilate(img, kernel):
img = cv2.dilate(img, kernel, iterations=1)
return img
def opening(img, kernel):
img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
return img
def closing(img, kernel):
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
return img
def create_kernel(size, shape):
if shape == 'square':
return np.ones((size, size), np.uint8)
if shape == 'cross':
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (size, size))
return kernel
if shape == 'ellipse':
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (size, size))
return kernel
else:
print("Invalid shape")
return None
def main():
img = cv2.imread("Images/processed.png")
cv2.imshow("Original_img", img)
cv2.waitKey(0)
structuring_element = create_kernel(3, 'square')
img = opening(img, structuring_element)
cv2.imshow("Opening", img)
cv2.waitKey(0)
structuring_element = create_kernel(4, 'square')
img = opening(img, structuring_element)
cv2.imshow("Opening 2", img)
cv2.waitKey(0)
img = dilate(img, structuring_element)
cv2.imshow("Dilate", img)
cv2.waitKey(0)
#if __name__ == "__main__":
# main()