Note
Go to the end to download the full example code.
Make Ball at Shape Center
A utility function to create a ball at the center of a shape in an image. Can be useful to initialise a mask for a guided registration task.
try:
import sys, os
# add the parent directory to the path
base_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),'..')
sys.path.insert(0,base_path)
import __init__
except NameError:
pass
# import __init__
import torch
from demeter.constants import ROOT_DIRECTORY
from demeter.utils.torchbox import make_ball_at_shape_center,reg_open, imCmp
import matplotlib.pyplot as plt
import demeter.utils.image_3d_plotter as i3p
print(f"2D Example :")
img = reg_open('m0t')
ball, info = make_ball_at_shape_center(img,
overlap_threshold=.1,
verbose=True)
centre_x,centre_y,r = info
fig,ax = plt.subplots(1,2)
ax[0].imshow(img[0,0],cmap='gray')
ax[0].plot(centre_x,centre_y,'x')
ax[1].imshow(imCmp(img,ball),origin='lower')
plt.show()

2D Example :
centre = (tensor(350), tensor(351)), r = 143 and the seg and ball have 10061 pixels overlapping
_ = input(“Press for 3D example :”)
img = torch.load(ROOT_DIRECTORY+"/examples/im3Dbank/hanse_w_ball.pt")
ball, info = make_ball_at_shape_center(img,
# shape_binarization=img == img.max(),
overlap_threshold=.1,
# force_r=50,
verbose=True)
centre_x,centre_y,centre_z,r = info
img_cmp = imCmp(img,ball)
i3p.imshow_3d_slider(img_cmp)
plt.show()

/home/runner/work/Demeter_metamorphosis/Demeter_metamorphosis/examples/3_utils/make_ball_at_shape_center.py:47: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
img = torch.load(ROOT_DIRECTORY+"/examples/im3Dbank/hanse_w_ball.pt")
centre = (tensor(65), tensor(78), tensor(65)), r = 21 and the seg and ball have 38641 pixels overlapping
Total running time of the script: (0 minutes 0.603 seconds)