How Image Filtering is used to Improve Picture Quality

Gaussian Blur

Box Blur

import numpy as npimport cv2from matplotlib import pyplot as pltimg = cv2.imread("outdoor.jpg", -1);blurred = np.uint8(cv2.boxFilter(img, cv2.CV_64F, ksize=(5, 5), normalize=True));f = plt.figure(figsize=(15,15));f.add_subplot(1, 2, 1).set_title('Original Image');plt.imshow(img[:,:,::-1])f.add_subplot(1, 2, 2).set_title('Blurred Image');plt.imshow(blurred[:,:,::-1]);;

Min and Max Filters

import numpy as npfrom PIL import Image, ImageFilterfrom matplotlib import pyplot as pltimg ="indoor.jpg");size = 5;blurred = img.filter(ImageFilter.MinFilter(size=size));f = plt.figure(figsize=(15,15))f.add_subplot(1, 2, 1).set_title('Original Image');plt.imshow(img)f.add_subplot(1, 2, 2).set_title('Blurred Image');plt.imshow(blurred);

Median and Mode Filtering

import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread(“imgs/chapter5/outdoor.jpg”, -1);
blurred = cv2.medianBlur(img, 5);
f = plt.figure(figsize=(15,15);
f.add_subplot(1, 2, 1).set_title('Original Image');
f.add_subplot(1, 2, 2).set_title('Blurred Image');
img = img.filter(ImageFilter.ModeFilter(size=size));

Use Case: Pepper and Salt Noise Removal

import numpy as npimport randomimport cv2from matplotlib import pyplot as pltfrom PIL import Image, ImageFilter#Function to add salt and pepper noise to the imagedef sp_noise(image,prob):output = np.zeros(image.shape,np.uint8)thres = 1 - probfor i in range(image.shape[0]):for j in range(image.shape[1]):rdn = random.random()if rdn < prob:output[i][j] = [0, 0, 0]elif rdn > thres:output[i][j] = [255, 255, 255]else:output[i][j] = image[i][j]return outputimg = cv2.imread('outdoor.jpg',1) noise_img = sp_noise(image,0.05)#box filterkernel_size=5;box_filtered = cv2.blur(img, (kernel_size, kernel_size));# max filtertemp = Image.fromarray(noise_img, mode="RGB");max_filtered = temp.filter(ImageFilter.MinFilter(size=kernel_size));# min filtertemp = Image.fromarray(noise_img, mode="RGB");min_filtered = temp.filter(ImageFilter.MaxFilter(size=kernel_size));# median filtermedian_filtered = cv2.medianBlur(img, kernel_size);# mode filtertemp = Image.fromarray(noise_img, mode="RGB");
mode_filtered = temp.filter(ImageFilter.ModeFilter(size=kernel_size));
f = plt.figure(figsize=(15,15))f.add_subplot(4, 2, 1).set_title('Original Image');
plt.imshow(image[:,:,::-1]);f.add_subplot(4, 2, 2).set_title('Noisy Image');
plt.imshow(noise_img[:,:,::-1]);f.add_subplot(4, 2, 3).set_title('Box Filtered Image');
plt.imshow(box_filtered[:,:,::-1]);f.add_subplot(4, 2, 4).set_title('Min Filtered Image');
plt.imshow(min_filtered);f.add_subplot(4, 2, 5).set_title('Max Filtered Image');
plt.imshow(max_filtered);f.add_subplot(4, 2, 6).set_title('Median Filtered Image');
plt.imshow(median_filtered[:,:,::-1]);f.add_subplot(4, 2, 7).set_title('Mode Filtered Image');




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store