Skip to content

Commit 0de52b9

Browse files
committed
add second example on mean filtering
1 parent 0a32688 commit 0de52b9

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

edgedetect/kernel.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,56 @@ mean_blur = np.array(
7070

7171
2. To be fully convinced that the mean filtering operation is doing what we expect it to do, we can inspect the pixel values before- and after- the convolution, to verify that the math checks out by hand. We do this in `meanblur_02.py`.
7272

73+
```py
74+
img = cv2.imread("assets/canal.png")
75+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
76+
print(f'Gray: {gray[:5, :5]}')
77+
# [[ 31 27 21 17 21]
78+
# [ 77 85 86 87 90]
79+
# [205 205 215 227 222]
80+
# [224 230 222 243 249]
81+
# [138 210 206 218 242]]
82+
for i in range(3):
83+
newval = np.round(np.mean(gray[:5, i:i+5]))
84+
print(f'Mean of 25x25 pixel #{i+1}: {np.int(newval)}')
85+
# output:
86+
# Mean of 25x25 pixel #1: 152
87+
# Mean of 25x25 pixel #2: 158
88+
# Mean of 25x25 pixel #3: 160
89+
#
90+
```
91+
The code above shows that the output of such a convolution operation beginning at the top-left region of the image would be 152. As we slide along the horizontal direction and re-compute the mean of the neighborhood, we get 158. As we slide our kernel along the horizontal direction for a second time and re-compute the mean of the neighborhood we obtain the value of 160.
92+
93+
If you prefer you can verify these values by hand, using the raw pixel values from `gray[:5, :5]` (5x5 top-left region of the image).
94+
95+
```py
96+
mean_blur = np.ones(KERNEL_SIZE, dtype="float32") * (1.0 / (5 ** 2))
97+
smoothed_gray = cv2.filter2D(gray, -1, mean_blur)
98+
print(f'Smoothed: {smoothed_gray[:5, :5]}')
99+
# output:
100+
# [[122 123 125 127 128]
101+
# [126 127 128 131 132]
102+
# [148 149 152 158 160]
103+
# [177 179 184 196 202]
104+
# [197 199 204 222 229]]
105+
```
106+
Notice that from the output of our mean-filter, the first anchor (center of the neighborhood) has transformed from 215 to 152, and the one to the right of it has transformed from 227 to 158, and so on. The math does work out and you can observe the blur effect directly by running `meanblur02.py`.
107+
108+
3. As it turns out, `opencv` provides a set of convenience functions to apply filtering onto our images. All the three approaches below yield the same output, as can be verified from the output pixel values after executing `meanblur_03.py`:
109+
110+
```py
111+
# approach 1
112+
mean_blur = np.ones(KERNEL_SIZE, dtype="float32") * (1.0 / (5 ** 2))
113+
smoothed_gray = cv2.filter2D(gray, -1, mean_blur)
114+
115+
# approach 2
116+
smoothed_gray = cv2.blur(gray, KERNEL_SIZE)
117+
118+
# approach 3
119+
smoothed_gray = cv2.boxFilter(gray, -1, KERNEL_SIZE)
120+
```
121+
122+
73123

74124

75125
## Role in Convolutional Neural Network

edgedetect/meanblur_02.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
import cv2
3+
4+
KERNEL_SIZE = (5, 5)
5+
6+
img = cv2.imread("assets/canal.png")
7+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
8+
print(f'Gray: {gray[:5, :5]}')
9+
print(f'Shape of Original: {gray.shape}')
10+
11+
for i in range(3):
12+
newval = np.round(np.mean(gray[:5, i:i+5]))
13+
print(f'Mean of 25x25 pixel #{i+1}: {np.int(newval)}')
14+
15+
cv2.imshow("Gray", gray)
16+
cv2.waitKey(0)
17+
18+
mean_blur = np.ones(KERNEL_SIZE, dtype="float32") * (1.0 / (5 ** 2))
19+
smoothed_col = cv2.filter2D(img, -1, mean_blur)
20+
smoothed_gray = cv2.filter2D(gray, -1, mean_blur)
21+
22+
cv2.imshow("Smoothed Colored", smoothed_col)
23+
cv2.waitKey(0)
24+
25+
cv2.imshow("Smoothed Gray", smoothed_gray)
26+
cv2.waitKey(0)
27+
print(f'Smoothed: {smoothed_gray[:5, :5]}')
28+
print(f'Shape of Smoothed: {smoothed_gray.shape}')

0 commit comments

Comments
 (0)