Play with some images. I want to make a point about the meaning of 'resolution'.
Let's say that these properties are the 3 horsemen of resolution.
import numpy as np
#from numpy.fft import fft, fftfreq
from scipy.fftpack import fft, rfft, irfft, fftfreq, rfftfreq
import scipy.signal
import matplotlib.pyplot as plt
%matplotlib inline
import scipy.ndimage
im = np.zeros((20, 20))
im[5:-5, 5:-5] = 1
im = scipy.ndimage.distance_transform_bf(im)
im_noise = im + 0.2 * np.random.randn(*im.shape)
im_med = scipy.ndimage.median_filter(im_noise, 3)
plt.imshow(im_med, interpolation="none", cmap="Greys")
plt.show()
Start by reading the red channel of an astronomical photograph from a file.
m32 = scipy.ndimage.imread("m32.jpg")[...,0]
m32.shape
plt.imshow(m32, interpolation="none", cmap="gray")
m32_med = scipy.ndimage.median_filter(m32, 5)
plt.imshow(m32_med, interpolation="none", cmap="Greys")
plt.show()
m32_gss = scipy.ndimage.gaussian_filter(m32, 5)
plt.imshow(m32_gss, interpolation="none", cmap="Greys")
plt.show()
m32.shape
m32
Let's look for another image
import requests
from StringIO import StringIO
from PIL import Image
a = requests.get("http://svs.gsfc.nasa.gov/vis/a010000/a010400/a010485/M31_Wide.jpg")
m31 = np.array(Image.open(StringIO(a.content))) # ndimage can't work with this object
plt.imshow(m31)
m32 = m31[5200:6000,3700:4500,0]
plt.imshow(m32, interpolation="none", cmap="gray")
plt.plot(m32[400,:])
def index(a, colours=8):
b = a.reshape(a.size)
b = float(colours) * b / np.amax(b)
bins = np.linspace(0, colours-1, colours)
c = np.digitize(b, bins)
return c.reshape(a.shape)
quantized = index(m32, 8)
plt.figure(figsize=(12,5))
plt.subplot(121)
plt.imshow(quantized)
plt.hlines(400,0,880,'red')
plt.subplot(122)
plt.plot(quantized[400,:], 'red')
plt.show()
We can attack it in the signal:noise space:
noise = np.random.random_integers(0, 255, m32.shape) # The image is 8-bit int.
m32_nse = m32 + noise
plt.figure(figsize=(15,8))
plt.subplot(121)
plt.imshow(m32, cmap="gray", interpolation="none")
plt.subplot(122)
plt.imshow(m32_nse, cmap="gray", interpolation="none")
plt.show()