-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorphology.py
More file actions
46 lines (37 loc) · 1.24 KB
/
morphology.py
File metadata and controls
46 lines (37 loc) · 1.24 KB
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
import cv2
import numpy as np
img = np.zeros((600, 600))
cv2.putText(img, "JAN", (150, 350), cv2.FONT_HERSHEY_SIMPLEX, 5, 255, 25)
cv2.imshow("Black background", img)
cv2.waitKey(0)
print(img)
kernel = np.ones((5, 5))
erosion = cv2.erode(img, kernel, iterations=1)
cv2.imshow("Erosion - 1 Iteration", erosion)
cv2.waitKey(0)
erosion = cv2.erode(img, kernel, iterations=5)
cv2.imshow("Erosion - 5 Iteration", erosion)
cv2.waitKey(0)
dilation = cv2.dilate(img, kernel, iterations=1)
cv2.imshow("Dilation - 1 Iteration", dilation)
cv2.waitKey(0)
dilation = cv2.dilate(img, kernel, iterations=5)
cv2.imshow("Dilation - 5 Iteration", dilation)
cv2.waitKey(0)
whiteNoise = np.random.randint(low=-1, high=2, size=(600, 600))
whiteNoise *= 255
whiteNoisedImg = img + whiteNoise
whiteNoisedImg[whiteNoisedImg == -255] = 0
cv2.imshow("White Noised Img", whiteNoisedImg)
print(whiteNoisedImg)
cv2.waitKey(0)
kernel = np.ones((2, 2))
opening = cv2.morphologyEx(whiteNoisedImg, cv2.MORPH_OPEN, kernel)
cv2.imshow("Opening", opening)
cv2.waitKey(0)
closing = cv2.morphologyEx(whiteNoisedImg, cv2.MORPH_CLOSE, kernel)
cv2.imshow("Closing", closing)
cv2.waitKey(0)
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
cv2.imshow("Gradient", gradient)
cv2.waitKey(0)