[Insight-users] Managed ITK problem

Dan Mueller dan.muel at gmail.com
Thu Sep 24 08:09:25 EDT 2009


Hi Ali,

Okay, let me summarize what I understand of your situation:

a. You are using 3D CT images of the head (skull, jaw)
b. Your task is to perform deformation of the jaw (using FEM)
c. You want to achieve the task in C# (you still did not make it 100%
clear to me why, but I will assume you have no option here; the only
reason I keep saying this is that you will save yourself a huge amount
of effort if you use ITK from native C++).

I propose the following:

1. Create a native C++ class deriving from itkImageToImageFilter
called MyCustomFemJawDeformationFilter (or whatever name you want)

2. For now this filter should do nothing (perhaps simply pass the
input to the output). There are numerous examples available for how to
do this. Perhaps, if you get stuck with this step, you can create a
new thread on the Users mailing list to ask for specific advice
regarding this step.

3. Create a ManagedITK external project to wrap this skeleton filter.
For instructions see:
http://manageditk.googlecode.com/files/ManagedITK-article.pdf chapter
5, page 31. Also look at the Examples/ExternalProjects folder provided
with the ManagedITK source code. Your CMakeLists.txt will look
something like this:

PROJECT(WrapMyCustomFemJawDeformationFilter)

# Find required packages
FIND_PACKAGE(ITK REQUIRED)
FIND_PACKAGE(ManagedITK REQUIRED)

# Use required packages
INCLUDE(${ITK_USE_FILE})
INCLUDE(${MANAGED_ITK_USE_FILE})

# Wrap the project
BEGIN_MANAGED_WRAP_EXTERNAL_PROJECT("MyCustomFemJawDeformation")
  SET(MANAGED_WRAPPER_OUTPUT "${CMAKE_BINARY_DIR}")
END_MANAGED_WRAP_EXTERNAL_PROJECT()

Your wrap file (managed_MyCustomFemJawDeformationFilter.cmake) will
look something like this:

# Begin the wrapping
WRAP_CLASS("itk::MyCustomFemJawDeformationFilter")

  # Wrap the class for SCALAR types
  FOREACH(t ${WRAP_ITK_SCALAR})
    WRAP_TEMPLATE("${ITKM_${t}}" "${ITKT_${t}}")
  ENDFOREACH(t)

# End the wrapping
END_WRAP_CLASS()

4. Configure the project using CMake, generate the solution, compile.
The result should be a .NET assembly containing your filter.

5. Create a test .NET console application (or similar) to try out your
dummy filter. It might look something like this:

using System;
using itk;

using FilterType = itk.MyCustomFemJawDeformationFilter;

namespace MyNamespace
{
static class MyCustomFilterTest
{
    [STAThread]
    static void Main(String[] args)
    {
        try
        {
            // Setup input and output images
            itkImage_F3 input = itkImage_F3.New();
            itkImage_F3 output = itkImage_F3.New();

            // Read the input image
            input.Read(args[0]);

            // Apply the filter
            FilterType filter = FilterType.New(input, output);
            filter.SetInput(input);
            filter.Update();
            filter.GetOutput(output);

            // Write the output to disk
            output.Write(args[1]);

            // Clean up
            filter.Dispose();
            input.Dispose();
            output.Dispose();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    } // end main
} // end class
} // end namespace

6. Throw your hands in the air and shout "yippee" as loud as you can
(you have reached a milestone).

7. Next, consider what parameters you require your filter to have. eg.
perhaps a string specifying the filename of the FEM config file.
Perhaps you might also need some element/material properties eg. a
double for the moment of inertia or density.

8. Add the required parameters to the native C++ filter. For example:

itkGetStringMacro(FemConfigFilename);
itkSetStringMacro(FemConfigFilename);
itkSetMacro(Density, double);
itkGetConstMacro(Density, double);

9. Add the new parameters to your wrapping file. For example:

