Created
August 17, 2013 20:07
-
-
Save lizecillie/6258496 to your computer and use it in GitHub Desktop.
Median filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from skimage import io, img_as_float | |
def salt_and_pepper_noise_gray(A, d): | |
rows = np.shape(A)[0] | |
columns = np.shape(A)[1] | |
number_of_pixels = int(d*rows*columns) | |
x = columns*np.random.random((number_of_pixels)) | |
y = rows*np.random.random((number_of_pixels)) | |
coordinates = np.column_stack((x, y)) | |
for i in coordinates[:len(coordinates)/2]: | |
A[int(i[1]), int(i[0])] = 0 | |
for i in coordinates[len(coordinates)/2:]: | |
A[int(i[1]), int(i[0])] = 1 | |
return A | |
def salt_and_pepper_noise_colour(A, d): | |
A[:, :, 0] = salt_and_pepper_noise_gray(A[:, :, 0], d) | |
A[:, :, 1] = salt_and_pepper_noise_gray(A[:, :, 1], d) | |
A[:, :, 2] = salt_and_pepper_noise_gray(A[:, :, 2], d) | |
return A | |
def median_filter(A, filter_size): | |
rows = np.shape(A)[0] | |
columns = np.shape(A)[1] | |
half_length = int((filter_size-1)/2) | |
A = salt_and_pepper_noise_colour(A, d) | |
pad_A = np.concatenate([np.zeros((rows, half_length, 3)), A, np.zeros((rows, half_length, 3))], axis=1) | |
pad_A = np.concatenate([np.zeros((half_length, columns+(2*half_length), 3)), pad_A, np.zeros((half_length, columns+(2*half_length), 3))], axis=0) | |
B = np.zeros_like(A) | |
c2 = 0 | |
c1 = 0 | |
k = 0 | |
for i in xrange(0, rows): | |
for j in xrange(0, columns): | |
B[i, j, k] = np.median(pad_A[c1:filter_size + c1, c2:filter_size + c2, k]) | |
c2 += 1 | |
c2 = 0 | |
c1 += 1 | |
k += 1 | |
return B | |
A = io.imread('lugbalonne.jpg') | |
A = (0.3*A[:, :, 0] + 0.59*A[:, :, 1] + 0.11*A[:, :, 2])/256 | |
G = np.empty_like(A) | |
G[:] = A | |
d = 0.2 | |
out = salt_and_pepper_noise_gray(A, d) | |
B = io.imread('meisie.jpg') | |
B = img_as_float(B) | |
C = np.empty_like(B) | |
C[:] = B | |
d = 0.2 | |
out1 = salt_and_pepper_noise_colour(B, d) | |
filter_size = 5 | |
out2 = median_filter(B, filter_size) | |
f, (ax0, ax1) = plt.subplots(1, 2) | |
f.tight_layout() | |
ax0.imshow(G, cmap=plt.cm.gray) | |
ax0.set_title('Input image') | |
ax1.imshow(out, cmap=plt.cm.gray) | |
ax1.set_title('Salt and pepper noise, d = 0.2') | |
f, (ax0, ax1) = plt.subplots(1, 2) | |
f.tight_layout() | |
ax0.imshow(C) | |
ax0.set_title('Input image') | |
ax1.imshow(out1) | |
ax1.set_title('Salt and pepper noise, d = 0.2') | |
f, (ax0, ax1) = plt.subplots(1, 2) | |
f.tight_layout() | |
ax0.imshow(out1) | |
ax0.set_title('Input image, d = 0.2') | |
ax1.imshow(out2) | |
ax1.set_title('Median filter, filter size = 5') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment