본문 바로가기
Python

[Python] 2D 이미지 FFT 적용하기

by xangmin 2022. 10. 5.
반응형

python에서 2D 이미지에 FFT를 적용해서 Spectrum으로 바꾸어 보려고 한다.

2D 이미지는 크게 RGB-scale과 Gray-scale로 나뉘는데 각각의 방법에 따라 진행해보자.

 

먼저 Gray-scale을 보면 다음과 같다.

import numpy as np
import cv2
from matplotlib import pyplot as plt
from PIL import Image

img = cv2.imread('./lena_color.png', 0)

dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

magnitude_spectrum1 = 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_spectrum1, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()

 

다음은 RGB-scale이다.

RGB에서는 Gray와 다르게 3-channel을 갖는다. 그렇기 때문에 각자의 channel에서 FFT를 하고 나중에 결합한다.

< RGB-scale Input Image>
< R-input image, R-spectrum >
< G-input image, G-spectrum >
< B-input image, B-spectrum >
< RGB-input image, RGB-spectrum >

import numpy as np
import cv2
from matplotlib import pyplot as plt
from PIL import Image

img = cv2.imread('./lena_color.png')

img1, img2, img3 = img[:,:, 0], img[:,:, 1], img[:,:, 2]

dft = cv2.dft(np.float32(img1),flags = cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum1 = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
plt.subplot(121),plt.imshow(img1, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum1, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()

dft = cv2.dft(np.float32(img2),flags = cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum2 = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
plt.subplot(121),plt.imshow(img2, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum2, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()

dft = cv2.dft(np.float32(img3),flags = cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum3 = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
plt.subplot(121),plt.imshow(img3, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum3, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()

magnitude_spectrum1 = Image.fromarray(magnitude_spectrum1).convert('L')
magnitude_spectrum2 = Image.fromarray(magnitude_spectrum2).convert('L')
magnitude_spectrum3 = Image.fromarray(magnitude_spectrum3).convert('L')

#merge
img = Image.open('./lena_color.png')
merged=Image.merge("RGB",(magnitude_spectrum1, magnitude_spectrum2, magnitude_spectrum3))
plt.subplot(121), plt.imshow(img)
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(merged)
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()

 

반응형

댓글