-
Notifications
You must be signed in to change notification settings - Fork 0
fourier_analysis
Fourier analysis toolkit with noise introduction, spectral analysis, high/low/pass filters.
Let 1D fft be define as a 1D_FFT(input) where input is a 1D signal. A 1D FFT computes the discrete frequencies in a signal. Effectively it takes a signal and isolates all the possible frequecies. It can be used for manipulation as seen below
For more info see my personal implementation where I solve the math and code from scratch.
A 2D FFT is as follows:: fft_array = []
for row in image: fft_array[row] = 1D_FFT(row)
for col in fft_array: fft_array[col] = = 1D_FFT(col)
Understanding a 2D FFT
### Observing General FFT Trends
import image_processing as imp
from image_processing import fourier_analysis,im_processing
###create an image with black lines
vert_lines = fourier_analysis.gen_line_im(size=(100,100),samp_freq=5,vert=True,balance = True)
horiz_lines= fourier_analysis.gen_line_im(size=(100,100),samp_freq=5,vert=False,balance = True)
checker= fourier_analysis.gen_line_im(size=(100,100),samp_freq=5,checker= True)
rotated = im_processing.rot45(vert_lines)
box= fourier_analysis.gen_square_im(size=(100,100),n=2,val=1)
ims = [vert_lines,horiz_lines,checker,rotated,box]
ims = [fourier_analysis.im_noise(im).poisson() for im in ims]
titles= ['vert_lines','horiz_lines','checker','rotated45','square']
ims.extend([fourier_analysis.magnitude_spectrum(img) for img in ims]) #get FFT
titles.extend([t +' FFT' for t in titles])
imp.im_subplot(ims,shape=[2,5],titles=titles,
suptitle='Understanding Fast Fourier Transforms (FFT)')What this script did was add created vertical, horizontal, checkered, and diaganol lined images with a little bit of noise (More detail on noise below. Was done to see clearer FFT's)
General FFT Observations:
- vertical stripes ~ horizontally-oriented frequencies
- diagonal stripes ~ strong (opposite) diagonal components in FFT
- Images with both horizontal and vertical features have both vertical and horizontal components in FFT
- high frequencies are far from the origin, lower are closer
-
Gaussian: add normal distributed noise with set mean and standard deviation | N(mu,sig)
-
Salt & Pepper: Replaces random pixels with 0 or 1. Can control amount of noise and ratio of 0/1 (s/p)
-
Poisson: Poisson Distribution
-
Speckle: img + (img x N(0,1)) where N is gaussian distribution
import image_processing as imp
from image_processing import fourier_analysis
img = imp.cv_read(file_path='images/img_0.jpg',RGB=True) #Read RGB
noise= fourier_analysis.im_noise(img) #Noise Class
ims = [img,noise.gaussian(mean=0,sigma=40), noise.salt_pepper(s_vs_p = 0.5, amount = 0.04),noise.poisson(),noise.speckle()] #add noise to ims
titles= ['Orig Image','Gaussian Noise', 'Salt/Pepper Noise', 'Possion Noise','Speckle Noise']
imp.im_subplot(ims,titles=titles,suptitle='Noise') #compare
##Continued
ffts= [fourier_analysis.magnitude_spectrum(img) for img in ims] #get fft 2D
titles= [t+' FFT' for t in titles]
imp.im_subplot(ffts,titles=titles,
suptitle='Noise Fast Fourier Transforms (FFT)')