-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMorphological operation color
35 lines (31 loc) · 1.16 KB
/
Morphological operation color
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
import cv2
import matplotlib.pyplot as plt
import numpy as np
def main():
path = "C:\\Users\\edkni\\Downloads\\image_processing_files\\Dataset\\"
imgpath = path + "4.2.03.tiff"
img = cv2.imread(imgpath,1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# th = 0
# max_value = 255
# ret,bin_inv = cv2.threshold(img,th,max_value,cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# k = np.ones((5,5),np.float32)
# k = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
# k = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
k = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5))
print(k)
#erosion is expanding of black in border
erosion = cv2.erode(img,k,iterations = 3)
#dilation is expanding of white from border
dilation = cv2.dilate(img,k,iterations = 3)
#gradient is erosion - dilation
gradient = cv2.morphologyEx(img,cv2.MORPH_GRADIENT,k)
outputs = [img,erosion,dilation,gradient]
titles = ["original",'Erosion','Dilation','Gradient']
for i in range(4):
plt.subplot(2,2,i+1)
plt.imshow(outputs[i])
plt.title(titles[i])
plt.show()
if __name__ == "__main__":
main()