Created
August 17, 2013 22:08
-
-
Save lizecillie/6258921 to your computer and use it in GitHub Desktop.
Unsharp masking
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 numpy as np | |
import matplotlib.pyplot as plt | |
from skimage import io, img_as_float | |
def median_filter(A, filter_size): | |
rows = np.shape(A)[0] | |
columns = np.shape(A)[1] | |
half_length = int((filter_size-1)/2) | |
pad_A = np.concatenate([np.zeros((rows, half_length)), A, np.zeros((rows, half_length))], axis=1) | |
pad_A = np.concatenate([np.zeros((half_length, columns+2*half_length)), pad_A, np.zeros((half_length, columns+2*half_length))], axis=0) | |
B = np.zeros_like(A) | |
for i in xrange(0, rows): | |
for j in xrange(0, columns): | |
B[i, j] = np.median(pad_A[i:filter_size + i, j:filter_size + j]) | |
return B | |
def unsharp_masking(A): | |
smoothed = median_filter(A, 3) | |
mask = A - smoothed | |
sharpened = A + mask | |
return sharpened | |
A = io.imread('wrinkled_face.jpg', plugin='pil') | |
A = 0.3*A[:, :, 0] + 0.59*A[:, :, 1] + 0.11*A[:, :, 2] | |
out = unsharp_masking(A) | |
f, (ax0, ax1) = plt.subplots(1, 2) | |
f.tight_layout() | |
ax0.imshow(A, cmap=plt.cm.gray) | |
ax0.set_title('Input Image') | |
ax1.imshow(out, cmap=plt.cm.gray) | |
ax1.set_title('Unsharp masking applied to input image, filter size = 5') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment