-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathErosion-Morphological-Operation-Without-Build-in.py
43 lines (43 loc) · 1.33 KB
/
Erosion-Morphological-Operation-Without-Build-in.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
import cv2 as cv
import numpy as np
# padding
def padding(originalImg, padSize):
padImg = np.zeros((rows+2*padSize, columns+2*padSize), dtype=np.uint8)
# using Slicing
padImg[padSize:rows+padSize, padSize:columns+padSize] = originalImg
return padImg
# Morphological Erosion
def Erosion(padImg, kernel, size):
output = np.zeros((rows, columns), dtype=np.uint8)
for i in range(0, rows):
for j in range(0, columns):
# Slicing
portion = padImg[i:i+size, j:j+size]
portion1 = portion.flatten()
portion2 = kernel.flatten()
# sum of kernel and window
p1 = (np.sum(portion1))
p2 = (np.sum(portion2))*255
# if Fit condition satisfies
if p1 == p2:
output[i, j] = 255
else:
output[i, j] = np.min(portion1)
return output
size = 19
# Structuring Element
kernel = np.ones((size, size), np.uint8)
# padding size
p_size = size//2
# image reading
orginalImg = cv.imread('erosion.jpg', 0)
# getting size of image
rows = orginalImg.shape[0]
columns = orginalImg.shape[1]
# padding function call
padImg = padding(orginalImg, p_size)
# Morphological Erosion
Ero = Erosion(padImg, kernel, size)
# erode image show
cv.imshow('output', Ero)
cv.waitKey(0)