ITK/Using ITK from .NET: Difference between revisions

From KitwarePublic
< ITK
Jump to navigationJump to search
(Added note about building with the same target platform)
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
This page describes how to use ITK from .NET.
This page describes how to use ITK from .NET.
<br>
<br>
'''NOTE: You must build ITK, the wrapper DLL, and the example .NET exe all using the same target platform i.e. Win32/x86 or Win64/x64'''
'''NOTE: You must build ITK, wrapper DLL, and .NET exe all using the same target platform i.e. Win32/x86 or Win64/x64'''
<ol>
<ol>
<li>
<li>
Line 112: Line 112:
<li>
<li>
Run SWIG using the following command: <code style="font-size: medium">swig -c++ -csharp -dllimport ExampleWrapper example.i</code>
Run SWIG using the following command: <code style="font-size: medium">swig -c++ -csharp -dllimport ExampleWrapper example.i</code>
<br>
The following files should be generated:
* <code style="font-size: medium">example_wrap.cxx</code>
* <code style="font-size: medium">example.cs</code>
* <code style="font-size: medium">examplePINVOKE.cs</code>
</li>
</li>
<li>
<li>
Line 117: Line 122:
</li>
</li>
<li>
<li>
Create the following <code style="font-size: medium">runme.cs</code> C# file:
Create the following C# <code style="font-size: medium">runme.cs</code> file:
<div style="background: white; border: 1px dashed rgb(100, 100, 100); padding: 5px; margin: 10px 0px 10px 0px; font-size: medium">
<div style="background: white; border: 1px dashed rgb(100, 100, 100); padding: 5px; margin: 10px 0px 10px 0px; font-size: medium">
<syntaxhighlight lang="csharp">
<syntaxhighlight lang="csharp">
Line 141: Line 146:
</li>
</li>
<li>
<li>
Build the C# executable. For Mono use the following: <code style="font-size: medium">mcs -platform:x86 -target:exe -out:example.exe *.cs</code>
Build the .NET executable. For Mono use the following: <code style="font-size: medium">mcs -platform:x86 -target:exe -out:example.exe runme.cs example.cs examplePINVOKE.cs</code>
</li>
<li>
Run the .NET executable. Make sure it can find <code style="font-size: medium">ExampleWrapper.dll</code> (the easiest way is to copy the dll to the same folder as <code style="font-size: medium">example.exe</code>)
</li>
</li>
</ol>
</ol>

Latest revision as of 23:29, 27 November 2012

This page describes how to use ITK from .NET.
NOTE: You must build ITK, wrapper DLL, and .NET exe all using the same target platform i.e. Win32/x86 or Win64/x64

  1. Download CMake, ITK source code, and SWIG
  2. Build ITK (see here for help)
  3. Create the following CMakeLists.txt file:

    <syntaxhighlight lang="make">

    1. File: CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)

    project(ExampleWrapper)

    find_package(ITK REQUIRED) include(${ITK_USE_FILE})

    add_library(ExampleWrapper SHARED example.h example.hxx example_wrap.cxx) target_link_libraries(ExampleWrapper ${ITK_LIBRARIES}) </syntaxhighlight>

  4. Create the following example.h file:

    <syntaxhighlight lang="cpp"> // File : example.h

    1. ifndef __example_h
    2. define __example_h
    1. include <string>

    template<class TPixel, unsigned int VDimension> void SmoothImage(

       const std::string input, const std::string output, const double sigma
    

    );

    1. include "example.hxx"
    1. endif // __example_h

    </syntaxhighlight>

  5. Create the following example.hxx file:

    <syntaxhighlight lang="cpp">

    1. ifndef __example_hxx
    2. define __example_hxx
    1. include "example.h"
    1. include "itkImage.h"
    2. include "itkImageFileReader.h"
    3. include "itkImageFileWriter.h"
    4. include "itkSmoothingRecursiveGaussianImageFilter.h"

    template<class TPixel, unsigned int VDimension> void SmoothImage(

       const std::string input, const std::string output, const double sigma
    

    ) {

       // Read input
       typedef itk::Image<TPixel,VDimension> ImageType;
       typedef itk::ImageFileReader<ImageType> ReaderType;
       ReaderType::Pointer reader = ReaderType::New();
       reader->SetFileName(input);
       reader->Update();
    
       // Apply filter
       typedef itk::SmoothingRecursiveGaussianImageFilter<ImageType> FilterType;
       FilterType::Pointer filter = FilterType::New();
       filter->SetInput(reader->GetOutput());
       filter->SetSigma(sigma);
       filter->Update();
    
       // Write output
       typedef itk::ImageFileWriter<ImageType> WriterType;
       WriterType::Pointer writer = WriterType::New();
       writer->SetInput(filter->GetOutput());
       writer->SetFileName(output);
       writer->Update();
    

    }

    1. endif // __example_hxx

    </syntaxhighlight>

  6. Create the following example.i file (instructs SWIG how to generate the wrapper):

    <syntaxhighlight lang="cpp"> // File : example.i %module example %include "std_string.i"

    %{

    1. include "example.h"

    %}

    %include "example.h"

    %template(SmoothImageUC2)SmoothImage<unsigned char, 2>; %template(SmoothImageF2) SmoothImage<float, 2>; </syntaxhighlight>

  7. Run SWIG using the following command: swig -c++ -csharp -dllimport ExampleWrapper example.i
    The following files should be generated:
    • example_wrap.cxx
    • example.cs
    • examplePINVOKE.cs
  8. Run CMake, configure and generate, build the project. The result should be ExampleWrapper.dll
  9. Create the following C# runme.cs file:

    <syntaxhighlight lang="csharp"> // File: runme.cs using System;

    public class runme {

       [STAThread()]
       static void Main() {
           try {
               example.SmoothImageUC2(
                   input: "C:/Temp/cthead1.png",
                   output: "C:/Temp/cthead1-smooth.png",
                   sigma: 5.0
               );
           } catch (Exception ex) {
               Console.WriteLine(ex);
           }
       }
    

    } </syntaxhighlight>

  10. Build the .NET executable. For Mono use the following: mcs -platform:x86 -target:exe -out:example.exe runme.cs example.cs examplePINVOKE.cs
  11. Run the .NET executable. Make sure it can find ExampleWrapper.dll (the easiest way is to copy the dll to the same folder as example.exe)