[Insight-users] Simple-ITK installation Tutorial for C#

Bradley Lowekamp blowekamp at mail.nih.gov
Mon Jun 18 18:09:35 EDT 2012


You need to extract a component from the vector image, not just a simple cast.

The name of the filter you need is VectorIndexSelectionCast.

http://www.itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1VectorIndexSelectionCastImageFilter.html



On Jun 18, 2012, at 5:08 PM, Federico Lopez Bertoni wrote:

> Dan,
> 
> Thanks for the reply. 
> 
> Your advice did help me out. I'm able to run SimpleITK with C#. I still have a couple of 
> questions for you since I'm stuck in another place now. 
> 
> 1. When I try to run the example below I get the following error:
> 
> sitk::ERROR: Pixel type: vector of 8-bit unsigned integer is not supported in 2D byclass 
> itk::simple::BinaryThresholdImageFilter
> 
> I understand that the file I'm reading (a regular PNG) is a 8-bit unsigned integer 
> vector and that I need to cast it to some type that the filter supports.
> 
> I try this:
> var testImage = SimpleITK.Cast(input, PixelIDValueEnum.sitkUInt8.swigValue);
> 
> and get the following error:
> 
> Exception thrown in SimpleITK Cast: C:\Users\Bradley 
> Lowekamp\Documents\Source\SimpleITK\Code\BasicFilters\src\sitkCastImageFilter.cxx:75:
> sitk::ERROR: Filter does not support casting from casting vector of 8-bit unsigned 
> integer to 8-bit unsigned integer
> 
> Sorry if the question is a little basic, but I can't get it to run/cast my images. 
> 
> 2. Is it ok for me to post your response in the stackoverflow site? 
> 
> Thanks again for the help,
> 
> Federico
> 
> 
> 
> On Sat, 16 Jun 2012 11:36:58 +0930, Dan Mueller wrote
>> Hi Federico,
>> 
>> The error you are receiving gives the hint:
>>    "An attempt was made to load a program with an incorrect format."
>> 
>> This error occurs when a .NET program tries to access functions in a
>> dll which was built using a different architecture; see for example
>> here:
>>    http://blogs.msdn.com/b/arvindsh/archive/2009/06/21/tip-of-the-day-an-
>> attempt-was-made-to-load-a-program-with-an-incorrect-format-net-p-invoke-issue.aspx
>> 
>> The easiest way to resolve this is to use the same building settings
>> as SimpleITK. For example, if you downloaded
>> "SimpleITK-0.4.0-CSharp-Win64-anycpu.zip" then your calling program
>> should be built with x64 AnyCPU settings. If you downloaded
>> "SimpleITK-0.4.0-CSharp-Win32-x86.zip" you should use x86 Win32
>> settings.
>> 
>> To answer your other question: you only need to add
>> "SimpleITKCSharpManaged.dll" to your program references, but
>> "SimpleITKCSharpNative.dll" must be in the probe path (the easiest way
>> of achieving this is to put it in your binary directory next to your
>> program executable and the managed dll). This is explained at the
>> following page (this page is for Linux, but the same approach is
>> needed for Windows also):
>> 
> http://www.itk.org/Wiki/ITK/Release_4/SimpleITK/GettingStarted/Visual_guide_to_building_
> on_Linux#A_simple_C.23_program
>> 
>> Please let us know how you get on.
>> 
>> HTH
>> 
>> Cheers, Dan
>> 
>> On 15 June 2012 23:33, Federico J. Lopez Bertoni <fjlb at pol.una.py> wrote:
>>> Hi,
>>> 
>>> I'm transcribing my post on stackoverflow where I couldn't get any
>>> help. http://stackoverflow.com/questions/10826487/simple-itk-installation-tutorial-
> for-c-sharp
>>> 
>>> I've used ITK with c++ some time ago and I'm trying to use simple itk in c#
>>> now. I've looked at the MICCAI 2011 tutorial and I'm trying to get started.
>>> I'm using a Windows 7 64 bit machine and Visual Studio Ultimate 2010.
>>> According to this wiki I should be able to just unzip and reference the
>>> .dlls to my project and it should be working. I'm able to add the
>>> SimpleITKCSharpManaged.dll to my references. However I'm not able to add the
>>> SimpleITKCSharpNative.dll to my references. It gives me the error: Please
>>> make sure the file is accesible, and that it is a valid assembly or COM
>>> component.
>>> 
>>> I then try to run a simple console app
>>> 
>>> using System;
>>> 
>>> using System.Collections.Generic;
>>> 
>>> using System.IO;
>>> 
>>> using System.Linq;
>>> 
>>> using System.Text;
>>> 
>>> using itk.simple;
>>> 
>>> 
>>> namespace TestSimpleItk
>>> 
>>> {
>>> 
>>>     class Program
>>> 
>>>     {
>>> 
>>>         static void Main(string[] args)
>>> 
>>>         {
>>> 
>>>             string inputFilename = "001.png";
>>> 
>>>             if (File.Exists(inputFilename))
>>> 
>>>             {
>>> 
>>>                 try
>>> 
>>>                 {
>>> 
>>>                     var input = SimpleITK.ReadImage(inputFilename);
>>> 
>>>                     var binary = SimpleITK.BinaryThreshold(input, 100, 255,
>>> 255, 0);
>>> 
>>>                     SimpleITK.WriteImage(binary, "cthead1-binary.png");
>>> 
>>>                 }
>>> 
>>>                 catch (Exception ex)
>>> 
>>>                 {
>>> 
>>>                     Console.WriteLine("{0}", ex);
>>> 
>>>                 }
>>> 
>>> 
>>>             }
>>> 
>>>             else
>>> 
>>>             {
>>> 
>>>                 Console.WriteLine("File '{0}' does not exist",
>>> inputFilename);
>>> 
>>>             }
>>> 
>>>         }
>>> 
>>>     }
>>> 
>>> }
>>> 
>>> 
>>> The exception thrown is: {"The type initializer for
>>> 'itk.simple.SimpleITKPINVOKE' threw an exception."} and the inner exception
>>> is: {"An attempt was made to load a program with an incorrect format.
>>> (Exception from HRESULT: 0x8007000B)"}
>>> 
>>> I've tried to copy the SimpleITKCSharpNative.dll to my bin/debug directory
>>> and I'm still getting the same error.
>>> 
>>> Any ideas on how to solve this, or how to get SimpleItk up and running
>>> without actually building it locally from the source code using CMAKE?
>>> 
>>> Thanks
>>> 
>>> 
>>> Federico
> 
> 
> --
> Open WebMail Project (http://openwebmail.org)
> _____________________________________
> 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://www.itk.org/mailman/listinfo/insight-users



More information about the Insight-users mailing list