Computer Vision: Gaussian Filter from Scratch.

Ashish Kumar
Mar 17, 2019 · 3 min read

Gaussian Filter is used in reducing noise in the image and also the details of the image. Gaussian Filter is always preferred compared to the Box Filter.

Steps involved in implementing Gaussian Filter from Scratch on an image:

  1. Defining the convolution function which iterates over the image based on the kernel size(Gaussian filter). In the figure below left image represent the old image with the red box as the kernel calculating the value from all the nine pixels and inserting in the center pixel.
def convolution(oldimage, kernel):
#image = Image.fromarray(image, 'RGB')
image_h = oldimage.shape[0]
image_w = oldimage.shape[1]


kernel_h = kernel.shape[0]
kernel_w = kernel.shape[1]

if(len(oldimage.shape) == 3):
image_pad = np.pad(oldimage, pad_width=(
\(kernel_h // 2, kernel_h // 2),(kernel_w // 2,
\kernel_w // 2),(0,0)), mode='constant',
\constant_values=0).astype(np.float32)
elif(len(oldimage.shape) == 2):
image_pad = np.pad(oldimage, pad_width=(
\(kernel_h // 2, kernel_h // 2),(kernel_w // 2,
\kernel_w // 2)), mode='constant', constant_values=0)
\.astype(np.float32)


h = kernel_h // 2
w = kernel_w // 2

image_conv = np.zeros(image_pad.shape)

for i in range(h, image_pad.shape[0]-h):
for j in range(w, image_pad.shape[1]-w):
#sum = 0
x = image_pad[i-h:i-h+kernel_h, j-w:j-w+kernel_w]
x = x.flatten()*kernel.flatten()
image_conv[i][j] = x.sum()
h_end = -h
w_end = -w

if(h == 0):
return image_conv[h:,w:w_end]
if(w == 0):
return image_conv[h:h_end,w:]
return image_conv[h:h_end,w:w_end]

2. Defining the Gaussian function based on the size of sigma(standard deviation).

Formula:

def GaussianBlurImage(image, sigma):
#image = imread(image)
image = Image.open(image)
image = np.asarray(image)
#print(image)
filter_size = 2 * int(4 * sigma + 0.5) + 1
gaussian_filter = np.zeros((filter_size, filter_size), np.float32)
m = filter_size//2
n = filter_size//2

for x in range(-m, m+1):
for y in range(-n, n+1):
x1 = 2*np.pi*(sigma**2)
x2 = np.exp(-(x**2 + y**2)/(2* sigma**2))
gaussian_filter[x+m, y+n] = (1/x1)*x2

im_filtered = np.zeros_like(image, dtype=np.float32)
for c in range(3):
im_filtered[:, :, c] = convolution(image[:, :, c], gaussian_filter)
return (im_filtered.astype(np.uint8))

Original Image:

Final Output Image after applying Gaussian Filter:

Thanks for reading.

Ashish Kumar

Written by

Computer Science graduate student. Machine learning & Data Science enthusiast. https://github.com/Ashi-s

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade