Skip to content

Commit 6bed5d9

Browse files
committed
05-Spatial-filters
1 parent 4758d8e commit 6bed5d9

File tree

1 file changed

+353
-0
lines changed

1 file changed

+353
-0
lines changed

05-Spatial-filters.ipynb

+353
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,353 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Spatial Filtering\n",
8+
"\n",
9+
"**Spatial Filtering** technique is used directly on pixels of an image. Mask is usually considered to be added in size so that it has specific center pixel. This mask is moved on the image such that the center of the mask traverses all image pixels.\n",
10+
"\n",
11+
"1. Linear Spatial Filter\n",
12+
"2. Non-linear Spatial Filter\n",
13+
"\n",
14+
"![Image filtering 1](dataset/imgs/spatial1.jpg)\n"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"**Smoothing Spatial Filter:** Smoothing filter is used for blurring and noise reduction in the image. Blurring is pre-processing steps for removal of small details and Noise Reduction is accomplished by blurring.\n",
22+
"\n",
23+
"Types of Smoothing Spatial Filter:\n",
24+
"\n",
25+
"1. Linear Filter (Mean Filter)\n",
26+
"2. Order Statistics (Non-linear) filter \n",
27+
"\n",
28+
"These are explained as following below.\n",
29+
"\n",
30+
"**Mean Filter:**\n",
31+
"\n",
32+
"Linear spatial filter is simply the average of the pixels contained in the neighborhood of the filter mask. The idea is replacing the value of every pixel in an image by the average of the grey levels in the neighborhood define by the filter mask.\n",
33+
"\n",
34+
"Types of Mean filter:\n",
35+
"\n",
36+
"> *(i) Averaging filter:* It is used in reduction of the detail in image. All coefficients are equal.\n",
37+
"\n",
38+
"\n",
39+
"![Averaging](dataset/imgs/spatial2.jpg)\n",
40+
"\n",
41+
"\n",
42+
"> *(ii) Weighted averaging filter:* In this, pixels are multiplied by different coefficients. Center pixel is multiplied by a higher value than average filter.\n",
43+
"\n",
44+
"\n",
45+
"**Order Statistics Filter:**\n",
46+
"\n",
47+
"It is based on the ordering the pixels contained in the image area encompassed by the filter. It replaces the value of the center pixel with the value determined by the ranking result. Edges are better preserved in this filtering.\n",
48+
"\n",
49+
"Types of Order statistics filter:\n",
50+
"\n",
51+
"> *(i) Minimum filter:* 0th percentile filter is the minimum filter. The value of the center is replaced by the smallest value in the window.\n",
52+
"\n",
53+
"> *(ii) Maximum filter:* 100th percentile filter is the maximum filter. The value of the center is replaced by the largest value in the window.\n",
54+
"\n",
55+
"> *(iii) Median filter:* Each pixel in the image is considered. First neighboring pixels are sorted and original values of the pixel is replaced by the median of the list."
56+
]
57+
},
58+
{
59+
"cell_type": "markdown",
60+
"metadata": {},
61+
"source": [
62+
"## Image Convolution\n",
63+
"\n",
64+
"![image convolution gif](dataset/imgs/spatial3.jpg)\n",
65+
"\n",
66+
"\n",
67+
"**How one performs convolution on an image?**\n",
68+
"\n",
69+
"![Convolution of image](dataset/imgs/spatial4.jpg)\n",
70+
"\n",
71+
"And how do the values are computed? Here is an example...\n",
72+
"\n",
73+
"![Convolution of image, arithmetics](dataset/imgs/spatial5.gif)"
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"metadata": {},
79+
"source": [
80+
"What if the image is multi-channel?\n",
81+
"\n",
82+
"So, the kernel would also be a multi-channel kernel, and the convolution will occur on all channels. This is how it is demostrated.\n",
83+
"\n",
84+
"![multi-channel convolution](dataset/imgs/spatial8.jpg)\n",
85+
"\n",
86+
"Now, look at how it moves in animation:\n",
87+
"\n",
88+
"![Multi-channel convolution](dataset/imgs/spatial6.gif)\n",
89+
"\n",
90+
"\n",
91+
"![image sharpening](dataset/imgs/spatial7.png)"
92+
]
93+
},
94+
{
95+
"cell_type": "markdown",
96+
"metadata": {},
97+
"source": [
98+
"## Convolution Operation\n",
99+
"\n",
100+
"### scipy.signal.convolve2d()"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 5,
106+
"metadata": {},
107+
"outputs": [
108+
{
109+
"name": "stdout",
110+
"output_type": "stream",
111+
"text": [
112+
"[[184 184 201 241 267 267]\n",
113+
" [184 184 201 241 267 267]\n",
114+
" [143 143 209 272 252 252]\n",
115+
" [161 161 205 260 29 29]\n",
116+
" [158 158 255 224 166 166]\n",
117+
" [158 158 255 224 166 166]]\n"
118+
]
119+
}
120+
],
121+
"source": [
122+
"from scipy import signal\n",
123+
"\n",
124+
"\n",
125+
"# for example \n",
126+
"kernel = [\n",
127+
" [0, -1, 0],\n",
128+
" [-1, 5, -1],\n",
129+
" [0, -1, 0]\n",
130+
"]\n",
131+
"\n",
132+
"img = [\n",
133+
" [190, 206, 228, 238],\n",
134+
" [180, 205, 227, 219],\n",
135+
" [182, 203, 211, 159],\n",
136+
" [184, 212, 206, 177]\n",
137+
"]\n",
138+
"\n",
139+
"print(signal.convolve2d(img, kernel, boundary=\"symm\"))\n"
140+
]
141+
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": 6,
145+
"metadata": {},
146+
"outputs": [
147+
{
148+
"name": "stdout",
149+
"output_type": "stream",
150+
"text": [
151+
"[[208.3231 208.3231 202.413 198.5626 198.1941 198.1941]\n",
152+
" [208.3231 208.3231 202.413 198.5626 198.1941 198.1941]\n",
153+
" [220.6117 220.6117 214.8457 208.0947 213.8241 213.8241]\n",
154+
" [227.9516 227.9516 218.6319 221.3496 230.4079 230.4079]\n",
155+
" [228.1421 228.1421 215.5618 230.1563 235.5418 235.5418]\n",
156+
" [228.1421 228.1421 215.5618 230.1563 235.5418 235.5418]]\n"
157+
]
158+
}
159+
],
160+
"source": [
161+
"from scipy import signal\n",
162+
"\n",
163+
"\n",
164+
"gaussian_kernel = [\n",
165+
" [0.0116, 0.0861, 0.0116],\n",
166+
" [0.0861, 0.6366, 0.0861],\n",
167+
" [0.0116, 0.0861, 0.0116]\n",
168+
"]\n",
169+
"\n",
170+
"img_matrix = [\n",
171+
" [202, 195, 192, 191],\n",
172+
" [216, 211, 200, 209],\n",
173+
" [224, 212, 215, 227],\n",
174+
" [224, 205, 227, 230]\n",
175+
"]\n",
176+
"\n",
177+
"\n",
178+
"print(signal.convolve2d(img_matrix, gaussian_kernel, boundary=\"symm\"))"
179+
]
180+
},
181+
{
182+
"cell_type": "markdown",
183+
"metadata": {},
184+
"source": [
185+
"## Median Filter"
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": 56,
191+
"metadata": {},
192+
"outputs": [
193+
{
194+
"data": {
195+
"text/plain": [
196+
"array([[32, 32, 32, 32],\n",
197+
" [23, 32, 32, 32],\n",
198+
" [32, 44, 32, 23],\n",
199+
" [44, 44, 23, 23],\n",
200+
" [44, 44, 23, 23]])"
201+
]
202+
},
203+
"execution_count": 56,
204+
"metadata": {},
205+
"output_type": "execute_result"
206+
}
207+
],
208+
"source": [
209+
"from scipy.ndimage import median_filter\n",
210+
"\n",
211+
"img = [\n",
212+
" [23, 51, 23, 32],\n",
213+
" [32, 44, 44, 23],\n",
214+
" [23, 23, 44, 32],\n",
215+
" [44, 44, 23, 23],\n",
216+
" [44, 44, 23, 23]\n",
217+
"]\n",
218+
"\n",
219+
"\n",
220+
"# Median Spatial Domain Filtering\n",
221+
"median_filter(img, size=3, )"
222+
]
223+
},
224+
{
225+
"cell_type": "markdown",
226+
"metadata": {},
227+
"source": [
228+
"## Gaussian Filter"
229+
]
230+
},
231+
{
232+
"cell_type": "code",
233+
"execution_count": 52,
234+
"metadata": {},
235+
"outputs": [
236+
{
237+
"name": "stdout",
238+
"output_type": "stream",
239+
"text": [
240+
"[[202 196 192 192]\n",
241+
" [214 208 201 208]\n",
242+
" [221 212 214 223]\n",
243+
" [221 209 223 228]]\n"
244+
]
245+
}
246+
],
247+
"source": [
248+
"from scipy.ndimage import gaussian_filter\n",
249+
"\n",
250+
"\n",
251+
"img_matrix = [\n",
252+
" [202, 195, 192, 191],\n",
253+
" [216, 211, 200, 209],\n",
254+
" [224, 212, 215, 227],\n",
255+
" [224, 205, 227, 230]\n",
256+
"]\n",
257+
"\n",
258+
"print(gaussian_filter(img_matrix, sigma=0.5))\n",
259+
"\n",
260+
"\n",
261+
"# gaussian kernel for sigma = 0.5 is:\n",
262+
"# gaussian_kernel = [\n",
263+
"# [0.0116, 0.0861, 0.0116],\n",
264+
"# [0.0861, 0.6366, 0.0861],\n",
265+
"# [0.0116, 0.0861, 0.0116]\n",
266+
"# ]\n"
267+
]
268+
},
269+
{
270+
"cell_type": "markdown",
271+
"metadata": {},
272+
"source": [
273+
"## Mean Filter"
274+
]
275+
},
276+
{
277+
"cell_type": "code",
278+
"execution_count": 55,
279+
"metadata": {},
280+
"outputs": [
281+
{
282+
"name": "stdout",
283+
"output_type": "stream",
284+
"text": [
285+
"[[ 0 0 0 0]\n",
286+
" [ 0 207 205 0]\n",
287+
" [ 0 214 215 0]\n",
288+
" [ 0 0 0 0]]\n"
289+
]
290+
}
291+
],
292+
"source": [
293+
"import numpy as np\n",
294+
" \n",
295+
"\n",
296+
"def mean_filter(img, mask_size):\n",
297+
"\n",
298+
" row, col = len(img), len(img[0])\n",
299+
"\n",
300+
" # Develop Averaging filter mask\n",
301+
" mask = np.ones([mask_size, mask_size], dtype = int)\n",
302+
" mask = mask / 9\n",
303+
"\n",
304+
" # Convolve the mask over the image\n",
305+
" img_new = np.zeros([row, col])\n",
306+
" \n",
307+
" for i in range(1, row-1):\n",
308+
" for j in range(1, col-1):\n",
309+
" temp = img[i-1, j-1]*mask[0, 0]+img[i-1, j]*mask[0, 1]+img[i-1, j + 1]*mask[0, 2]+img[i, j-1]*mask[1, 0]+ img[i, j]*mask[1, 1]+img[i, j + 1]*mask[1, 2]+img[i + 1, j-1]*mask[2, 0]+img[i + 1, j]*mask[2, 1]+img[i + 1, j + 1]*mask[2, 2]\n",
310+
" img_new[i, j]= temp\n",
311+
" \n",
312+
" img_new = img_new.astype(np.uint8)\n",
313+
" return img_new\n",
314+
"\n",
315+
"\n",
316+
"# for example\n",
317+
"img = [\n",
318+
" [202, 195, 192, 191],\n",
319+
" [216, 211, 200, 209],\n",
320+
" [224, 212, 215, 227],\n",
321+
" [224, 205, 227, 230]\n",
322+
"]\n",
323+
"img = np.array(img)\n",
324+
"\n",
325+
"print(mean_filter(img, 3))"
326+
]
327+
}
328+
],
329+
"metadata": {
330+
"interpreter": {
331+
"hash": "4e45770021c82b83c6a56dde8a63f999991416ec1b91001cec6caa452825c2d3"
332+
},
333+
"kernelspec": {
334+
"display_name": "Python 3 (ipykernel)",
335+
"language": "python",
336+
"name": "python3"
337+
},
338+
"language_info": {
339+
"codemirror_mode": {
340+
"name": "ipython",
341+
"version": 3
342+
},
343+
"file_extension": ".py",
344+
"mimetype": "text/x-python",
345+
"name": "python",
346+
"nbconvert_exporter": "python",
347+
"pygments_lexer": "ipython3",
348+
"version": "3.10.3"
349+
}
350+
},
351+
"nbformat": 4,
352+
"nbformat_minor": 2
353+
}

0 commit comments

Comments
 (0)