-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorphology.py
27 lines (19 loc) · 917 Bytes
/
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
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img=cv.imread('smarties.png',cv.IMREAD_GRAYSCALE)
_,mask=cv.threshold(img,220,255,cv.THRESH_BINARY_INV)
kernal=np.ones((5,5),np.uint8)
dilation=cv.dilate(mask,kernal,iterations=2)
erosion=cv.erode(mask,kernal,iterations=1)
opening=cv.morphologyEx(mask,cv.MORPH_OPEN,kernal)# erosion followed by dilation
closing=cv.morphologyEx(mask,cv.MORPH_CLOSE,kernal)# dilation followed by erosion
mg=cv.morphologyEx(mask,cv.MORPH_GRADIENT,kernal)# difference between dilation and erosion
th=cv.morphologyEx(mask,cv.MORPH_TOPHAT,kernal)#difference between opeing and image
titles=['image','mask','dilation','erosion','opening','closing','mg','th']
images=[img,mask,dilation,erosion,opening,closing,mg,th]
for i in range(8):
plt.subplot(2,4,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()