.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/3_utils/make_ball_at_shape_center.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_3_utils_make_ball_at_shape_center.py: .. _make_ball_at_shape_center: 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. .. GENERATED FROM PYTHON SOURCE LINES 10-30 .. code-block:: Python 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 .. GENERATED FROM PYTHON SOURCE LINES 31-45 .. code-block:: Python 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() .. image-sg:: /auto_examples/3_utils/images/sphx_glr_make_ball_at_shape_center_001.png :alt: make ball at shape center :srcset: /auto_examples/3_utils/images/sphx_glr_make_ball_at_shape_center_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2D Example : centre = (tensor(350), tensor(351)), r = 143 and the seg and ball have 10061 pixels overlapping .. GENERATED FROM PYTHON SOURCE LINES 46-47 _ = input("Press for 3D example :") .. GENERATED FROM PYTHON SOURCE LINES 47-59 .. code-block:: Python 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() .. image-sg:: /auto_examples/3_utils/images/sphx_glr_make_ball_at_shape_center_002.png :alt: make ball at shape center :srcset: /auto_examples/3_utils/images/sphx_glr_make_ball_at_shape_center_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /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 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.603 seconds) .. _sphx_glr_download_auto_examples_3_utils_make_ball_at_shape_center.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: make_ball_at_shape_center.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: make_ball_at_shape_center.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: make_ball_at_shape_center.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_