[ITK-users] Encapsulate a ITK function into a DLL to be used with C#

Bradley Lowekamp brad at lowekamp.net
Sat Nov 22 12:06:09 EST 2014


Hello

Have you looked into SimpleITK's C Sharp bindings?


> On Nov 22, 2014, at 7:31 AM, Matias Montroull <matimontg at gmail.com> wrote:
> 
> Hi,
> 
> I have the following code that I would like to use in C#. Since it is ITK, it's coded in C++ so I was wondering if I could create a DLL that can be used in C# and then call the function as it was a class, pass parameters and use the method?
> 
> All I need is to pass 3 parameters to the function (which is a function to rotate images)
> 
> here's the code:
> 
> #include "itkImage.h"
> #include "itkImageFileReader.h"
> #include "itkImageFileWriter.h"
> #include "itkResampleImageFilter.h"
> #include "itkAffineTransform.h"
> #include "itkGDCMImageIO.h"
> 
> int main( int argc, char * argv[] )
> {
> if( argc < 4 )
> {
> std::cerr << "Usage: " << std::endl;
> std::cerr << argv[0] << " inputImageFile outputImageFile degrees" << std::endl;
> return EXIT_FAILURE;
> }
> 
> const unsigned int Dimension = 2; //imagen 2D
> typedef signed short InputPixelType;
> typedef signed short OutputPixelType;
> typedef itk::Image< InputPixelType, Dimension > InputImageType;
> typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
> typedef itk::ImageFileReader< InputImageType > ReaderType;
> typedef itk::ImageFileWriter< OutputImageType > WriterType;
> ReaderType::Pointer reader = ReaderType::New(); //lo que lee
> WriterType::Pointer writer = WriterType::New(); //lo que escribe
> reader->SetFileName( argv[1] ); //primer argumento
> writer->SetFileName( argv[2] ); //segundo argumento
> 
> typedef itk::GDCMImageIO ImageIOType;
> ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
> reader->SetImageIO( gdcmImageIO );
> 
> 
> const double angleInDegrees = atof( argv[3] ); //angulo de rotación
> 
> typedef itk::ResampleImageFilter<InputImageType, OutputImageType > FilterType; //Filtro
> FilterType::Pointer filter = FilterType::New();
> 
> typedef itk::AffineTransform< double, Dimension > TransformType; //usado para mapear el espacio de entrada con el espacio de salida
> TransformType::Pointer transform = TransformType::New();
> 
> typedef itk::LinearInterpolateImageFunction<InputImageType, double > InterpolatorType;
> InterpolatorType::Pointer interpolator = InterpolatorType::New();
> filter->SetInterpolator( interpolator );
> filter->SetDefaultPixelValue(0); //Pixel por defecto de lo que queda fuera de la imagen cuando se rota
> 
> reader->Update();
> 
> const InputImageType * inputImage = reader->GetOutput();
> const InputImageType::SpacingType & spacing = inputImage->GetSpacing();
> const InputImageType::PointType & origin = inputImage->GetOrigin();
> InputImageType::SizeType size =
> inputImage->GetLargestPossibleRegion().GetSize();
> filter->SetOutputOrigin( origin );
> filter->SetOutputSpacing( spacing );
> filter->SetOutputDirection( inputImage->GetDirection() );
> filter->SetSize( size );
> 
> filter->SetInput( reader->GetOutput() );
> writer->SetInput( filter->GetOutput() );
> writer->UseInputMetaDataDictionaryOff();
> writer->SetImageIO(gdcmImageIO);
>  TransformType::OutputVectorType translation1;
> const double imageCenterX = origin[0] + spacing[0] * size[0] / 2.0;
> const double imageCenterY = origin[1] + spacing[1] * size[1] / 2.0;
> translation1[0] = -imageCenterX;
> translation1[1] = -imageCenterY;
> transform->Translate( translation1 );
> // Software Guide : EndCodeSnippet
> std::cout << "imageCenterX = " << imageCenterX << std::endl;
> std::cout << "imageCenterY = " << imageCenterY << std::endl;
> 
> const double degreesToRadians = std::atan(1.0) / 45.0;
> const double angle = angleInDegrees * degreesToRadians;
> transform->Rotate2D( -angle, false );
> 
> TransformType::OutputVectorType translation2;
> translation2[0] = imageCenterX;
> translation2[1] = imageCenterY;
> transform->Translate( translation2, false );
> filter->SetTransform( transform );
> 
> try
> {
> writer->Update();
> }
> catch( itk::ExceptionObject & excep )
> {
> std::cerr << "Exception caught !" << std::endl;
> std::cerr << excep << std::endl;
> }
> // Software Guide : EndCodeSnippet
> return EXIT_SUCCESS;
> }
> 
> _____________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> 
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.php
> 
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/insight-users


More information about the Insight-users mailing list