├── lena512.jpg ├── lena1000p.jpg ├── compare_9x9.png ├── compare_13x13.png ├── Image_Filtering_LATran.pdf ├── README.md ├── Compare_Filters.py ├── Wiener_Filter.py └── Median_Filter.py /lena512.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tranleanh/wiener-median-comparison/HEAD/lena512.jpg -------------------------------------------------------------------------------- /lena1000p.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tranleanh/wiener-median-comparison/HEAD/lena1000p.jpg -------------------------------------------------------------------------------- /compare_9x9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tranleanh/wiener-median-comparison/HEAD/compare_9x9.png -------------------------------------------------------------------------------- /compare_13x13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tranleanh/wiener-median-comparison/HEAD/compare_13x13.png -------------------------------------------------------------------------------- /Image_Filtering_LATran.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tranleanh/wiener-median-comparison/HEAD/Image_Filtering_LATran.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Wiener-Median-Comparison 2 | A comparison of Median filter and Wiener filter in image restoration. 3 | The image corrupted by motion blurring and adding Gaussian noise was used to test in the program. Link to project: https://www.researchgate.net/publication/332574579_Image_Processing_Course_Project_Image_Filtering_with_Wiener_Filter_and_Median_Filter 4 | 5 | The results produced by running Compare_Filters.py: 6 | 7 | Comparison with the kernel size of 9x9: 8 | ![picture](compare_9x9.png) 9 | 10 | Comparison with the kernel size of 13x13: 11 | ![picture](compare_13x13.png) 12 | 13 | Tran Le Anh, April 22, 2019. 14 | -------------------------------------------------------------------------------- /Compare_Filters.py: -------------------------------------------------------------------------------- 1 | import os 2 | import matplotlib.pyplot as plt 3 | from Median_Filter import median_filter, add_gaussian_noise, rgb2gray 4 | from Wiener_Filter import blur, gaussian_kernel, wiener_filter 5 | 6 | if __name__ == '__main__': 7 | # Load image and convert it to gray scale 8 | file_name = os.path.join('lena1000p.jpg') 9 | img = rgb2gray(plt.imread(file_name)) 10 | 11 | # Blur image 12 | blurred_img = blur(img, kernel_size = 9) 13 | 14 | # Add Gaussian noise 15 | noisy_img = add_gaussian_noise(blurred_img, 20) 16 | 17 | # Apply Median Filter 18 | median_filter = median_filter(noisy_img, 9) 19 | 20 | # Apply Wiener Filter 21 | wiener_filter = wiener_filter(noisy_img, gaussian_kernel(9), K = 0.5) 22 | 23 | # Display results 24 | fig = plt.figure(figsize = (12, 10)) 25 | 26 | display = [img, noisy_img, median_filter, wiener_filter] 27 | title = ['Original image', 'Blurred + Gaussian Noise Added', 28 | 'Median Filter', 'Wiener Filter'] 29 | 30 | for i in range(len(display)): 31 | fig.add_subplot(2, 2, i+1) 32 | plt.imshow(display[i], cmap = 'gray') 33 | plt.title(title[i]) 34 | 35 | plt.show() -------------------------------------------------------------------------------- /Wiener_Filter.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | from numpy.fft import fft2, ifft2 4 | from scipy.signal import gaussian, convolve2d 5 | import matplotlib.pyplot as plt 6 | 7 | def blur(img, kernel_size = 3): 8 | dummy = np.copy(img) 9 | h = np.eye(kernel_size) / kernel_size 10 | dummy = convolve2d(dummy, h, mode = 'valid') 11 | return dummy 12 | 13 | def add_gaussian_noise(img, sigma): 14 | gauss = np.random.normal(0, sigma, np.shape(img)) 15 | noisy_img = img + gauss 16 | noisy_img[noisy_img < 0] = 0 17 | noisy_img[noisy_img > 255] = 255 18 | return noisy_img 19 | 20 | def wiener_filter(img, kernel, K): 21 | kernel /= np.sum(kernel) 22 | dummy = np.copy(img) 23 | dummy = fft2(dummy) 24 | kernel = fft2(kernel, s = img.shape) 25 | kernel = np.conj(kernel) / (np.abs(kernel) ** 2 + K) 26 | dummy = dummy * kernel 27 | dummy = np.abs(ifft2(dummy)) 28 | return dummy 29 | 30 | def gaussian_kernel(kernel_size = 3): 31 | h = gaussian(kernel_size, kernel_size / 3).reshape(kernel_size, 1) 32 | h = np.dot(h, h.transpose()) 33 | h /= np.sum(h) 34 | return h 35 | 36 | def rgb2gray(rgb): 37 | return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140]) 38 | 39 | 40 | if __name__ == '__main__': 41 | # Load image and convert it to gray scale 42 | file_name = os.path.join('lena1000p.jpg') 43 | img = rgb2gray(plt.imread(file_name)) 44 | 45 | # Blur the image 46 | blurred_img = blur(img, kernel_size = 7) 47 | 48 | # Add Gaussian noise 49 | noisy_img = add_gaussian_noise(blurred_img, sigma = 20) 50 | 51 | # Apply Wiener Filter 52 | kernel = gaussian_kernel(5) 53 | filtered_img = wiener_filter(noisy_img, kernel, K = 10) 54 | 55 | # Display results 56 | display = [img, blurred_img, noisy_img, filtered_img] 57 | label = ['Original Image', 'Motion Blurred Image', 'Motion Blurring + Gaussian Noise', 'Wiener Filter applied'] 58 | 59 | fig = plt.figure(figsize=(12, 10)) 60 | 61 | for i in range(len(display)): 62 | fig.add_subplot(2, 2, i+1) 63 | plt.imshow(display[i], cmap = 'gray') 64 | plt.title(label[i]) 65 | 66 | plt.show() -------------------------------------------------------------------------------- /Median_Filter.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | def median_filter(data, kernel_size): 6 | temp = [] 7 | indexer = kernel_size // 2 8 | data_final = [] 9 | data_final = np.zeros((len(data),len(data[0]))) 10 | for i in range(len(data)): 11 | 12 | for j in range(len(data[0])): 13 | 14 | for z in range(kernel_size): 15 | if i + z - indexer < 0 or i + z - indexer > len(data) - 1: 16 | for c in range(kernel_size): 17 | temp.append(0) 18 | else: 19 | if j + z - indexer < 0 or j + indexer > len(data[0]) - 1: 20 | temp.append(0) 21 | else: 22 | for k in range(kernel_size): 23 | temp.append(data[i + z - indexer][j + k - indexer]) 24 | 25 | temp.sort() 26 | data_final[i][j] = temp[len(temp) // 2] 27 | temp = [] 28 | return data_final 29 | 30 | def add_gaussian_noise(img, sigma): 31 | gauss = np.random.normal(0, sigma, np.shape(img)) 32 | noisy_img = img + gauss 33 | noisy_img[noisy_img < 0] = 0 34 | noisy_img[noisy_img > 255] = 255 35 | return noisy_img 36 | 37 | def rgb2gray(rgb): 38 | return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140]) 39 | 40 | 41 | if __name__ == '__main__': 42 | # Load image and convert it to gray scale 43 | file_name = os.path.join('lena1000p.jpg') 44 | img = rgb2gray(plt.imread(file_name)) 45 | 46 | # Add Gaussian noise 47 | noisy_img = add_gaussian_noise(img, 30) 48 | 49 | # Apply Median Filter 50 | removed_noise_3 = median_filter(noisy_img, 3) 51 | removed_noise_5 = median_filter(noisy_img, 5) 52 | 53 | # Display results 54 | fig = plt.figure(figsize = (12, 10)) 55 | display = [img, noisy_img, removed_noise_3, removed_noise_5] 56 | title = ['Original Image', 'Gaussian Noise Added', '3x3 Median Filter', '5x5 Median Filter'] 57 | 58 | for i in range(len(display)): 59 | fig.add_subplot(2, 2, i+1) 60 | plt.imshow(display[i], cmap = 'gray') 61 | plt.title(title[i]) 62 | 63 | plt.show() --------------------------------------------------------------------------------