Skip to content

Commit

Permalink
add initial sphere script as a cli option (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesagraham authored May 17, 2024
1 parent 49ca1b0 commit d67cb06
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/ttmask/sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import einops
import napari
import typer
from scipy.ndimage import distance_transform_edt

@cli.command(name='sphere')
def sphere(
boxsize: int = typer.Option(...),
sphere_diameter: float = typer.Option(...),
soft_edge_size: int = typer.Option(...),
):

sphere_radius = sphere_diameter / 2
Expand All @@ -29,6 +31,12 @@ def sphere(
idx = distance < sphere_radius
mask[idx] = 1

distance_from_edge = distance_transform_edt(mask == 0)
boundary_pixels = (distance_from_edge <= soft_edge_size) & (distance_from_edge != 0)
normalised_distance_from_edge = (distance_from_edge[boundary_pixels] / soft_edge_size) * np.pi

mask[boundary_pixels] = (0.5 * np.cos(normalised_distance_from_edge) + 0.5)

viewer = napari.Viewer(ndisplay=3)
viewer.add_image(mask)
napari.run()
Expand Down

0 comments on commit d67cb06

Please sign in to comment.