# This is for FFT
# we are going to use matplotlib image package and import the image read so that we can load our image dog.jpg
from matplotlib.image import imread #for reading the functions
import numpy as np #by using mathematical and logical operations on a
ays can be performed.
import matplotlib.pyplot as plt #this is a state-based interface to matplotlib. It provides a MATLAB-like way of plotting.pyplot is mainly intended for interactive plots and simple cases of programmatic plot generation:
import os
plt.rcParams['figure.figsize'] = [5, 5]
plt.rcParams.update({'font.size': 18})
#reading an image by using imread funtion
A=imread(os.path.join('/content/drive/MyDrive/dog.jpg'))
B = np.mean(A, -1)
#crearting a figure
plt.figure()#The figure() function in pyplot module of matplotlib li
ary is used to create a new figure.
#showing the image
plt.imshow(256-A)#The imshow() function in pyplot module of matplotlib li
ary is used to display data as an image
plt.axis('off')#The plt. axis() method allows you to set the x and y limits with a single call,
Bt=np.fft.fft2(B)
#sorting a
ay
Btsort = np.sort(np.abs(Bt.reshape(-1)))
for keep in(0.1,0.05,0.01,0.002):
thresh =Btsort[int(np.floor((1-keep)*len(Btsort)))]
ind = np.abs(Bt)>thresh
Btlow=Bt*ind
Alow = np.fft.ifft2(Btlow).real
plt.figure()
plt.imshow(256-Alow,cmap='gray')
plt.axis('off')
plt.title('Compressed image: keep = ' + str(keep*100) + '%')
****************************************************************************
****************************************************************************
****************************************************************************
# This is for Wavelet Transform
from matplotlib.image import imread
import numpy as np
import matplotlib.pyplot as plt##this is a state-based interface to matplotlib. It provides a MATLAB-like way of plotting.pyplot is mainly intended for interactive plots and simple cases of programmatic plot generation:
import os
import pywt#this is a scientific Python module for Wavelet Transform calculations.
#changing the size of an image
plt.rcParams['figure.figsize'] = [16, 16]
#changing the font size
plt.rcParams.update({'font.size': 18})
#reading the image using imread image
A=imread(os.path.join('/content/drive/MyDrive/dog.jpg'))
#taking average
B = np.mean(A, -1)
n=2
w='db1'
#using pywt.wavedec2 for multilevel decomposition
coeffs = pywt.wavedec2(B, wavelet=w, level=n)
coeffs[0] /= np.abs(coeffs[0]).max()
for detail_level in range(n):
coeffs[detail_level + 1] = [d/np.abs(d).max() for d in coeffs[detail_level + 1]]
a
, coeff_slices= pywt.coeffs_to_a
ay(coeffs)
plt.imshow(a
, cmap='gray_r',vmin=-0.25,vmax=0.75)
#again changing the figure size
plt.rcParams['figure.figsize'] = [16, 16]
fig=plt.figure(figsize=(18, 16))
plt.show()