diff --git a/.vscode/settings.json b/.vscode/settings.json index 4d64aeb5..0383074a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -84,6 +84,7 @@ "valarray": "cpp", "variant": "cpp", "*.in": "cpp", - "*.inc": "cpp" + "*.inc": "cpp", + "*.tmpl": "cpp" } } diff --git a/ants/utils/add_noise_to_image.py b/ants/utils/add_noise_to_image.py index 7bc57f30..5a2dd7a7 100644 --- a/ants/utils/add_noise_to_image.py +++ b/ants/utils/add_noise_to_image.py @@ -45,7 +45,7 @@ def add_noise_to_image(image, if len(noise_parameters) != 2: raise ValueError("Incorrect number of parameters.") - libfn = utils.get_lib_fn("additiveGaussianNoiseF%i" % image_dimension) + 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, @@ -55,7 +55,7 @@ def add_noise_to_image(image, if len(noise_parameters) != 3: raise ValueError("Incorrect number of parameters.") - libfn = utils.get_lib_fn("saltAndPepperNoiseF%i" % image_dimension) + 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, @@ -65,7 +65,7 @@ def add_noise_to_image(image, if not isinstance(noise_parameters, (int, float)): raise ValueError("Incorrect parameter specification.") - libfn = utils.get_lib_fn("shotNoiseF%i" % image_dimension) + libfn = utils.get_lib_fn("shotNoise") noise = libfn(image.pointer, noise_parameters) output_image = iio.ANTsImage(pixeltype='float', dimension=image_dimension, components=1, @@ -75,7 +75,7 @@ def add_noise_to_image(image, if not isinstance(noise_parameters, (int, float)): raise ValueError("Incorrect parameter specification.") - libfn = utils.get_lib_fn("speckleNoiseF%i" % image_dimension) + libfn = utils.get_lib_fn("speckleNoise") noise = libfn(image.pointer, noise_parameters) output_image = iio.ANTsImage(pixeltype='float', dimension=image_dimension, components=1, diff --git a/src/LOCAL_addNoiseToImage.cxx b/src/LOCAL_addNoiseToImage.cxx index c04ab59f..69a7675c 100644 --- a/src/LOCAL_addNoiseToImage.cxx +++ b/src/LOCAL_addNoiseToImage.cxx @@ -24,26 +24,28 @@ namespace nb = nanobind; using namespace nb::literals; template -void * additiveGaussianNoise( typename ImageType::Pointer itkImage, +AntsImage additiveGaussianNoise( AntsImage & antsImage, float mean, float standardDeviation ) { + typename ImageType::Pointer itkImage = antsImage.ptr; using NoiseFilterType = itk::AdditiveGaussianNoiseImageFilter; typename NoiseFilterType::Pointer noiser = NoiseFilterType::New(); noiser->SetInput( itkImage ); noiser->SetMean( mean ); noiser->SetStandardDeviation( standardDeviation ); noiser->Update(); - - return wrap( noiser->GetOutput() ); + AntsImage outImage = { noiser->GetOutput() }; + return outImage; } template -void * saltAndPepperNoise( typename ImageType::Pointer itkImage, +AntsImage saltAndPepperNoise( AntsImage & antsImage, float probability, float saltValue, float pepperValue ) { + typename ImageType::Pointer itkImage = antsImage.ptr; using NoiseFilterType = itk::SaltAndPepperNoiseImageFilter; typename NoiseFilterType::Pointer noiser = NoiseFilterType::New(); noiser->SetInput( itkImage ); @@ -51,37 +53,40 @@ void * saltAndPepperNoise( typename ImageType::Pointer itkImage, noiser->SetSaltValue( saltValue ); noiser->SetPepperValue( pepperValue ); noiser->Update(); - - return wrap( noiser->GetOutput() ); + AntsImage outImage = { noiser->GetOutput() }; + return outImage; } template -void * shotNoise( typename ImageType::Pointer itkImage, +AntsImage shotNoise( AntsImage & antsImage, float scale ) { + typename ImageType::Pointer itkImage = antsImage.ptr; using NoiseFilterType = itk::ShotNoiseImageFilter; typename NoiseFilterType::Pointer noiser = NoiseFilterType::New(); noiser->SetInput( itkImage ); noiser->SetScale( scale ); noiser->Update(); - return wrap( noiser->GetOutput() ); + AntsImage outImage = { noiser->GetOutput() }; + return outImage; } template -void * speckleNoise( typename ImageType::Pointer itkImage, +AntsImage speckleNoise( AntsImage & antsImage, float scale ) { - + typename ImageType::Pointer itkImage = antsImage.ptr; using NoiseFilterType = itk::SpeckleNoiseImageFilter; typename NoiseFilterType::Pointer noiser = NoiseFilterType::New(); noiser->SetInput( itkImage ); noiser->SetStandardDeviation( scale ); noiser->Update(); - return wrap( noiser->GetOutput() ); + AntsImage outImage = { noiser->GetOutput() }; + return outImage; } void local_addNoiseToImage(nb::module_ &m) diff --git a/src/LOCAL_fitThinPlateSplineDisplacementFieldToScatteredData.cxx b/src/LOCAL_fitThinPlateSplineDisplacementFieldToScatteredData.cxx index 94ff15f0..d31e511d 100644 --- a/src/LOCAL_fitThinPlateSplineDisplacementFieldToScatteredData.cxx +++ b/src/LOCAL_fitThinPlateSplineDisplacementFieldToScatteredData.cxx @@ -1,7 +1,11 @@ -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -14,16 +18,17 @@ #include "LOCAL_antsImage.h" -namespace py = pybind11; +namespace nb = nanobind; +using namespace nb::literals; template -py::capsule fitThinPlateSplineVectorImageToScatteredDataHelper( - py::array_t displacementOrigins, - py::array_t displacements, - py::array_t origin, - py::array_t spacing, - py::array_t size, - py::array_t direction +AntsImage> fitThinPlateSplineVectorImageToScatteredDataHelper( + std::vector> displacementOrigins, + std::vector> displacements, + std::vector origin, + std::vector spacing, + std::vector size, + std::vector> direction ) { using RealType = float; @@ -50,12 +55,12 @@ py::capsule fitThinPlateSplineVectorImageToScatteredDataHelper( auto field = ITKFieldType::New(); - auto originP = origin.unchecked<1>(); - auto spacingP = spacing.unchecked<1>(); - auto sizeP = size.unchecked<1>(); - auto directionP = direction.unchecked<2>(); + auto originP = origin;//.unchecked<1>(); + auto spacingP = spacing;//.unchecked<1>(); + auto sizeP = size;//.unchecked<1>(); + auto directionP = direction;//.unchecked<2>(); - if( originP.shape(0) == 0 || sizeP.shape(0) == 0 || spacingP.shape(0) == 0 || directionP.shape(0) == 0 ) + if( originP.size() == 0 || sizeP.size() == 0 || spacingP.size() == 0 || directionP.size() == 0 ) { throw std::invalid_argument( "Thin-plate spline domain is not specified." ); } @@ -68,12 +73,12 @@ py::capsule fitThinPlateSplineVectorImageToScatteredDataHelper( for( unsigned int d = 0; d < Dimension; d++ ) { - fieldOrigin[d] = originP(d); - fieldSpacing[d] = spacingP(d); - fieldSize[d] = sizeP(d); + fieldOrigin[d] = originP[d]; + fieldSpacing[d] = spacingP[d]; + fieldSize[d] = sizeP[d]; for( unsigned int e = 0; e < Dimension; e++ ) { - fieldDirection(d, e) = directionP(d, e); + fieldDirection[d][e] = directionP[d][e]; } } field->SetRegions( fieldSize ); @@ -91,16 +96,16 @@ py::capsule fitThinPlateSplineVectorImageToScatteredDataHelper( PointType sourcePoint; PointType targetPoint; - auto displacementOriginsP = displacementOrigins.unchecked<2>(); - auto displacementsP = displacements.unchecked<2>(); - unsigned int numberOfPoints = displacementsP.shape(0); + auto displacementOriginsP = displacementOrigins;//.unchecked<2>(); + auto displacementsP = displacements;//.unchecked<2>(); + unsigned int numberOfPoints = displacementsP.size();//.shape(0); for( unsigned int n = 0; n < numberOfPoints; n++ ) { for( unsigned int d = 0; d < Dimension; d++ ) { - sourcePoint[d] = displacementOriginsP(n, d); - targetPoint[d] = displacementOriginsP(n, d) + displacementsP(n, d); + sourcePoint[d] = displacementOriginsP[n][d]; + targetPoint[d] = displacementOriginsP[n][d] + displacementsP[n][d]; } sourceLandmarkContainer->InsertElement( n, sourcePoint ); targetLandmarkContainer->InsertElement( n, targetPoint ); @@ -138,10 +143,11 @@ py::capsule fitThinPlateSplineVectorImageToScatteredDataHelper( antsField->SetPixel( It.GetIndex(), antsVector ); } - return wrap< ANTsFieldType >( antsField ); + AntsImage out_ants_image = { antsField }; + return out_ants_image; } -PYBIND11_MODULE(fitThinPlateSplineDisplacementFieldToScatteredData, m) +void local_fitThinPlateSplineDisplacementFieldToScatteredData(nb::module_ &m) { m.def("fitThinPlateSplineDisplacementFieldToScatteredDataD2", &fitThinPlateSplineVectorImageToScatteredDataHelper<2>); m.def("fitThinPlateSplineDisplacementFieldToScatteredDataD3", &fitThinPlateSplineVectorImageToScatteredDataHelper<3>); diff --git a/src/LOCAL_histogramMatchImages.cxx b/src/LOCAL_histogramMatchImages.cxx index 1ae95d01..9fea7252 100644 --- a/src/LOCAL_histogramMatchImages.cxx +++ b/src/LOCAL_histogramMatchImages.cxx @@ -1,6 +1,11 @@ -#include -#include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -11,18 +16,19 @@ #include "LOCAL_antsImage.h" -namespace py = pybind11; +namespace nb = nanobind; +using namespace nb::literals; template < typename ImageType > -py::capsule histogramMatchImage( py::capsule & antsSourceImage, - py::capsule & antsReferenceImage, +AntsImage histogramMatchImage( AntsImage & antsSourceImage, + AntsImage & antsReferenceImage, unsigned int numberOfHistogramBins, unsigned int numberOfMatchPoints, bool useThresholdAtMeanIntensity ) { typedef typename ImageType::Pointer ImagePointerType; - ImagePointerType itkSourceImage = as< ImageType >( antsSourceImage ); - ImagePointerType itkReferenceImage = as< ImageType >( antsReferenceImage ); + ImagePointerType itkSourceImage = antsSourceImage.ptr; + ImagePointerType itkReferenceImage = antsReferenceImage.ptr; typedef itk::HistogramMatchingImageFilter FilterType; typename FilterType::Pointer filter = FilterType::New(); @@ -37,10 +43,11 @@ py::capsule histogramMatchImage( py::capsule & antsSourceImage, filter->SetNumberOfMatchPoints( numberOfMatchPoints ); filter->Update(); - return wrap< ImageType >( filter->GetOutput() ); + AntsImage out_ants_image = { filter->GetOutput() }; + return out_ants_image; } -PYBIND11_MODULE(histogramMatchImage, m) +void local_histogramMatchImages(nb::module_ &m) { m.def("histogramMatchImageF2", &histogramMatchImage>); m.def("histogramMatchImageF3", &histogramMatchImage>); diff --git a/src/LOCAL_labelStats.cxx b/src/LOCAL_labelStats.cxx index b66c9734..e6e2461f 100644 --- a/src/LOCAL_labelStats.cxx +++ b/src/LOCAL_labelStats.cxx @@ -1,6 +1,11 @@ -#include -#include +#include +#include +#include +#include +#include +#include +#include #include "itkImage.h" #include "itkLabelStatisticsImageFilter.h" @@ -8,11 +13,11 @@ #include "LOCAL_antsImage.h" -namespace py = pybind11; -using namespace py::literals; +namespace nb = nanobind; +using namespace nb::literals; template< unsigned int Dimension > -py::dict labelStatsHelper( +nb::dict labelStatsHelper( typename itk::Image< float, Dimension >::Pointer image, typename itk::Image< unsigned int, Dimension>::Pointer labelImage) { @@ -112,38 +117,40 @@ py::dict labelStatsHelper( if ( Dimension > 3 ) t[labelcount]=comvec[ labelcount ][3]; } - py::dict labelStats = py::dict( "LabelValue"_a=labelvals, - "Mean"_a=means, - "Min"_a=mins, - "Max"_a=maxes, - "Variance"_a=variances, - "Count"_a=counts, - "Volume"_a=volumes, - "Mass"_a=mass, - "x"_a=x, - "y"_a=y, - "z"_a=z, - "t"_a=t ); + nb::dict labelStats; + labelStats["LabelValue"] = labelvals; + labelStats["Mean"] = means; + labelStats["Min"] = mins; + labelStats["Max"] = maxes; + labelStats["Variance"] = variances; + labelStats["Count"] = counts; + labelStats["Volume"] = volumes; + labelStats["Mass"] = mass; + labelStats["x"] = x; + labelStats["y"] = y; + labelStats["z"] = z; + labelStats["t"] = t; + return (labelStats); } template -py::dict labelStats(py::capsule py_image, - py::capsule py_labelImage) +nb::dict labelStats(AntsImage> & py_image, + AntsImage> & py_labelImage) { typedef itk::Image FloatImageType; typedef itk::Image IntImageType; typedef typename FloatImageType::Pointer FloatImagePointerType; typedef typename IntImageType::Pointer IntImagePointerType; - FloatImagePointerType myimage = as>( py_image ); - IntImagePointerType mylabelimage = as>( py_labelImage ); + + FloatImagePointerType myimage = py_image.ptr; + IntImagePointerType mylabelimage = py_labelImage.ptr; return labelStatsHelper( myimage, mylabelimage ); } - -PYBIND11_MODULE(labelStats, m) +void local_labelStats(nb::module_ &m) { m.def("labelStats2D", &labelStats<2>); m.def("labelStats3D", &labelStats<3>); diff --git a/src/LOCAL_weingartenImageCurvature.cxx b/src/LOCAL_weingartenImageCurvature.cxx index cd88b15f..c4c26278 100644 --- a/src/LOCAL_weingartenImageCurvature.cxx +++ b/src/LOCAL_weingartenImageCurvature.cxx @@ -1,6 +1,12 @@ -#include -#include +#include +#include +#include +#include +#include +#include +#include + #include #include @@ -13,10 +19,10 @@ #include "LOCAL_antsImage.h" +namespace nb = nanobind; +using namespace nb::literals; -namespace py = pybind11; - -py::capsule weingartenImageCurvature( py::capsule myimage, +AntsImage> weingartenImageCurvature( AntsImage> myimage, float sigma, int opt ) { typedef itk::Image ImageType; @@ -25,7 +31,7 @@ py::capsule weingartenImageCurvature( py::capsule myimage, enum { ImageDimension = ImageType::ImageDimension }; typedef itk::SurfaceImageCurvature ParamType; typename ParamType::Pointer Parameterizer = ParamType::New(); - typename ImageType::Pointer input = as( myimage ); + typename ImageType::Pointer input = myimage.ptr; typename ImageType::DirectionType imgdir = input->GetDirection(); typename ImageType::DirectionType iddir = input->GetDirection(); iddir.SetIdentity(); @@ -52,12 +58,14 @@ py::capsule weingartenImageCurvature( py::capsule myimage, } typename ImageType::Pointer output = Parameterizer->GetFunctionImage(); output->SetDirection( imgdir ); - return wrap( output ); + + AntsImage outImage = { output }; + return outImage; } -PYBIND11_MODULE(weingartenImageCurvature, m) +void local_weingartenImageCurvature(nb::module_ &m) { m.def("weingartenImageCurvature", &weingartenImageCurvature); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9bc5a1f8..76597b03 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,11 +11,14 @@ #include "LOCAL_cropImage.cxx" #include "LOCAL_fitBsplineDisplacementFieldToScatteredData.cxx" #include "LOCAL_fitBsplineObjectToScatteredData.cxx" +#include "LOCAL_fitThinPlateSplineDisplacementFieldToScatteredData.cxx" #include "LOCAL_fsl2antstransform.cxx" #include "LOCAL_getNeighborhoodMatrix.cxx" +#include "LOCAL_histogramMatchImages.cxx" #include "LOCAL_integrateVelocityField.cxx" #include "LOCAL_invertDisplacementField.cxx" #include "LOCAL_labelOverlapMeasures.cxx" +#include "LOCAL_labelStats.cxx" #include "LOCAL_mergeChannels.cxx" #include "LOCAL_padImage.cxx" #include "LOCAL_readImage.cxx" @@ -25,6 +28,7 @@ #include "LOCAL_reorientImage2.cxx" #include "LOCAL_sliceImage.cxx" #include "LOCAL_SmoothImage.cxx" +#include "LOCAL_weingartenImageCurvature.cxx" #include "WRAP_antsAffineInitializer.cxx" #include "WRAP_antsApplyTransforms.cxx" @@ -59,11 +63,14 @@ void local_cropImage(nb::module_ &); void local_composeDisplacementFields(nb::module_ &); void local_fitBsplineDisplacementFieldToScatteredData(nb::module_ &); void local_fitBsplineObjectToScatteredData(nb::module_ &); +void local_fitThinPlateSplineDisplacementFieldToScatteredData(nb::module_ &); void local_fsl2antstransform(nb::module_ &); void local_getNeighborhoodMatrix(nb::module_ &); +void local_histogramMatchImages(nb::module_ &); void local_integrateVelocityField(nb::module_ &); void local_invertDisplacementField(nb::module_ &); void local_labelOverlapMeasures(nb::module_ &); +void local_labelStats(nb::module_ &); void local_mergeChannels(nb::module_ &); void local_padImage(nb::module_ &); void local_readImage(nb::module_ &); @@ -73,6 +80,7 @@ void local_reorientImage(nb::module_ &); void local_reorientImage2(nb::module_ &); void local_sliceImage(nb::module_ &); void local_SmoothImage(nb::module_ &); +void local_weingartenImageCurvature(nb::module_ &); void wrap_antsAffineInitializer(nb::module_ &); void wrap_antsApplyTransforms(nb::module_ &); @@ -106,11 +114,14 @@ NB_MODULE(lib, m) { local_cropImage(m); local_fitBsplineDisplacementFieldToScatteredData(m); local_fitBsplineObjectToScatteredData(m); + local_fitThinPlateSplineDisplacementFieldToScatteredData(m); local_fsl2antstransform(m); local_getNeighborhoodMatrix(m); + local_histogramMatchImages(m); local_integrateVelocityField(m); local_invertDisplacementField(m); local_labelOverlapMeasures(m); + local_labelStats(m); local_mergeChannels(m); local_padImage(m); local_readImage(m); @@ -120,6 +131,7 @@ NB_MODULE(lib, m) { local_reorientImage2(m); local_sliceImage(m); local_SmoothImage(m); + local_weingartenImageCurvature(m); wrap_antsAffineInitializer(m); wrap_antsApplyTransforms(m);