Skip to content

Commit

Permalink
wrap more utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ncullen93 committed May 10, 2024
1 parent 913d618 commit e71582c
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 83 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"valarray": "cpp",
"variant": "cpp",
"*.in": "cpp",
"*.inc": "cpp"
"*.inc": "cpp",
"*.tmpl": "cpp"
}
}
8 changes: 4 additions & 4 deletions ants/utils/add_noise_to_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
27 changes: 16 additions & 11 deletions src/LOCAL_addNoiseToImage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,64 +24,69 @@ namespace nb = nanobind;
using namespace nb::literals;

template <typename ImageType>
void * additiveGaussianNoise( typename ImageType::Pointer itkImage,
AntsImage<ImageType> additiveGaussianNoise( AntsImage<ImageType> & antsImage,
float mean,
float standardDeviation )
{
typename ImageType::Pointer itkImage = antsImage.ptr;
using NoiseFilterType = itk::AdditiveGaussianNoiseImageFilter<ImageType, ImageType>;
typename NoiseFilterType::Pointer noiser = NoiseFilterType::New();
noiser->SetInput( itkImage );
noiser->SetMean( mean );
noiser->SetStandardDeviation( standardDeviation );
noiser->Update();

return wrap<ImageType>( noiser->GetOutput() );
AntsImage<ImageType> outImage = { noiser->GetOutput() };
return outImage;
}

template <typename ImageType>
void * saltAndPepperNoise( typename ImageType::Pointer itkImage,
AntsImage<ImageType> saltAndPepperNoise( AntsImage<ImageType> & antsImage,
float probability,
float saltValue,
float pepperValue )
{
typename ImageType::Pointer itkImage = antsImage.ptr;
using NoiseFilterType = itk::SaltAndPepperNoiseImageFilter<ImageType, ImageType>;
typename NoiseFilterType::Pointer noiser = NoiseFilterType::New();
noiser->SetInput( itkImage );
noiser->SetProbability( probability );
noiser->SetSaltValue( saltValue );
noiser->SetPepperValue( pepperValue );
noiser->Update();

return wrap<ImageType>( noiser->GetOutput() );
AntsImage<ImageType> outImage = { noiser->GetOutput() };
return outImage;
}

template <typename ImageType>
void * shotNoise( typename ImageType::Pointer itkImage,
AntsImage<ImageType> shotNoise( AntsImage<ImageType> & antsImage,
float scale
)
{
typename ImageType::Pointer itkImage = antsImage.ptr;
using NoiseFilterType = itk::ShotNoiseImageFilter<ImageType, ImageType>;
typename NoiseFilterType::Pointer noiser = NoiseFilterType::New();
noiser->SetInput( itkImage );
noiser->SetScale( scale );
noiser->Update();

return wrap<ImageType>( noiser->GetOutput() );
AntsImage<ImageType> outImage = { noiser->GetOutput() };
return outImage;
}

template <typename ImageType>
void * speckleNoise( typename ImageType::Pointer itkImage,
AntsImage<ImageType> speckleNoise( AntsImage<ImageType> & antsImage,
float scale
)
{

typename ImageType::Pointer itkImage = antsImage.ptr;
using NoiseFilterType = itk::SpeckleNoiseImageFilter<ImageType, ImageType>;
typename NoiseFilterType::Pointer noiser = NoiseFilterType::New();
noiser->SetInput( itkImage );
noiser->SetStandardDeviation( scale );
noiser->Update();

return wrap<ImageType>( noiser->GetOutput() );
AntsImage<ImageType> outImage = { noiser->GetOutput() };
return outImage;
}

void local_addNoiseToImage(nb::module_ &m)
Expand Down
60 changes: 33 additions & 27 deletions src/LOCAL_fitThinPlateSplineDisplacementFieldToScatteredData.cxx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include <nanobind/nanobind.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/tuple.h>
#include <nanobind/stl/list.h>
#include <nanobind/ndarray.h>
#include <nanobind/stl/shared_ptr.h>

#include <exception>
#include <vector>
Expand All @@ -14,16 +18,17 @@

#include "LOCAL_antsImage.h"

namespace py = pybind11;
namespace nb = nanobind;
using namespace nb::literals;

template<unsigned int Dimension>
py::capsule fitThinPlateSplineVectorImageToScatteredDataHelper(
py::array_t<double> displacementOrigins,
py::array_t<double> displacements,
py::array_t<double> origin,
py::array_t<double> spacing,
py::array_t<unsigned int> size,
py::array_t<double> direction
AntsImage<itk::VectorImage<float, Dimension>> fitThinPlateSplineVectorImageToScatteredDataHelper(
std::vector<std::vector<double>> displacementOrigins,
std::vector<std::vector<double>> displacements,
std::vector<double> origin,
std::vector<double> spacing,
std::vector<unsigned int> size,
std::vector<std::vector<double>> direction
)
{
using RealType = float;
Expand All @@ -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." );
}
Expand All @@ -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 );
Expand All @@ -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 );
Expand Down Expand Up @@ -138,10 +143,11 @@ py::capsule fitThinPlateSplineVectorImageToScatteredDataHelper(
antsField->SetPixel( It.GetIndex(), antsVector );
}

return wrap< ANTsFieldType >( antsField );
AntsImage<ANTsFieldType> 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>);
Expand Down
25 changes: 16 additions & 9 deletions src/LOCAL_histogramMatchImages.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <nanobind/nanobind.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/tuple.h>
#include <nanobind/stl/list.h>
#include <nanobind/ndarray.h>
#include <nanobind/stl/shared_ptr.h>

#include <exception>
#include <vector>
Expand All @@ -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<ImageType> histogramMatchImage( AntsImage<ImageType> & antsSourceImage,
AntsImage<ImageType> & 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<ImageType, ImageType> FilterType;
typename FilterType::Pointer filter = FilterType::New();
Expand All @@ -37,10 +43,11 @@ py::capsule histogramMatchImage( py::capsule & antsSourceImage,
filter->SetNumberOfMatchPoints( numberOfMatchPoints );
filter->Update();

return wrap< ImageType >( filter->GetOutput() );
AntsImage<ImageType> out_ants_image = { filter->GetOutput() };
return out_ants_image;
}

PYBIND11_MODULE(histogramMatchImage, m)
void local_histogramMatchImages(nb::module_ &m)
{
m.def("histogramMatchImageF2", &histogramMatchImage<itk::Image<float, 2>>);
m.def("histogramMatchImageF3", &histogramMatchImage<itk::Image<float, 3>>);
Expand Down
53 changes: 30 additions & 23 deletions src/LOCAL_labelStats.cxx
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <nanobind/nanobind.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/tuple.h>
#include <nanobind/stl/list.h>
#include <nanobind/ndarray.h>
#include <nanobind/stl/shared_ptr.h>

#include "itkImage.h"
#include "itkLabelStatisticsImageFilter.h"
#include "itkImageRegionIteratorWithIndex.h"

#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)
{
Expand Down Expand Up @@ -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 <unsigned int Dimension>
py::dict labelStats(py::capsule py_image,
py::capsule py_labelImage)
nb::dict labelStats(AntsImage<itk::Image<float, Dimension>> & py_image,
AntsImage<itk::Image<unsigned int, Dimension>> & py_labelImage)
{
typedef itk::Image<float, Dimension> FloatImageType;
typedef itk::Image<unsigned int, Dimension> IntImageType;
typedef typename FloatImageType::Pointer FloatImagePointerType;
typedef typename IntImageType::Pointer IntImagePointerType;

FloatImagePointerType myimage = as<itk::Image<float, Dimension>>( py_image );
IntImagePointerType mylabelimage = as<itk::Image<unsigned int, Dimension>>( py_labelImage );

FloatImagePointerType myimage = py_image.ptr;
IntImagePointerType mylabelimage = py_labelImage.ptr;

return labelStatsHelper<Dimension>( myimage, mylabelimage );
}


PYBIND11_MODULE(labelStats, m)
void local_labelStats(nb::module_ &m)
{
m.def("labelStats2D", &labelStats<2>);
m.def("labelStats3D", &labelStats<3>);
Expand Down
Loading

0 comments on commit e71582c

Please sign in to comment.