From 264c0ef2881d75bd00115d8b23f6dd1b55461316 Mon Sep 17 00:00:00 2001 From: ncullen93 Date: Thu, 16 May 2024 15:18:22 +0200 Subject: [PATCH] move to pointer-only initialization --- ants/core/ants_image.py | 97 ++++++++++++------- ants/core/ants_image_io.py | 20 ++-- ants/core/ants_transform.py | 7 +- ants/core/ants_transform_io.py | 7 +- ants/registration/reorient_image.py | 5 +- ants/segmentation/anti_alias.py | 3 +- ants/utils/add_noise_to_image.py | 18 +--- ants/utils/channels.py | 10 +- ants/utils/compose_displacement_fields.py | 5 +- ants/utils/crop_image.py | 11 +-- ants/utils/fit_bspline_displacement_field.py | 6 +- .../fit_bspline_object_to_scattered_data.py | 6 +- ...it_thin_plate_spline_displacement_field.py | 6 +- ants/utils/histogram_match_image.py | 3 +- ants/utils/integrate_velocity_field.py | 5 +- ants/utils/invert_displacement_field.py | 5 +- ants/utils/pad_image.py | 5 +- ants/utils/scalar_rgb_vector.py | 6 +- ants/utils/simulate_displacement_field.py | 10 +- ants/utils/slice_image.py | 5 +- ants/utils/smooth_image.py | 5 +- ants/utils/weingarten_image_curvature.py | 5 +- src/antsImage.cxx | 13 +++ src/antsImage.h | 7 ++ 24 files changed, 132 insertions(+), 138 deletions(-) diff --git a/ants/core/ants_image.py b/ants/core/ants_image.py index ae1411b9..4a63aee4 100644 --- a/ants/core/ants_image.py +++ b/ants/core/ants_image.py @@ -44,47 +44,74 @@ class ANTsImage(object): - def __init__(self, pixeltype='float', dimension=3, components=1, pointer=None, is_rgb=False): + def __init__(self, pointer): """ - Initialize an ANTsImage + Initialize an ANTsImage. + + Creating an ANTsImage requires a pointer to an underlying ITK image that + is stored via a nanobind class wrapping a AntsImage struct. Arguments --------- - pixeltype : string - ITK pixeltype of image - - dimension : integer - number of image dimension. Does NOT include components dimension - - components : integer - number of pixel components in the image - - pointer : py::capsule (optional) - pybind11 capsule holding the pointer to the underlying ITK image object + pointer : nb::class + nanobind class wrapping the struct holding the pointer to the underlying ITK image object """ - ## Attributes which cant change without creating a new ANTsImage object self.pointer = pointer - self.pixeltype = pixeltype - self.dimension = dimension - self.components = components - self.has_components = self.components > 1 - self.dtype = _itk_to_npy_map[self.pixeltype] - self.is_rgb = is_rgb - - self._pixelclass = 'vector' if self.has_components else 'scalar' - self._shortpclass = 'V' if self._pixelclass == 'vector' else '' - if is_rgb: - self._pixelclass = 'rgb' - self._shortpclass = 'RGB' - - self._libsuffix = '%s%s%i' % (self._shortpclass, utils.short_ptype(self.pixeltype), self.dimension) - - self.shape = tuple(utils.get_lib_fn('getShape')(self.pointer)) - self.physical_shape = tuple([round(sh*sp,3) for sh,sp in zip(self.shape, self.spacing)]) - self._array = None + @property + def _libsuffix(self): + return str(type(self.pointer)).split('AntsImage')[-1].split("'")[0] + + @property + def shape(self): + return tuple(utils.get_lib_fn('getShape')(self.pointer)) + + @property + def physical_shape(self): + return tuple([round(sh*sp,3) for sh,sp in zip(self.shape, self.spacing)]) + + @property + def is_rgb(self): + return 'RGB' in self._libsuffix + + @property + def has_components(self): + suffix = self._libsuffix + return suffix.startswith('V') or suffix.startswith('RGB') + + @property + def components(self): + if not self.has_components: + return 1 + + libfn = utils.get_lib_fn('getComponents') + return libfn(self.pointer) + + @property + def pixeltype(self): + ptype = self._libsuffix[:-1] + if self.has_components: + if self.is_rgb: + ptype = ptype[3:] + else: + ptype = ptype[1:] + + ptype_map = {'UC': 'unsigned char', + 'UI': 'unsigned int', + 'F': 'float', + 'D': 'double'} + return ptype_map[ptype] + + @property + def dtype(self): + return _itk_to_npy_map[self.pixeltype] + + @property + def dimension(self): + return int(self._libsuffix[-1]) + @property def spacing(self): """ @@ -284,11 +311,7 @@ def clone(self, pixeltype=None): fn_suffix = '%s%i' % (p2_short,ndim) libfn = utils.get_lib_fn('antsImageClone%s'%fn_suffix) pointer_cloned = libfn(self.pointer) - return ANTsImage(pixeltype=pixeltype, - dimension=self.dimension, - components=self.components, - is_rgb=self.is_rgb, - pointer=pointer_cloned) + return iio2.from_pointer(pointer_cloned) # pythonic alias for `clone` is `copy` copy = clone diff --git a/ants/core/ants_image_io.py b/ants/core/ants_image_io.py index 959a1e19..a72872f0 100644 --- a/ants/core/ants_image_io.py +++ b/ants/core/ants_image_io.py @@ -3,6 +3,7 @@ """ __all__ = [ + 'from_pointer', "image_header_info", "image_clone", "image_read", @@ -66,6 +67,9 @@ _image_read_dict[itype][p][d] = "imageRead%s%s%i" % (ita, pa, d) +def from_pointer(pointer): + return iio.ANTsImage(pointer) + def from_numpy( data, origin=None, spacing=None, direction=None, has_components=False, is_rgb=False ): @@ -128,9 +132,7 @@ def _from_numpy( if not has_components: itk_image = libfn(data, data.shape[::-1]) - ants_image = iio.ANTsImage( - pixeltype=ptype, dimension=ndim, components=1, pointer=itk_image - ) + ants_image = from_pointer(itk_image) ants_image.set_origin(origin) ants_image.set_spacing(spacing) ants_image.set_direction(direction) @@ -141,9 +143,7 @@ def _from_numpy( ants_images = [] for i in range(len(arrays)): tmp_ptr = libfn(arrays[i], data_shape[::-1]) - tmp_img = iio.ANTsImage( - pixeltype=ptype, dimension=ndim, components=1, pointer=tmp_ptr - ) + tmp_img = from_pointer(tmp_ptr) tmp_img.set_origin(origin) tmp_img.set_spacing(spacing) tmp_img.set_direction(direction) @@ -547,13 +547,7 @@ def image_read(filename, dimension=None, pixeltype="float", reorient=False): libfn = utils.get_lib_fn(_image_read_dict[pclass][ptype][ndim]) itk_pointer = libfn(filename) - ants_image = iio.ANTsImage( - pixeltype=ptype, - dimension=ndim, - components=ncomp, - pointer=itk_pointer, - is_rgb=is_rgb, - ) + ants_image = from_pointer(itk_pointer) if pixeltype is not None: ants_image = ants_image.clone(pixeltype) diff --git a/ants/core/ants_transform.py b/ants/core/ants_transform.py index 19ac4624..727bed9a 100644 --- a/ants/core/ants_transform.py +++ b/ants/core/ants_transform.py @@ -16,7 +16,7 @@ 'transform_index_to_physical_point', 'transform_physical_point_to_index'] -from . import ants_image as iio +from . import ants_image as iio, ants_image_io as iio2 from .. import utils @@ -188,10 +188,7 @@ def apply_to_image(self, image, reference=None, interpolation='linear'): reference = reference.clone(image.pixeltype) img_ptr = tform_fn(self.pointer, image.pointer, reference.pointer, interpolation) - return iio.ANTsImage(pixeltype=image.pixeltype, - dimension=image.dimension, - components=image.components, - pointer=img_ptr) + return iio2.from_pointer(img_ptr) def __repr__(self): s = "ANTsTransform\n" +\ diff --git a/ants/core/ants_transform_io.py b/ants/core/ants_transform_io.py index 5c7a8214..3fa127d7 100644 --- a/ants/core/ants_transform_io.py +++ b/ants/core/ants_transform_io.py @@ -10,7 +10,7 @@ import os import numpy as np -from . import ants_image as iio +from . import ants_image as iio, ants_image_io as iio2 from . import ants_transform as tio from .. import utils @@ -297,10 +297,7 @@ def transform_to_displacement_field(xfrm, ref): raise ValueError("Transform must be of DisplacementFieldTransform type") libfn = utils.get_lib_fn("antsTransformToDisplacementField") field_ptr = libfn(xfrm.pointer, ref.pointer) - return iio.ANTsImage( pixeltype=xfrm.precision, - dimension=xfrm.dimension, - components=xfrm.dimension, - pointer=field_ptr) + return iio2.from_pointer(field_ptr) def read_transform(filename, precision="float"): """ diff --git a/ants/registration/reorient_image.py b/ants/registration/reorient_image.py index 40e2fb72..ef1e7dc4 100644 --- a/ants/registration/reorient_image.py +++ b/ants/registration/reorient_image.py @@ -11,7 +11,7 @@ from . import apply_transforms from .. import utils -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 _possible_orientations = ['RIP','LIP', 'RSP', 'LSP', 'RIA', 'LIA', @@ -72,8 +72,7 @@ def reorient_image2(image, orientation='RAS'): libfn = utils.get_lib_fn('reorientImage2') itkimage = libfn(image.pointer, orientation) - new_img = iio.ANTsImage(pixeltype='float', dimension=ndim, - components=image.components, pointer=itkimage)#.clone(inpixeltype) + new_img = iio2.from_pointer(itkimage) if inpixeltype != 'float': new_img = new_img.clone(inpixeltype) return new_img diff --git a/ants/segmentation/anti_alias.py b/ants/segmentation/anti_alias.py index d3a1afb0..6b82128a 100644 --- a/ants/segmentation/anti_alias.py +++ b/ants/segmentation/anti_alias.py @@ -40,5 +40,4 @@ def anti_alias(image): libfn = utils.get_lib_fn('antiAlias%s' % image._libsuffix) new_ptr = libfn(image.pointer) - return iio.ANTsImage(pixeltype='float', dimension=image.dimension, - components=image.components, pointer=new_ptr) + return iio2.from_pointer(new_ptr) diff --git a/ants/utils/add_noise_to_image.py b/ants/utils/add_noise_to_image.py index 5a2dd7a7..7568e017 100644 --- a/ants/utils/add_noise_to_image.py +++ b/ants/utils/add_noise_to_image.py @@ -1,7 +1,7 @@ __all__ = ["add_noise_to_image"] from .. import utils -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 def add_noise_to_image(image, noise_model, @@ -47,9 +47,7 @@ def add_noise_to_image(image, libfn = utils.get_lib_fn("additiveGaussianNoise") noise = libfn(image.pointer, noise_parameters[0], noise_parameters[1]) - output_image = iio.ANTsImage(pixeltype='float', - dimension=image_dimension, components=1, - pointer=noise).clone('float') + output_image = iio2.from_pointer(noise).clone('float') return output_image elif noise_model == 'saltandpepper': if len(noise_parameters) != 3: @@ -57,9 +55,7 @@ def add_noise_to_image(image, libfn = utils.get_lib_fn("saltAndPepperNoise") noise = libfn(image.pointer, noise_parameters[0], noise_parameters[1], noise_parameters[2]) - output_image = iio.ANTsImage(pixeltype='float', - dimension=image_dimension, components=1, - pointer=noise).clone('float') + output_image = iio2.from_pointer(noise).clone('float') return output_image elif noise_model == 'shot': if not isinstance(noise_parameters, (int, float)): @@ -67,9 +63,7 @@ def add_noise_to_image(image, libfn = utils.get_lib_fn("shotNoise") noise = libfn(image.pointer, noise_parameters) - output_image = iio.ANTsImage(pixeltype='float', - dimension=image_dimension, components=1, - pointer=noise).clone('float') + output_image = iio2.from_pointer(noise).clone('float') return output_image elif noise_model == 'speckle': if not isinstance(noise_parameters, (int, float)): @@ -77,9 +71,7 @@ def add_noise_to_image(image, libfn = utils.get_lib_fn("speckleNoise") noise = libfn(image.pointer, noise_parameters) - output_image = iio.ANTsImage(pixeltype='float', - dimension=image_dimension, components=1, - pointer=noise).clone('float') + output_image = iio2.from_pointer(noise).clone('float') return output_image else: raise ValueError("Unknown noise model.") diff --git a/ants/utils/channels.py b/ants/utils/channels.py index 7f48facc..2bc3f79f 100644 --- a/ants/utils/channels.py +++ b/ants/utils/channels.py @@ -4,7 +4,7 @@ __all__ = ['merge_channels', 'split_channels'] -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 from .. import utils @@ -45,10 +45,7 @@ def merge_channels(image_list): libfn = utils.get_lib_fn('mergeChannels') image_ptr = libfn([image.pointer for image in image_list]) - return iio.ANTsImage(pixeltype=inpixeltype, - dimension=dimension, - components=components, - pointer=image_ptr) + return iio2.from_pointer(image_ptr) def split_channels(image): @@ -82,8 +79,7 @@ def split_channels(image): libfn = utils.get_lib_fn('splitChannels') itkimages = libfn(image.pointer) - antsimages = [iio.ANTsImage(pixeltype=inpixeltype, dimension=dimension, - components=components, pointer=itkimage) for itkimage in itkimages] + antsimages = [iio2.from_pointer(itkimage) for itkimage in itkimages] return antsimages diff --git a/ants/utils/compose_displacement_fields.py b/ants/utils/compose_displacement_fields.py index 2eee65f8..d58cf6b1 100644 --- a/ants/utils/compose_displacement_fields.py +++ b/ants/utils/compose_displacement_fields.py @@ -1,7 +1,7 @@ __all__ = ['compose_displacement_fields'] -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 from .. import utils @@ -27,8 +27,7 @@ def compose_displacement_fields(displacement_field, libfn = utils.get_lib_fn('composeDisplacementFieldsD%i' % displacement_field.dimension) comp_field = libfn(displacement_field.pointer, warping_field.pointer) - new_image = iio.ANTsImage(pixeltype='float', dimension=displacement_field.dimension, - components=displacement_field.dimension, pointer=comp_field).clone('float') + new_image = iio2.from_pointer(comp_field).clone('float') return new_image diff --git a/ants/utils/crop_image.py b/ants/utils/crop_image.py index a65bc7c2..eba00734 100644 --- a/ants/utils/crop_image.py +++ b/ants/utils/crop_image.py @@ -7,7 +7,7 @@ from .get_mask import get_mask -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 from .. import utils @@ -52,8 +52,7 @@ def crop_image(image, label_image=None, label=1): libfn = utils.get_lib_fn('cropImage') itkimage = libfn(image.pointer, label_image.pointer, label, 0, [], []) - return iio.ANTsImage(pixeltype='float', dimension=ndim, - components=image.components, pointer=itkimage).clone(inpixeltype) + return iio2.from_pointer(itkimage).clone(inpixeltype) def crop_indices(image, lowerind, upperind): @@ -98,8 +97,7 @@ def crop_indices(image, lowerind, upperind): libfn = utils.get_lib_fn('cropImage') itkimage = libfn(image.pointer, image.pointer, 1, 2, lowerind, upperind) - ants_image = iio.ANTsImage(pixeltype='float', dimension=image.dimension, - components=image.components, pointer=itkimage) + ants_image = iio2.from_pointer(itkimage) if inpixeltype != 'float': ants_image = ants_image.clone(inpixeltype) return ants_image @@ -141,8 +139,7 @@ def decrop_image(cropped_image, full_image): libfn = utils.get_lib_fn('cropImage') itkimage = libfn(cropped_image.pointer, full_image.pointer, 1, 1, [], []) - ants_image = iio.ANTsImage(pixeltype='float', dimension=cropped_image.dimension, - components=cropped_image.components, pointer=itkimage) + ants_image = iio2.from_pointer(itkimage) if inpixeltype != 'float': ants_image = ants_image.clone(inpixeltype) diff --git a/ants/utils/fit_bspline_displacement_field.py b/ants/utils/fit_bspline_displacement_field.py index 7b69e5e6..ad6507e5 100644 --- a/ants/utils/fit_bspline_displacement_field.py +++ b/ants/utils/fit_bspline_displacement_field.py @@ -2,7 +2,7 @@ import numpy as np -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 from .. import core from .. import utils @@ -198,8 +198,6 @@ def fit_bspline_displacement_field(displacement_field=None, enforce_stationary_boundary, estimate_inverse, rasterize_points) - bspline_displacement_field = iio.ANTsImage(pixeltype='float', - dimension=dimensionality, components=dimensionality, - pointer=bspline_field).clone('float') + bspline_displacement_field = iio2.from_pointer(bspline_field).clone('float') return bspline_displacement_field diff --git a/ants/utils/fit_bspline_object_to_scattered_data.py b/ants/utils/fit_bspline_object_to_scattered_data.py index 0c88b547..442ed806 100644 --- a/ants/utils/fit_bspline_object_to_scattered_data.py +++ b/ants/utils/fit_bspline_object_to_scattered_data.py @@ -2,7 +2,7 @@ import numpy as np -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 from .. import utils @@ -183,8 +183,6 @@ def fit_bspline_object_to_scattered_data(scattered_data, if parametric_dimension == 1: return np.array(bspline_object) else: - bspline_image = iio.ANTsImage(pixeltype='float', - dimension=parametric_dimension, components=data_dimension, - pointer=bspline_object).clone('float') + bspline_image = iio2.from_pointer(bspline_object).clone('float') return bspline_image diff --git a/ants/utils/fit_thin_plate_spline_displacement_field.py b/ants/utils/fit_thin_plate_spline_displacement_field.py index 53d8399a..ef68c5f6 100644 --- a/ants/utils/fit_thin_plate_spline_displacement_field.py +++ b/ants/utils/fit_thin_plate_spline_displacement_field.py @@ -2,7 +2,7 @@ import numpy as np -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 from .. import core from .. import utils @@ -101,8 +101,6 @@ def fit_thin_plate_spline_displacement_field(displacement_origins=None, libfn = utils.get_lib_fn("fitThinPlateSplineDisplacementFieldToScatteredDataD%i" % (dimensionality)) tps_field = libfn(displacement_origins, displacements, origin, spacing, size, direction) - tps_displacement_field = iio.ANTsImage(pixeltype='float', - dimension=dimensionality, components=dimensionality, - pointer=tps_field).clone('float') + tps_displacement_field = iio2.from_pointer(tps_field).clone('float') return tps_displacement_field diff --git a/ants/utils/histogram_match_image.py b/ants/utils/histogram_match_image.py index 44f309f6..de28f21c 100644 --- a/ants/utils/histogram_match_image.py +++ b/ants/utils/histogram_match_image.py @@ -50,8 +50,7 @@ def histogram_match_image(source_image, reference_image, number_of_histogram_bin libfn = utils.get_lib_fn('histogramMatchImageF%i' % ndim) itkimage = libfn(source_image.pointer, reference_image.pointer, number_of_histogram_bins, number_of_match_points, use_threshold_at_mean_intensity) - new_image = iio.ANTsImage(pixeltype='float', dimension=ndim, - components=source_image.components, pointer=itkimage).clone(inpixeltype) + new_image = iio2.from_pointer(itkimage).clone(inpixeltype) return new_image diff --git a/ants/utils/integrate_velocity_field.py b/ants/utils/integrate_velocity_field.py index 717e86a0..44ff6815 100644 --- a/ants/utils/integrate_velocity_field.py +++ b/ants/utils/integrate_velocity_field.py @@ -1,7 +1,7 @@ __all__ = ['integrate_velocity_field'] -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 from .. import utils @@ -42,8 +42,7 @@ def integrate_velocity_field(velocity_field, integrated_field = libfn(velocity_field.pointer, lower_integration_bound, upper_integration_bound, number_of_integration_steps) - new_image = iio.ANTsImage(pixeltype='float', dimension=(velocity_field.dimension-1), - components=(velocity_field.dimension-1), pointer=integrated_field).clone('float') + new_image = iio2.from_pointer(integrated_field).clone('float') return new_image diff --git a/ants/utils/invert_displacement_field.py b/ants/utils/invert_displacement_field.py index e356768f..5c0a958b 100644 --- a/ants/utils/invert_displacement_field.py +++ b/ants/utils/invert_displacement_field.py @@ -1,7 +1,7 @@ __all__ = ['invert_displacement_field'] -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 from .. import utils @@ -45,8 +45,7 @@ def invert_displacement_field(displacement_field, maximum_number_of_iterations, mean_error_tolerance_threshold, max_error_tolerance_threshold, enforce_boundary_condition) - new_image = iio.ANTsImage(pixeltype='float', dimension=displacement_field.dimension, - components=displacement_field.dimension, pointer=inverse_field).clone('float') + new_image = iio2.from_pointer(inverse_field).clone('float') return new_image diff --git a/ants/utils/pad_image.py b/ants/utils/pad_image.py index 86905f51..dbcee476 100644 --- a/ants/utils/pad_image.py +++ b/ants/utils/pad_image.py @@ -3,7 +3,7 @@ import math -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 from .. import utils @@ -72,8 +72,7 @@ def pad_image(image, shape=None, pad_width=None, value=0.0, return_padvals=False libfn = utils.get_lib_fn('padImage') itkimage = libfn(image.pointer, lower_pad_vals, upper_pad_vals, value) - new_image = iio.ANTsImage(pixeltype='float', dimension=ndim, - components=image.components, pointer=itkimage).clone(inpixeltype) + new_image = iio2.from_pointer(itkimage).clone(inpixeltype) if return_padvals: return new_image, lower_pad_vals, upper_pad_vals else: diff --git a/ants/utils/scalar_rgb_vector.py b/ants/utils/scalar_rgb_vector.py index 444cfcd4..3679db7a 100644 --- a/ants/utils/scalar_rgb_vector.py +++ b/ants/utils/scalar_rgb_vector.py @@ -101,8 +101,7 @@ def rgb_to_vector(image): idim = image.dimension libfn = utils.get_lib_fn('RgbToVector%i' % idim) new_ptr = libfn(image.pointer) - new_img = iio.ANTsImage(pixeltype=image.pixeltype, dimension=image.dimension, - components=3, pointer=new_ptr, is_rgb=False) + new_img = iio2.from_pointer(new_ptr) return new_img @@ -132,7 +131,6 @@ def vector_to_rgb(image): idim = image.dimension libfn = utils.get_lib_fn('VectorToRgb%i' % idim) new_ptr = libfn(image.pointer) - new_img = iio.ANTsImage(pixeltype=image.pixeltype, dimension=image.dimension, - components=3, pointer=new_ptr, is_rgb=True) + new_img = iio2.from_pointer(new_ptr) return new_img diff --git a/ants/utils/simulate_displacement_field.py b/ants/utils/simulate_displacement_field.py index d0330d02..c996b3a3 100644 --- a/ants/utils/simulate_displacement_field.py +++ b/ants/utils/simulate_displacement_field.py @@ -2,7 +2,7 @@ import numpy as np -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 from .. import utils @@ -74,18 +74,14 @@ def simulate_displacement_field(domain_image, libfn = utils.get_lib_fn("simulateBsplineDisplacementField%iD" % image_dimension) field = libfn(domain_image.pointer, number_of_random_points, sd_noise, enforce_stationary_boundary, number_of_fitting_levels, number_of_control_points) - bspline_field = iio.ANTsImage(pixeltype='float', - dimension=image_dimension, components=image_dimension, - pointer=field).clone('float') + bspline_field = iio2.from_pointer(field).clone('float') return bspline_field elif field_type == 'exponential': libfn = utils.get_lib_fn("simulateExponentialDisplacementField%iD" % image_dimension) field = libfn(domain_image.pointer, number_of_random_points, sd_noise, enforce_stationary_boundary, sd_smoothing) - exp_field = iio.ANTsImage(pixeltype='float', - dimension=image_dimension, components=image_dimension, - pointer=field).clone('float') + exp_field = iio2.from_pointer(field).clone('float') return exp_field else: diff --git a/ants/utils/slice_image.py b/ants/utils/slice_image.py index 3f50c5d9..790f291f 100644 --- a/ants/utils/slice_image.py +++ b/ants/utils/slice_image.py @@ -3,7 +3,7 @@ import math -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 from .. import utils @@ -45,7 +45,6 @@ def slice_image(image, axis, idx, collapse_strategy=0): libfn = utils.get_lib_fn('sliceImage') itkimage = libfn(image.pointer, axis, idx, collapse_strategy) - return iio.ANTsImage(pixeltype='float', dimension=ndim-1, - components=image.components, pointer=itkimage).clone(inpixeltype) + return iio2.from_pointer(itkimage).clone(inpixeltype) diff --git a/ants/utils/smooth_image.py b/ants/utils/smooth_image.py index 0b5c55c1..e82ac345 100644 --- a/ants/utils/smooth_image.py +++ b/ants/utils/smooth_image.py @@ -6,7 +6,7 @@ import math from .. import utils -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 @@ -26,8 +26,7 @@ def _smooth_image_helper(image, sigma, sigma_in_physical_coordinates=True, FWHM= smooth_image_fn = utils.get_lib_fn('SmoothImage') outimage = smooth_image_fn(image_float.pointer, sigma, sigma_in_physical_coordinates, max_kernel_width) - ants_outimage = iio.ANTsImage(pixeltype='float', dimension=image.dimension, - components=image.components, pointer=outimage) + ants_outimage = iio2.from_pointer(outimage) return ants_outimage diff --git a/ants/utils/weingarten_image_curvature.py b/ants/utils/weingarten_image_curvature.py index 393af1d1..a8a44c39 100644 --- a/ants/utils/weingarten_image_curvature.py +++ b/ants/utils/weingarten_image_curvature.py @@ -4,7 +4,7 @@ import numpy as np from .. import core -from ..core import ants_image as iio +from ..core import ants_image as iio, ants_image_io as iio2 from .. import utils @@ -60,8 +60,7 @@ def weingarten_image_curvature(image, sigma=1.0, opt='mean'): libfn = utils.get_lib_fn('weingartenImageCurvature') mykout = libfn(temp.pointer, sigma, optnum) - mykout = iio.ANTsImage(pixeltype=image.pixeltype, dimension=3, - components=image.components, pointer=mykout) + mykout = iio2.from_pointer(mykout) if image.dimension == 3: return mykout elif image.dimension == 2: diff --git a/src/antsImage.cxx b/src/antsImage.cxx index d517fc8e..44106ddb 100644 --- a/src/antsImage.cxx +++ b/src/antsImage.cxx @@ -96,6 +96,19 @@ void local_antsImage(nb::module_ &m) { m.def("ptrstr", &ptrstr,2>>); m.def("ptrstr", &ptrstr,3>>); + m.def("getComponents", &getComponents>); + m.def("getComponents", &getComponents>); + m.def("getComponents", &getComponents>); + m.def("getComponents", &getComponents>); + m.def("getComponents", &getComponents>); + m.def("getComponents", &getComponents>); + m.def("getComponents", &getComponents>); + m.def("getComponents", &getComponents>); + m.def("getComponents", &getComponents>); + m.def("getComponents", &getComponents>); + m.def("getComponents", &getComponents>); + m.def("getComponents", &getComponents>); + m.def("getShape", &getShape>); m.def("getShape", &getShape>); m.def("getShape", &getShape>); diff --git a/src/antsImage.h b/src/antsImage.h index 6a146b70..ad55b91f 100644 --- a/src/antsImage.h +++ b/src/antsImage.h @@ -89,6 +89,13 @@ std::list getShape( AntsImage & myPointer ) } +template +int getComponents( AntsImage & myPointer ) +{ + typename ImageType::Pointer image = myPointer.ptr; + return image->GetNumberOfComponentsPerPixel(); +} + template std::vector getOrigin( AntsImage & myPointer ) {