In [1]:
# manipulate arrays and have math
import numpy as np
# plot results
import matplotlib.pyplot as plt
# plot them inline in jupyter
%matplotlib inline
# make matplotlib load images
import matplotlib.image as mpimg
In [2]:
sharp_image = mpimg.imread('picture.png')
soft_image = mpimg.imread('picture_gaussian_10.png')
In [3]:
plt.imshow(sharp_image)
print 'sharp image'
plt.show()
plt.imshow(soft_image)
print 'soft image'
plt.show()
In [4]:
sharp_test = sharp_image[1000:1400,2000:2400].copy()
soft_test = soft_image[1000:1400,2000:2400].copy()
In [5]:
plt.imshow(sharp_test)
print 'sharp image'
plt.show()
plt.imshow(soft_test)
print 'soft image'
plt.show()
In [6]:
sharp_test_noise = sharp_image[1000:1400,2000:2400].copy()
soft_test_noise = soft_image[1005:1405,2012:2412].copy()
In [7]:
# add noise
def noise(data, noise_value):
noisy_data = data + noise_value * np.random.random([400,400,3]) - noise_value/2
# shift values to be within bounds
tmp_data = noisy_data.copy()
noisy_data[tmp_data < 0] = np.abs(tmp_data[tmp_data < 0])
noisy_data[tmp_data > 1] = -tmp_data[tmp_data > 1] + 2
return noisy_data
In [8]:
sharp_test_noise = noise(sharp_test_noise, 0.2)
soft_test_noise = noise(soft_test_noise, 0.2)
plt.imshow(sharp_test_noise)
plt.show()
plt.imshow(soft_test_noise)
plt.show()
In [9]:
# compute a variance measure that should prefer a contrasty result, thus a sharper one
def variance_sharpness_metric(data):
# compute average from one colour channel (greyscale, remember)
average = np.mean(data[:,:,0])
# look at variance (yes, there is a np.var call as well)
variance = (data - average)**2
# take mean over all pixels
metric1 = np.mean(variance)
# compare to the built-in one
metric2 = np.mean(np.var(data[:,:,0]))
return metric1, metric2
In [10]:
print ' metric (sharp) :', variance_sharpness_metric(sharp_test)
print ' metric (soft) :', variance_sharpness_metric(soft_test)
In [11]:
print ' metric (sharp) :', variance_sharpness_metric(sharp_test_noise)
print ' metric (soft) :', variance_sharpness_metric(soft_test_noise)
In [12]:
print ' ratio/cleanliness of results:', \
variance_sharpness_metric(sharp_test_noise)[0]/variance_sharpness_metric(soft_test_noise)[0]
In [ ]: