Objectives:
The Sobel operator is given by $$\mathbf{G} = \sqrt{ {\mathbf{G}_x}^2 + {\mathbf{G}_y}^2 }$$ where both images are respectively obtained by a convolution with the given kernels $$\mathbf{g}_y = \begin{bmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ +1 & +2 & +1 \end{bmatrix} \quad \mbox{and} \quad \mathbf{g}_x = \begin{bmatrix} -1 & 0 & +1 \\ -2 & 0 & +2 \\ -1 & 0 & +1 \end{bmatrix} $$
%matplotlib inline
from skimage.io import imread
import numpy as np
from numpy.fft import fft2,ifft2,fftshift,ifftshift
import matplotlib.pyplot as plt
im = imread('http://homepages.ulb.ac.be/~odebeir/data/moire1.png').astype(np.float)
f = fft2(im)
power = fftshift(np.abs(f)**2)
pmax = np.max(power)
plt.figure(figsize=[10,5])
plt.subplot(1,2,1)
plt.imshow(im,cmap = plt.cm.gray)
plt.title('$f(x,y)$');
plt.subplot(1,2,2)
plt.imshow(power,vmin=0,vmax=pmax/5000.);
plt.title('$|F(u,v)|^2$');
Starting from the example given above:
from skimage.data import astronaut
rgb = astronaut()
print(rgb.shape) #this is a 3D image 2 spatial dim. + the spectral dim.
r = rgb[:,:,0]
plt.imshow(r,cmap=plt.cm.gray);
(512, 512, 3)
import skimage.filters.rank as skr
from skimage.morphology import disk
# example code to add salt and pepper noise to an image
ima = astronaut()[:,:,0]
n = np.random.random(ima.shape)
ima[n<.05] = 0 # set 5% of the pixels to 0
ima[n>.95] = 255 # set 5% other pixels to 255
plt.subplot(1,2,1)
plt.imshow(ima,cmap=plt.cm.gray);
plt.subplot(1,2,2)
plt.imshow(skr.mean(ima,disk(4)),cmap=plt.cm.gray);
Starting from this example: