You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: edgedetect/kernel.md
+50Lines changed: 50 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -70,6 +70,56 @@ mean_blur = np.array(
70
70
71
71
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`.
72
72
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 inrange(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).
Notice that from the output of our mean-filter, the first anchor (center of the neighborhood) has transformed from215 to 152, and the one to the right of it has transformed from227 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`:
0 commit comments