-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1D line extraction from 2D images (#10)
- Loading branch information
Showing
8 changed files
with
213 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
__author__ = "Alister Burt" | ||
__email__ = "[email protected]" | ||
|
||
from .project import project_3d_to_2d | ||
from .project import project_3d_to_2d, project_2d_to_1d | ||
from .backproject import backproject_2d_to_3d | ||
from .slice_insertion import insert_central_slices_rfft_3d | ||
from .slice_extraction import extract_central_slices_rfft_3d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .central_slice_fftfreq_grid import central_slice_fftfreq_grid | ||
from .central_line_fftfreq_grid import central_line_fftfreq_grid | ||
from .central_slice_fftfreq_grid import central_slice_fftfreq_grid |
33 changes: 33 additions & 0 deletions
33
src/torch_fourier_slice/grids/central_line_fftfreq_grid.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import einops | ||
import torch | ||
|
||
from ..dft_utils import fftshift_1d, rfft_shape | ||
|
||
|
||
def central_line_fftfreq_grid( | ||
image_shape: tuple[int, int], | ||
rfft: bool, | ||
fftshift: bool = False, | ||
device: torch.device | None = None, | ||
) -> torch.Tensor: | ||
# generate 1d grid of DFT sample frequencies, shape (w, 1) | ||
w, = image_shape[-1:] | ||
grid = ( | ||
torch.fft.rfftfreq(w, device=device) | ||
if rfft | ||
else torch.fft.fftfreq(w, device=device) | ||
) | ||
|
||
# get grid of same shape with all zeros, append as third coordinate | ||
if rfft is True: | ||
zeros = torch.zeros(size=rfft_shape((w,)), dtype=grid.dtype, device=device) | ||
else: | ||
zeros = torch.zeros(size=(w,), dtype=grid.dtype, device=device) | ||
central_slice_grid, _ = einops.pack([zeros, grid], pattern="w *") # (w, 2) | ||
|
||
# fftshift if requested | ||
if fftshift is True: | ||
central_slice_grid = einops.rearrange(central_slice_grid, "w freq -> freq w") | ||
central_slice_grid = fftshift_1d(central_slice_grid, rfft=rfft) | ||
central_slice_grid = einops.rearrange(central_slice_grid, "freq w -> w freq") | ||
return central_slice_grid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from ._extract_central_slices_rfft_2d import extract_central_slices_rfft_2d | ||
from ._extract_central_slices_rfft_3d import extract_central_slices_rfft_3d |
82 changes: 82 additions & 0 deletions
82
src/torch_fourier_slice/slice_extraction/_extract_central_slices_rfft_2d.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import einops | ||
import torch | ||
from torch_image_lerp import sample_image_2d | ||
|
||
from ..dft_utils import fftfreq_to_dft_coordinates | ||
from ..grids.central_line_fftfreq_grid import central_line_fftfreq_grid | ||
|
||
|
||
def extract_central_slices_rfft_2d( | ||
image_rfft: torch.Tensor, | ||
image_shape: tuple[int, int], | ||
rotation_matrices: torch.Tensor, # (..., 2, 2) | ||
fftfreq_max: float | None = None, | ||
) -> torch.Tensor: | ||
"""Extract central slice from an fftshifted rfft.""" | ||
# generate grid of DFT sample frequencies for a central slice spanning the x-plane | ||
freq_grid = central_line_fftfreq_grid( | ||
image_shape=image_shape, | ||
rfft=True, | ||
fftshift=True, | ||
device=image_rfft.device, | ||
) # (w, 2) yx coords | ||
|
||
# keep track of some shapes | ||
stack_shape = tuple(rotation_matrices.shape[:-2]) | ||
rfft_shape = (freq_grid.shape[-2],) | ||
output_shape = (*stack_shape, *rfft_shape) | ||
|
||
# get (b, 2, 1) array of yx coordinates to rotate | ||
if fftfreq_max is not None: | ||
freq_grid_mask = freq_grid <= fftfreq_max | ||
valid_coords = freq_grid[freq_grid_mask, ...] | ||
else: | ||
valid_coords = freq_grid | ||
valid_coords = einops.rearrange(valid_coords, "b yx -> b yx 1") | ||
|
||
# rotation matrices rotate xyz coordinates, make them rotate zyx coordinates | ||
# xyz: | ||
# [a b c] [x] [ax + by + cz] | ||
# [d e f] [y] = [dx + ey + fz] | ||
# [g h i] [z] [gx + hy + iz] | ||
# | ||
# zyx: | ||
# [i h g] [z] [gx + hy + iz] | ||
# [f e d] [y] = [dx + ey + fz] | ||
# [c b a] [x] [ax + by + cz] | ||
rotation_matrices = torch.flip(rotation_matrices, dims=(-2, -1)) | ||
|
||
# add extra dim to rotation matrices for broadcasting | ||
rotation_matrices = einops.rearrange(rotation_matrices, "... i j -> ... 1 i j") | ||
|
||
# rotate all valid coordinates by each rotation matrix | ||
rotated_coords = rotation_matrices @ valid_coords # (..., b, yx, 1) | ||
|
||
# remove last dim of size 1 | ||
rotated_coords = einops.rearrange(rotated_coords, "... b yx 1 -> ... b yx") | ||
|
||
# flip coordinates that ended up in redundant half transform after rotation | ||
conjugate_mask = rotated_coords[..., 1] < 0 | ||
rotated_coords[conjugate_mask, ...] *= -1 | ||
|
||
# convert frequencies to array coordinates in fftshifted DFT | ||
rotated_coords = fftfreq_to_dft_coordinates( | ||
frequencies=rotated_coords, image_shape=image_shape, rfft=True | ||
) # (...) rfft | ||
samples = sample_image_2d(image=image_rfft, coordinates=rotated_coords) | ||
|
||
# take complex conjugate of values from redundant half transform | ||
samples[conjugate_mask] = torch.conj(samples[conjugate_mask]) | ||
|
||
# insert samples back into DFTs | ||
projection_image_dfts = torch.zeros( | ||
output_shape, device=image_rfft.device, dtype=image_rfft.dtype | ||
) | ||
if fftfreq_max is None: | ||
freq_grid_mask = torch.ones( | ||
size=rfft_shape, dtype=torch.bool, device=image_rfft.device | ||
) | ||
|
||
projection_image_dfts[..., freq_grid_mask] = samples | ||
|
||
return projection_image_dfts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters