Skip to content
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

ENH: Add collapse strategy option. #420

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 14 additions & 2 deletions ants/lib/LOCAL_sliceImage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace py = pybind11;

template < typename ImageType, typename PixelType, unsigned int NewDimension >
py::capsule sliceImage( py::capsule antsImage, int plane, int slice)
py::capsule sliceImage( py::capsule antsImage, int plane, int slice, int collapseStrategy)
{
typedef typename ImageType::Pointer ImagePointerType;
ImagePointerType itkImage = as< ImageType >( antsImage );
Expand All @@ -33,7 +33,19 @@ py::capsule sliceImage( py::capsule antsImage, int plane, int slice)

filter->SetExtractionRegion( desiredRegion );
filter->SetInput( itkImage );
filter->SetDirectionCollapseToGuess();
if( collapseStrategy == 0 )
{
filter->SetDirectionCollapseToSubmatrix();
}
else if( collapseStrategy == 1 )
{
filter->SetDirectionCollapseToIdentity();
}
else // if( collapseStrategy == 2 )
{
filter->SetDirectionCollapseToGuess();
}

filter->Update();

return wrap<SliceImageType>( filter->GetOutput() );
Expand Down
21 changes: 19 additions & 2 deletions ants/utils/slice_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@
from .. import utils


def slice_image(image, axis=None, idx=None):
def slice_image(image, axis=None, idx=None, collapse_strategy=0):
"""
Slice an image.

Arguments
---------
axis: integer
Which axis.

idx: integer
Which slice number.

collapse_strategy: integer
Collapse strategy for sub-matrix: 0, 1, or 2. 0: collapse to sub-matrix
if positive-definite. Otherwise throw an exception. Default. 1: Collapse
to identity. 2: Collapse to sub-matrix if positive definite. Otherwise
collapse to identity.

Example
-------
>>> import ants
Expand All @@ -20,13 +34,16 @@ def slice_image(image, axis=None, idx=None):
if image.dimension < 3:
raise ValueError('image must have at least 3 dimensions')

if collapse_strategy != 0 or collapse_strategy != 1 or collapse_strategy != 2:
raise ValueError('collapse_strategy must be 0, 1, or 2.')

inpixeltype = image.pixeltype
ndim = image.dimension
if image.pixeltype != 'float':
image = image.clone('float')

libfn = utils.get_lib_fn('sliceImageF%i' % ndim)
itkimage = libfn(image.pointer, axis, idx)
itkimage = libfn(image.pointer, axis, idx, collapse_strategy)

return iio.ANTsImage(pixeltype='float', dimension=ndim-1,
components=image.components, pointer=itkimage).clone(inpixeltype)
Expand Down