  BEGIN_MANAGED_PROPERTY("FemConfigFilename" GETSET)
    SET(MANAGED_PROPERTY_SUMMARY   "Get/set if the filename of the FEM
config file.")
    SET(MANAGED_PROPERTY_TYPE      "String^")
    SET(MANAGED_PROPERTY_GET_BODY  "return gcnew String(
m_PointerToNative->GetFemConfigFilename() );")
    SET(MANAGED_PROPERTY_SET_BODY  "std::string stdvalue;
ManagedTypes::ToNativeString( value, stdvalue );
m_PointerToNative->SetFemConfigFilename( stdvalue.c_str() );")
  END_MANAGED_PROPERTY()

  BEGIN_MANAGED_PROPERTY("Density" GETSET)
    SET(MANAGED_PROPERTY_SUMMARY   "Get/set if the density of the material.")
    SET(MANAGED_PROPERTY_TYPE      "double")
    SET(MANAGED_PROPERTY_GET_BODY  "return m_PointerToNative->GetDensity();")
    SET(MANAGED_PROPERTY_SET_BODY  "m_PointerToNative->SetDensity(value);")
  END_MANAGED_PROPERTY()

10. Configure the project using CMake, generate the solution, compile.

11. Update your .NET console application to try out the new
functionality. It might look something like this:

filter.FemConfigFilename = "C:/Temp/params.txt";
filter.Density = 0.9;

12. Add to your native C++ filter the required code in the
GenerateData method. Having a look at the ITK
Examples/Registration/DeformationRegistration1.cxx example, I can
imagine that you will need to do the following:
a. Have some typedefs for the FEM types
b. Register the correct fem::ImageMetricLoad
c. Create an instance of itk::FEMRegistrationFilter
d. Set and load the config filename (in your custom filter that will
be stored in m_FemConfigFilename)
e. Set the fixed and moving images to the internal registration filter
(in your custom filter that will be the 1st and 2nd inputs)
f. Set the FEM element and material values
g. Run the registration filter
h. Output the displacements (not exactly sure how you will want to do
this, perhaps writing to a file will work for you...)

13. Configure the project using CMake, generate the solution, compile.

14. Test the filter from the .NET console application.

15. Integrate the filter into your proper .NET application.

Hope this helps.

Regards, Dan

2009/9/24 InfoSeekerr <ali.mahmoud.habib at gmail.com>:
>
> Dear Dan,
>
> I develop a aproject using c# , I read a ct 2D slices and reconstruct to
> build 3D skull , after that I want to apply Finite elemet analysis to the 3d
> SKULL  , to the jaw , to remove the deformation in the jaw
>
> I am familiar to .NET and I did the first and second steps which are read
> and reconstruct the skull
>
> any advice please
>
> Dan Mueller-2 wrote:
>>
>> Hi Ali,
>>
>> I do not work with VTK+ITK+C# so I can not comment on performance...
>>
>> Native ITK can *NOT* be used directly from C#. ManagedITK allows the
>> use of some ITK classes easily from C#, however as already discussed
>> it does not allow the use of FEM functionality.
>>
>> You may find the following references useful for achieving ITK+C#
>> interoperability:
>>
>> http://public.kitware.com/pipermail/insight-users/2006-October/019652.html
>>   http://www.cmake.org/pipermail/insight-users/2007-September/023670.html
>>
>> http://www.itk.org/Wiki/ITK_FAQ#Deal_with_Runtime_Exceptions_in_a_Managed_C.2B.2B_Windows_Forms_Application
>>
>> Hope this helps.
>>
>> Regards, Dan
>>
>> 2009/9/9 Ali Habib <ali.mahmoud.habib at gmail.com>:
>>>
>>> thanks alot,
>>>
>>> it worked , but it give .lib files do u have any idea how to convert them
>>> to
>>> .dll to work with them in C# project,
>>>
>>> do u work with vtk , and itk in c# project , if yes is its performance
>>> good
>>>
>>>
>>> thanks again
>>> best regards
>>> On Mon, Sep 7, 2009 at 5:57 PM, Dan Mueller <dan.muel at gmail.com> wrote:
>>>>
>>>> Hi Ali,
>>>>
>>>> Did you click the "Configure" button?
>>>>
>>>> 2009/9/7 Ali Habib <ali.mahmoud.habib at gmail.com>:
>>>> > Hi ,
>>>> > thnks for ur help , those are the red properties:
>>>> > Build_DOXYGEN
>>>> > BUILD_EXAMPLE
>>>> > BUILD_SHARED_LIBS
>>>> > BUILD_TESTING
>>>> > CMAKE_BACKWARDS_COMPATABILITY: 2.4
>>>> > CMAKE_INSTALL_PREFIX
>>>> > ITK_USE_KWSTYLE
>>>> >
>>>> > I will read the tutorials
>>>> >
>>>> > best regards
>>>> >
>>>> > On Mon, Sep 7, 2009 at 2:23 PM, Dan Mueller <dan.muel at gmail.com>
>>>> wrote:
>>>> >>
>>>> >> Hi Ali,
>>>> >>
>>>> >> I see you are trying to build ITK. In the email I just sent I thought
>>>> >> you were trying to build ManagedITK. Please ignore my previous email.
>>>> >>
>>>> >> The following pages have some useful tutorials/guides for building
>>>> ITK:
>>>> >>    http://www.itk.org/ITK/help/help.html
>>>> >>    http://www.itk.org/CourseWare/Training/GettingStarted-I.pdf
>>>> >>
>>>> >> Regarding the red configuration item: please specify which
>>>> >> configuration item is red. Note that you may need to click
>>>> "Configure"
>>>> >> multiple times before you are able to click "Generate" (depending on
>>>> >> what settings you choose).
>>>> >>
>>>> >> Hope this helps.
>>>> >>
>>>> >> Regards, Dan
>>>> >>
>>>> >> 2009/9/7 Dan Mueller <dan.muel at gmail.com>:
>>>> >> > Hi Ali,
>>>> >> >
>>>> >> > Which configuration item was red? Was it ITK_DIR?
>>>> >> >
>>>> >> > If so, please specify the location of ITK (the output foloder)
>>>> which
>>>> >> > you have previous built.
>>>> >> >
>>>> >> > Hope this helps.
>>>> >> >
>>>> >> > Regards, Dan
>>>> >> >
>>>> >> > 2009/9/7 Ali Habib <ali.mahmoud.habib at gmail.com>:
>>>> >> >> Hi Dan,
>>>> >> >> I downloaded the source of ITK and used cmake to build it for
>>>> visual
>>>> >> >> studio.net
>>>> >> >>
>>>> >> >> a red configuration item appeared , and the generator button still
>>>> >> >> hide
>>>> >> >> how
>>>> >> >> to modify the red properties to make the generate button appear
>>>> >> >>
>>>> >> >> the properties are:
>>>> >> >>
>>>> >> >> Build_DOXYGEN
>>>> >> >> BUILD_EXAMPLE
>>>> >> >> BUILD_SHARED_LIBS
>>>> >> >> BUILD_TESTING
>>>> >> >> CMAKE_BACKWARDS_COMPATABILITY: 2.4
>>>> >> >> CMAKE_INSTALL_PREFIX
>>>> >> >> ITK_USE_KWSTYLE
>>>> >> >>
>>>> >> >> your fast response will be highly appretiated
>>>> >> >>
>>>> >> >>
>>>> >> >> On Tue, Sep 1, 2009 at 4:19 PM, Dan Mueller <dan.muel at gmail.com>
>>>> >> >> wrote:
>>>> >> >>>
>>>> >> >>> Hi Ali,
>>>> >> >>>
>>>> >> >>> Unfortunately ManagedITK does not cover the FEM classes.
>>>> >> >>>
>>>> >> >>> Probably the best approach is to create your own custom
>>>> >> >>> filter/process
>>>> >> >>> object which performs the desired functionality (ie. takes one or
>>>> >> >>> two
>>>> >> >>> input images, takes a number of parameters, applies the desired
>>>> FEM
>>>> >> >>> functionality, creates an output image) and wrap that custom
>>>> >> >>> object.
>>>> >> >>>
>>>> >> >>> For more details of wrapping custom filters/objects, see the last
>>>> >> >>> section of the FAQ:
>>>> >> >>>     http://manageditk.googlecode.com/files/ManagedITK-article.pdf
>>>> >> >>>
>>>> >> >>> Hope this helps.
>>>> >> >>>
>>>> >> >>> Regards, Dan
>>>> >> >>>
>>>> >> >>> 2009/9/1 InfoSeekerr <ali.mahmoud.habib at gmail.com>:
>>>> >> >>> >
>>>> >> >>> > Does Managed ITK version cover FEM class, I tried to search for
>>>> >> >>> > that
>>>> >> >>> > but
>>>> >> >>> > couldn't see this class
>>>> >> >>> >
>>>> >> >>> > does anyone face the same problem and how to overcome that


More information about the Insight-users mailing list