-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmanualErosionAndDilation.py
executable file
·81 lines (65 loc) · 2.37 KB
/
manualErosionAndDilation.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
# This file is meant to demonstrate erosions and dilations in OpenCV.
# For some of the theory, please see wikipedia / etc.
import cv2
import numpy as np
WHITE = 255
BLACK = 0
THRESH = 127
ksize = 5
# Dilation essentially makes white / bright areas bigger, and makes
# black / dark images smaller. It is done by taking the max of the
# kernel iterated over the entire image.
# Only works for grayscale images
# This function is SLOW
def manual_dilate(image):
dilated = np.zeros(image.shape, np.uint8)
i,j = 0,0
offset = int(ksize / 2)
for i in xrange(image.shape[0]):
for j in xrange(image.shape[1]):
a = image.take(range(i-offset, i+offset + 1), mode="wrap", axis=0)
b = a.take(range(j-offset, j+offset + 1), mode="wrap", axis=1)
dilated[i][j] = np.amax(b)
return dilated
# The opposite of dilation, erosion makes dark areas bigger, and bright
# areas smaller. This is done by taking the min of the kernel, iterated
# over the entire image.
# Only works for grayscale images
# This function is SLOW
def manual_erode(image):
eroded = np.zeros(image.shape, np.uint8)
i,j = 0,0
offset = int(ksize / 2)
for i in xrange(image.shape[0]):
for j in xrange(image.shape[1]):
a = image.take(range(i-offset, i+offset + 1), mode="wrap", axis=0)
b = a.take(range(j-offset, j+offset + 1), mode="wrap", axis=1)
eroded[i][j] = np.amin(b)
return eroded
# Our simple threshold function from before.
def threshold(image):
grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, thresholded = cv2.threshold(grey, THRESH, WHITE, cv2.THRESH_BINARY)
return thresholded
def main():
window_name = "Webcam!"
cam_index = 0
cv2.namedWindow(window_name, cv2.CV_WINDOW_AUTOSIZE)
cap = cv2.VideoCapture(cam_index)
cap.open(cam_index)
while True:
ret, frame = cap.read()
if frame is not None:
# First we do a threshold on our image
thresh = threshold(frame)
cv2.imshow(window_name, thresh)
cv2.imshow("Manual Dilate", manual_dilate(thresh))
cv2.imshow("Manual Erode", manual_erode(thresh))
k = cv2.waitKey(1) & 0xFF
if k == 27: # Escape key
cv2.destroyAllWindows()
cap.release()
break
if __name__ == "__main__":
main()