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.
Python 3.7.0 OpenCV 4.1.0 Jupyter Notebook
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()
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()
It turned out that the edge of the skeleton remained.
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()
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.
We performed Fourier transforms on images, high-pass filters, and low-pass filters to deepen our understanding.
--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