Created
August 22, 2013 21:12
-
-
Save lizecillie/6312844 to your computer and use it in GitHub Desktop.
Masks
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,color, img_as_float | |
def averaging_mask(A, mask_size): | |
rows = np.shape(A)[0] | |
columns = np.shape(A)[1] | |
half_length = int((mask_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) | |
mask = np.ones((mask_size, mask_size))/(mask_size**2) | |
B = np.zeros_like(A) | |
for i in xrange(0, rows): | |
for j in xrange(0, columns): | |
B[i, j] = (np.multiply(mask, pad_A[i:mask_size + i, j:mask_size + j]))[half_length, half_length] | |
print B[i, j] | |
return B | |
def high_boost_filtering(A, k): | |
smoothed = averaging_mask(A, mask_size) | |
mask = A - smoothed | |
sharpened = A + k*mask | |
return sharpened | |
def high_boost_filtering_single_mask(A, mask_size, k): | |
rows = np.shape(A)[0] | |
columns = np.shape(A)[1] | |
half_length = int((mask_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) | |
I = np.zeros((mask_size, mask_size)) | |
I[half_length, half_length] = 1 | |
average = np.ones((mask_size, mask_size))/(mask_size**2) | |
mask = I + k*(I - average) | |
sharpened = np.zeros_like(A) | |
for i in xrange(0, rows): | |
for j in xrange(0, columns): | |
sharpened[i, j] = np.multiply(mask, pad_A[i:mask_size + i, j:mask_size + j])[half_length, half_length] | |
return sharpened | |
A = io.imread('manwithspecs.jpg', plugin='pil') | |
B = color.rgb2gray(io.imread('moon.jpg', plugin='pil')) | |
mask_size = 17 | |
out = averaging_mask(A, mask_size) | |
k = 2 | |
out1 = high_boost_filtering(A, k) | |
out2 = high_boost_filtering_single_mask(A, mask_size, k) | |
print out1==out2 | |
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('Image smoothed using a averaging mask, mask size = ') | |
f1, (ax0, ax1) = plt.subplots(1, 2) | |
f1.tight_layout() | |
ax0.imshow(A, cmap=plt.cm.gray) | |
ax0.set_title('Input image') | |
ax1.imshow(out1, cmap=plt.cm.gray) | |
ax1.set_title('Image smoothed using high boost filtering, mask size = , k = ') | |
f2, (ax0, ax1) = plt.subplots(1, 2) | |
f2.tight_layout() | |
ax0.imshow(A, cmap=plt.cm.gray) | |
ax0.set_title('Input image') | |
ax1.imshow(out2, cmap=plt.cm.gray) | |
ax1.set_title('Image smoothed by a single mask convolution, mask size = , k = ') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment