In [9]:
#Low Pass Filter
import numpy as np
import cv2
from matplotlib import pyplot as plt
from skimage import data
from skimage.draw import circle
# img = cv2.imread('xfiles.jpg',0)
img = data.camera()
img_float32 = np.float32(img)
print('Shape of original image:',img_float32.shape)
dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT)
# Now once you got the result, zero frequency component
# (DC component) will be at top left corner.
# If you want to bring it to center, you need
# to shift the result by \frac{N}{2} in both the directions.
# This is simply done by the function, np.fft.fftshift().
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
In [10]:
import numpy as np
# Return the Discrete Fourier Transform sample frequencies.
freqs = np.fft.fftfreq(10, 0.1)
print(freqs)
freqs_shifted = np.fft.fftshift(freqs)
print(freqs_shifted)
print(cv2.magnitude(freqs_shifted,freqs_shifted))
In [22]:
rows, cols = img.shape
crow, ccol = int(rows/2) , int(cols/2) # center
flt_size = 20
# create a mask first, center square is 1, remaining all zeros
mask_ = np.zeros((rows, cols, 2), np.uint8)
#circle center in crow,ccol and radius-100
col,row = circle(crow, ccol,100)
mask_[col,row] = 1
In [23]:
new_shift = dft_shift*mask_
new_ishift = np.fft.ifftshift(new_shift)
img_back = cv2.idft(new_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
In [24]:
plt.subplot(131),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(132),plt.imshow(img_back, cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.subplot(133),plt.imshow(mask_[:,:,0], cmap = 'gray')
plt.title('Circle shape'), plt.xticks([]), plt.yticks([])
plt.show()
In [29]:
#Make High Pass Filter(HPF) as example take into consideration LPF
import numpy as np
import cv2
from matplotlib import pyplot as plt
from skimage import data
from skimage.draw import circle
# img = cv2.imread('xfiles.jpg',0)
img = data.camera()
rows, cols = img.shape
crow, ccol = int(rows/2) , int(cols/2) # center
flt_size = 20
# create a mask first, center square is 1, remaining all zeros
mask_ = np.ones((rows, cols, 2), np.uint8)# fill all the ones
#circle center in crow,ccol and radius-100
col,row = circle(crow, ccol,100)
mask_[col,row] = 0# circle inside all zeros outside ones
new_shift = dft_shift*mask_
new_ishift = np.fft.ifftshift(new_shift)
img_back = cv2.idft(new_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img_back, cmap = 'gray')
plt.title('EdgesUsing High Pass'), plt.xticks([]), plt.yticks([])
plt.show()
In [38]:
#BandPass Filter
import numpy as np
import cv2
from matplotlib import pyplot as plt
from skimage import data
from skimage.draw import circle
# img = cv2.imread('xfiles.jpg',0)
# img = cv2.imread('xfiles.jpg',0)
img = data.camera()
rows, cols = img.shape
crow, ccol = int(rows/2) , int(cols/2) # center
flt_size = 20
# create a mask first, center square is 1, remaining all zeros
mask_ = np.zeros((rows, cols, 2), np.uint8)# fill all the ones
#circle center in crow,ccol and radius-100
col,row = circle(crow, ccol,40)
mask_[col,row] = 1# circle inside all zeros outside ones
new_shift = dft_shift*mask_
new_ishift = np.fft.ifftshift(new_shift)
img_back = cv2.idft(new_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
#low pass then high pass band pass
rows, cols = img_back.shape
crow, ccol = int(rows/2) , int(cols/2) # center
flt_size = 20
# create a mask first, center square is 1, remaining all zeros
mask_ = np.ones((rows, cols, 2), np.uint8)# fill all the zeros
#circle center in crow,ccol and radius-100
col,row = circle(crow, ccol,40)
mask_[col,row] = 0# circle inside all zeros outside ones
new_shift = dft_shift*mask_
new_ishift = np.fft.ifftshift(new_shift)
img_back2 = cv2.idft(new_ishift)
img_back2 = cv2.magnitude(img_back2[:,:,0],img_back2[:,:,1])
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img_back2, cmap = 'gray')
plt.title('Band Pass Filter'), plt.xticks([]), plt.yticks([])
plt.show()
In [2]:
import numpy as np
from matplotlib import pyplot as plt
from skimage import data
from sklearn.preprocessing import MinMaxScaler
import pywt
img = data.camera()
imArray = np.float32(img)
imArray /= 255;
coeffs=pywt.wavedec2(imArray, 'haar')
coeffs_H=list(coeffs)
coeffs_H[0] *= 0;
imArray_H = pywt.waverec2(coeffs_H, 'haar')
plt.imshow(imArray_H,cmap='gray')
imArray = np.float32(img)
imArray /= 255;
# # reconstruction
imArray_H=pywt.waverec2(coeffs_H, 'haar')
plt.imshow(imArray_H)
plt.show()
In [ ]:
In [ ]: