Image processing from scratch with python (5) Fourier transform

A series of studying basic image processing from scratch (5).

Refer to the OpenCV-Python tutorial Image recognition book https://www.amazon.co.jp/dp/4061529129/ It is a policy to promote understanding of the processing that is being done in.

table of contents

  1. Environment
  2. Fourier transform
  3. High pass filter
  4. Low pass filter

environment

Python 3.7.0 OpenCV 4.1.0 Jupyter Notebook

Fourier transform

Apply 2D Discrete Fourier Transform (DFT) to grayscale skeleton images. The white area is concentrated in the center and contains a lot of low frequency components.

fourier.py



import cv2
import numpy as np
from matplotlib import pyplot as plt
from pylab import rcParams
%matplotlib inline
rcParams['figure.figsize'] = 25, 20

#Read in grayscale
img = cv2.imread('/brabra/1.jpg',0)

#Fourier transform
f = np.fft.fft2(img)
#Change the center of the image to the origin
fshift = np.fft.fftshift(f)
#Since the Fourier transform result is a complex number, it is set to an absolute value and made into a log.
magnitude_spectrum = 20*np.log(np.abs(fshift))

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()
fourier.PNG

High pass filter

A high-pass filter (leaving only high frequency components) was performed. After the Fourier transform, the components near the origin are filtered in a rectangular window and returned to the image.

highpass.py



rows, cols = img.shape
#Center of image
crow,ccol = int(rows/2) , int(cols/2)
#Width near the origin to filter
reg = 50

#Filter from Fourier transform image
fshift[crow-reg:crow+reg, ccol-reg:ccol+reg] = 0
#Return to image by inverse Fourier transform
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)

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('Image after HPF'), plt.xticks([]), plt.yticks([])
plt.subplot(133),plt.imshow(img_back)
plt.title('Result in JET'), plt.xticks([]), plt.yticks([])

plt.show()

ダウンロード.png

It turned out that the edge of the skeleton remained.

Low pass filter

A low-pass filter (leaving only low frequency components) was performed. After the Fourier transform, it is processed so as to leave only the vicinity of the origin and returned to the image.

lowpass.py



#Fourier transform with opencv
dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
#Origin shift
dft_shift = np.fft.fftshift(dft)
#Absolute value,log
magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))

rows, cols = img.shape
crow,ccol = int(rows/2) , int(cols/2)
#Width near the origin to leave
fil2 = 20

#Mask making
mask = np.zeros((rows,cols,2),np.uint8)
mask[crow-fil2:crow+fil2, ccol-fil2:ccol+fil2] = 1

#Apply mask to Fourier transform elephant
fshift = dft_shift*mask
#Return to image by inverse Fourier transform
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_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('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()

low.png

This time the edges have become thinner. What looks like a wave near the edge is the ringing effect that appears when you filter with a rectangular window.

Summary

We performed Fourier transforms on images, high-pass filters, and low-pass filters to deepen our understanding.

References

--Image processing using OpenCV (OpenCV-Python tutorial) http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_imgproc/py_table_of_contents_imgproc/py_table_of_contents_imgproc.html#py-table-of-content-imgproc

--Image recognition (machine learning professional series) https://www.amazon.co.jp/dp/4061529129/

Recommended Posts

Image processing from scratch with python (5) Fourier transform
Image processing from scratch with python (4) Contour extraction
Image processing with Python
Image processing with Python (Part 2)
Image processing with Python (Part 1)
Image processing with Python (Part 3)
[Python] Image processing with scikit-image
Signal processing in Python (1): Fourier transform
Image processing with Python 100 knocks # 3 Binarization
Image processing with Python 100 knocks # 2 Grayscale
python image processing
Image processing with Python 100 knock # 10 median filter
Image processing with Python 100 knocks # 8 Max pooling
Business efficiency starting from scratch with Python
Image processing with Python & OpenCV [Tone Curve]
Image processing with Python 100 knock # 12 motion filter
Image acquisition from camera with Python + OpenCV
Drawing with Matrix-Reinventor of Python Image Processing-
Easy image processing in Python with Pillow
Image processing with Python 100 knocks # 7 Average pooling
Light image processing with Python x OpenCV
Image processing with Python 100 knocks # 9 Gaussian filter
Image processing with MyHDL
First Python image processing
Image Processing with Python Environment Setup for Windows
Image Processing with PIL
How to scrape image data from flickr with python
Read QR code from image file with Python (Mac)
Image processing with PIL (Pillow)
"Apple processing" with OpenCV3 + Python3
Image editing with python OpenCV
Acoustic signal processing with Python (2)
Acoustic signal processing with Python
Sorting image files with Python (2)
Sorting image files with Python (3)
Tweet with image in Python
Sorting image files with Python
Image processing by python (Pillow)
Image Processing Collection in Python
With skype, notify with skype from python!
[Let's play with Python] Image processing to monochrome and dots
Cut out an image with python
Real-time image processing basics with opencv
[Python] Using OpenCV with Python (Image Filtering)
[Python] Easy parallel processing with Joblib
Using Rstan from Python with PypeR
100 Language Processing Knock with Python (Chapter 1)
[Python] Using OpenCV with Python (Image transformation)
Install Python from source with Ansible
Create folders from '01' to '12' with python
100 Language Processing Knock with Python (Chapter 3)
Personal notes for python image processing
Run Aprili from Python with Orange
Let's do image scraping with Python
Call python from nim with Nimpy
Find image similarity with Python + OpenCV
Image processing | predicting species from images
Read fbx from python with cinema4d
Send image with python, save with php
Gradation image generation with Python [1] | np.linspace
Try to extract a character string from an image with Python3