-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFourier_frequency_reduction_animation.py
More file actions
142 lines (107 loc) · 4.09 KB
/
Fourier_frequency_reduction_animation.py
File metadata and controls
142 lines (107 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import numpy as np
import matplotlib.pyplot as plt
from skimage.io import imread
from skimage.color import rgb2gray
from skimage import color, exposure, transform
from skimage import exposure
import os
from matplotlib.widgets import Button, Slider
import tkinter as tk
from tkinter import filedialog
# Get the directory where the script is located
script_directory = os.path.dirname(os.path.abspath(__file__))
# Function to load and preprocess the image
def load_image(filepath):
image = imread(filepath)
if image.shape[2] == 3:
image = rgb2gray(image)
image = exposure.rescale_intensity(image, out_range='float64')
return image
def visualize_image(image, percentage):
global fig, ax, fo, fa, io, ia
fourier_orig, fourier_masked, image_filtered = process_images(image, percentage)
io = ax[0].imshow(image, cmap = 'gray')
ax[0].set_title('Greyscale Image', fontsize = f_size)
fo = ax[1].imshow(np.log(abs(fourier_orig)), cmap='gray')
ax[1].set_title('Original Fourier', fontsize = f_size)
fa = ax[2].imshow(np.log(abs(fourier_masked)), cmap='gray')
ax[2].set_title('Masked Fourier', fontsize = f_size)
ia = ax[3].imshow(abs(image_filtered), cmap='gray')
ax[3].set_title('Transformed Greyscale Image', fontsize = f_size)
for i in range(0,4):
ax[i].set_xticks([])
ax[i].set_yticks([])
def change_image(event):
global image, fig, ax, fo, fa, io, ia
root = tk.Tk()
root.withdraw() # Hide the main window
file_path = filedialog.askopenfilename()
if file_path:
image = load_image(file_path)
# Clear the current axes
for a in ax:
a.clear()
visualize_image(image, freq_slider.val)
fig.canvas.draw_idle()
# Load the initial image
initial_image_file_name = "fourier_testing_images/wave.jpg"
initial_image_path = os.path.join(script_directory, initial_image_file_name)
image = load_image(initial_image_path)
percentage = 10
f_size = 15
def mask_circle(image, percentage):
"""
Keep only a circle in the center of the image and make the rest of the image black.
Parameters:
image (numpy array): The input image.
percentage (float): The percentage of the radius of the circle compared to the image size.
Returns:
numpy array: The masked image.
"""
# Get the dimensions of the image
x, y = image.shape[:2]
# Calculate the center and radius of the circle
center_x, center_y = x // 2, y // 2
radius = int(min(center_x, center_y) * (percentage / 100))
# Create a mask with a circle
Y, X = np.ogrid[:x, :y]
dist_from_center = np.sqrt((X - center_y)**2 + (Y - center_x)**2)
mask = dist_from_center <= radius
# Create an output image and apply the mask
masked_image = np.ones_like(image)
masked_image[mask] = image[mask]
return masked_image
def process_images(image, percentage):
fourier_orig = np.fft.fftshift(np.fft.fft2(image))
fourier_masked = mask_circle(fourier_orig,percentage)
image_filtered = np.fft.ifft2(fourier_masked)
return fourier_orig,fourier_masked,image_filtered
fig, ax = plt.subplots(1,4,figsize=(15,5))
visualize_image(image, percentage)
# adjust the main plot to make room for the sliders
fig.subplots_adjust(left=0.05)
# Make a horizontal slider to control the frequency.
axfreq = fig.add_axes([0.02, 0.1, 0.01, 0.8])
freq_slider = Slider(
ax=axfreq,
label='Reduction',
valmin=0.1,
valmax=100,
valinit=percentage,
orientation="vertical"
)
# Button to change the image
axchange = fig.add_axes([0.45, 0.05, 0.1, 0.075])
change_button = Button(axchange, 'Change Image')
# The function to be called anytime a slider's value changes
def update(val):
fourier_orig,fourier_masked,image_filtered = process_images(image,freq_slider.val)
fo.set_data(np.log(abs(fourier_orig)))
fa.set_data(np.log(abs(fourier_masked)))
ia.set_data(abs(image_filtered))
fig.canvas.draw_idle()
# register the update function with the slider
freq_slider.on_changed(update)
# Register the change image function with the button
change_button.on_clicked(change_image)
plt.show()