Thanks to visit codestin.com
Credit goes to github.com

Skip to content

add fft example #605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
57702ac
ndarray.ptp -> np.ptp for numpy v2
kushalkolar Jun 18, 2024
eecb5a3
Merge branch 'main' of https://github.com/fastplotlib/fastplotlib
kushalkolar Jun 18, 2024
ddf13bd
Merge branch 'main' of https://github.com/fastplotlib/fastplotlib
kushalkolar Jun 18, 2024
d1c9c31
Merge branch 'main' of https://github.com/fastplotlib/fastplotlib
kushalkolar Jun 18, 2024
ebebc91
Merge branch 'main' of https://github.com/fastplotlib/fastplotlib
kushalkolar Jun 26, 2024
89bf7cd
Merge branch 'main' of https://github.com/fastplotlib/fastplotlib
kushalkolar Jun 29, 2024
41f7205
Merge branch 'main' of https://github.com/fastplotlib/fastplotlib
kushalkolar Jul 11, 2024
47e9979
Merge branch 'main' of https://github.com/fastplotlib/fastplotlib
kushalkolar Jul 16, 2024
85d278c
Merge branch 'main' of https://github.com/fastplotlib/fastplotlib
kushalkolar Jul 24, 2024
abab244
Merge branch 'main' of https://github.com/fastplotlib/fastplotlib
kushalkolar Jul 29, 2024
681fbe3
Merge branch 'main' of https://github.com/fastplotlib/fastplotlib
kushalkolar Aug 2, 2024
16c3d82
add fft example
kushalkolar Sep 12, 2024
0df3221
Merge branch 'main' of https://github.com/fastplotlib/fastplotlib
kushalkolar Sep 13, 2024
ade14ea
Merge branch 'main' of https://github.com/fastplotlib/fastplotlib
kushalkolar Sep 17, 2024
5a7adfd
add fft example
kushalkolar Sep 12, 2024
036befb
simplify
kushalkolar Sep 17, 2024
8015085
stupid merge conflict
kushalkolar Sep 17, 2024
77ed481
see if it won't complain about smaller size
kushalkolar Sep 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions examples/desktop/selectors/fft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""
Explore fourier transform of images
===================================
Example showing how to use a `RectangleSelector` to interactively reconstruct
an image using portions of it fourier transform
"""

# test_example = false
# sphinx_gallery_pygfx_docs = 'screenshot'

import numpy as np
import fastplotlib as fpl
import scipy
import imageio.v3 as iio

image = iio.imread("imageio:camera.png")

# compute discrete fourier transform of image
dft = scipy.fftpack.fft2(image)

# image to just visualize absolute magnitudes
log_abs_dft = np.log(np.abs(dft))

# placeholders for displaying fft and inverse fft of selections
zeros = np.zeros(image.shape)

# create an ImageWidget to display all the images
iw = fpl.ImageWidget(
data=[image, log_abs_dft, zeros, zeros, zeros, zeros],
names=["image", "DFT", "selected", "IDFT of selected", "not-selected", "IDFT of not-selected"],
figure_shape=(3, 2), # so we can see image and fft side by side
figure_kwargs={"size": (700, 400)},
histogram_widget=False,
)

# set contrast limits based on the full DFT for the DFT-selection images
iw.figure["selected"].graphics[0].vmin, iw.figure["selected"].graphics[0].vmax = log_abs_dft.min(), log_abs_dft.max()
iw.figure["not-selected"].graphics[0].vmin, iw.figure["not-selected"].graphics[0].vmax = log_abs_dft.min(), log_abs_dft.max()

iw.show()

# create a rectangle selector
rs = iw.managed_graphics[1].add_rectangle_selector()


@rs.add_event_handler("selection")
def update_images(ev):
"""
Updates the images when the selection changes
"""

# get the bbox of the selection
row_ixs, col_ixs = ev.get_selected_indices()
row_slice = slice(row_ixs[0], row_ixs[-1] + 1)
col_slice = slice(col_ixs[0], col_ixs[-1] + 1)

# fft of the selection
selected_fft = np.zeros(image.shape, dtype=np.complex64)
selected_fft[row_slice, col_slice] = dft[row_slice, col_slice]

# update image graphic with the current fft selection
iw.managed_graphics[2].data = np.log(np.abs(selected_fft))

# inverse fft to reconstruct image using only the selection
iw.managed_graphics[3].data = scipy.fftpack.ifft2(selected_fft)
iw.managed_graphics[3].reset_vmin_vmax()

# fft of the region outside the selection
unselected_fft = dft.copy()
unselected_fft[row_slice, col_slice] = 0

# update image graphic with unselected fft area
iw.managed_graphics[4].data = np.log(np.abs(unselected_fft))

# inverse fft to reconstruct image using only the unselected part of the fft
iw.managed_graphics[5].data = scipy.fftpack.ifft2(unselected_fft)
iw.managed_graphics[5].reset_vmin_vmax()


# set initial selection to top right corner
rs.selection = (400, 512, 0, 100)


figure = iw.figure

# NOTE: `if __name__ == "__main__"` is NOT how to use fastplotlib interactively
# please see our docs for using fastplotlib interactively in ipython and jupyter
if __name__ == "__main__":
print(__doc__)
fpl.run()
Loading