Files
Robust_image_source_identif…/datasets/raise/fft/fft.py
2024-05-14 01:19:05 +02:00

59 lines
1.5 KiB
Python

from scipy import fftpack
import numpy as np
import imageio
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt
import sys
sys.path.insert(0, '../')
from utils import Color, mergeSingleColorChannelImagesAccordingToBayerFilter
PREFIX = 'mean_rafael_230424_mean_'
def getImageByColor(color):
filePath = PREFIX + f'{color}.npy'
image = np.load(filePath)
return image
singleColorChannelImages = {color: getImageByColor(color) for color in Color}
multipleColorsImage = mergeSingleColorChannelImagesAccordingToBayerFilter(singleColorChannelImages)
image = multipleColorsImage
#image = imageio.imread('image.jpg', mode = 'L')
#plt.imshow(image, cmap = 'grey')
#plt.show()
# convert image to numpy array
imageNp = np.array(image)
# fft of image
fft1 = fftpack.fftshift(fftpack.fft2(imageNp))
# create a low pass filter image
x, y = imageNp.shape
# size of circle
CIRCLE_RADIUS = 20
eX, eY = CIRCLE_RADIUS, CIRCLE_RADIUS
# create a box
middleX, middleY = x / 2, y / 2
bbox = (middleX - eX, middleY - eY, middleX + eX, middleY + eY)
lowPass = Image.new('L', imageNp.shape, color = 0)
draw = ImageDraw.Draw(lowPass)
draw.ellipse(bbox, fill = 1)
lowPassNp = np.array(lowPass)
# multiply both the images
filtered = np.multiply(fft1, lowPassNp.T)
# inverse fft
ifft2 = np.real(fftpack.ifft2(fftpack.ifftshift(filtered)))
ifft2 = np.maximum(0, np.minimum(ifft2, 255))
# save the image
plt.imshow(ifft2)
plt.show()
#imageio.imsave('fft-then-ifft.png', ifft2.astype(np.uint8))