Skip to content

Instantly share code, notes, and snippets.

@mofas
Created February 4, 2019 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mofas/8414d2316b3545e973507efd9f54717e to your computer and use it in GitHub Desktop.
Save mofas/8414d2316b3545e973507efd9f54717e to your computer and use it in GitHub Desktop.
sarbina cute cute
from scipy import fftpack
import imageio
import numpy
# load in an image, convert to grayscale if needed
image = imageio.imread('ft/input.png', as_gray=True)
# take the fourier transform of the image
fft2 = fftpack.fftshift(fftpack.fft2(image))
# save FFT to a file. To help with visualization, we take
# the log of the magnitudes, and then stretch them so they
# fill the whole range of pixel values from 0 to 255.
imageio.imsave(
'ft/fft.png',
(numpy.log(abs(fft2)) * 255 / numpy.amax(numpy.log(abs(fft2)))).astype(
numpy.uint8))
# At this point, fft2 is just a numpy array and you can
# modify it in order to modify the image in the frequency
# space. Here's a little example (that makes a nearly
# imperceptible change, but demonstrates what you can do.
# example remove high freq
print(fft2.shape)
# threshold = 80
middle = 103
def lowerIntenseRegion(fft2, start_x, width, start_y, height, ratio):
for i in range(middle - start_y - height, middle - start_y):
for j in range(middle - start_x - width, middle - start_x):
fft2[i, j] = 10e-6 + 10e-6j
for i in range(middle + start_y, middle + start_y + height):
for j in range(middle + start_x, middle + start_x + width):
fft2[i, j] = 10e-6 + 10e-6j
# remove noise
lowerIntenseRegion(fft2, 12, 12, 14, 10, 100)
lowerIntenseRegion(fft2, 80, 20, 20, 80, 200)
# bandfreq filter
# for i in range(206):
# for j in range(206):
# dist = (i - middle)**2 + (j - middle)**2
# if dist < 50**2 and dist > 30**2:
# fft2[i, j] = 10e-6 + 10e-6j
imageio.imsave(
'ft/fft-2.png',
(numpy.log(abs(fft2)) * 255 / numpy.amax(numpy.log(abs(fft2)))).astype(
numpy.uint8))
# now take the inverse transform to convert back to an image
ifft2 = abs(fftpack.ifft2(fftpack.ifftshift(fft2)))
# and save the image
imageio.imsave('ft/fft-then-ifft.png', ifft2.astype(numpy.uint8))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment