-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_image_morphology.py
54 lines (43 loc) · 1.73 KB
/
binary_image_morphology.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
import numpy as np
import cv2
# Binary Image Input
img = np.array([[0, 1, 1],
[0, 0, 1],
[0, 1, 0]])
SHAPE = 'cross' #'cross' for V/H, 'square' for V/H/D
def apply_morphology(image, operation, struct_element):
image = image.astype(np.uint8)
# Define the structuring element
if struct_element == 'cross':
struct = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))
elif struct_element == 'square':
struct = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
else:
raise ValueError(f"Invalid structuring element {struct_element}. Choose 'cross' or 'square'")
# Perform the chosen operation
if operation == 'dilation':
result = cv2.dilate(image, struct)
elif operation == 'erosion':
result = cv2.erode(image, struct)
elif operation == 'opening':
result = cv2.morphologyEx(image, cv2.MORPH_OPEN, struct)
elif operation == 'closing':
result = cv2.morphologyEx(image, cv2.MORPH_CLOSE, struct)
else:
raise ValueError(f"Invalid operation {operation}. Choose 'dilation', 'erosion', 'opening', or 'closing'")
print(result)
return result
print("\n---Initial Image---")
print(img)
# Apply dilation on the original image
print("\n---Dilation---")
result_dilation = apply_morphology(img, 'dilation', SHAPE)
# Apply erosion on the original image
print("\n---Erosion---")
result_erosion = apply_morphology(img, 'erosion', SHAPE)
# Perform closing on the original image
print("\n---Closing---")
result_closing = apply_morphology(img, 'closing', SHAPE)
# Perform opening on the original image
print("\n---Opening---")
result_opening = apply_morphology(img, 'opening', SHAPE)