[Insight-users] problem for ITKimage to VTKimage in managedITKin C# in Visual Studio 2008

Dan Mueller dan.mueller at philips.com
Mon Apr 14 05:56:26 EDT 2008


Hi Han,

I have further investigated the problem with ManagedITK and vtkDotNet:

1. Warning: in ..\...\Code\common\itkProcessObject.cxx, line 520
This warning actually arises from the Visual Studio debugger. The debugger 
is calling ProcessObject::GetReleaseDataFlag() which throws the warning, 
because ImageToVTKImageFilter does not have an output. This warning can be 
replicated in native ITK using the following code:

#include "itkImage.h"
#include "itkImageToVTKImageFilter.h"

int main(int argc, char* argv[])
{
    // Typedefs
    const unsigned int Dimension = 2;
    typedef float PixelType;
    typedef itk::Image< PixelType, Dimension > ImageType;
    typedef itk::ImageToVTKImageFilter< ImageType > ItkToVtkFilter;
 
    ItkToVtkFilter::Pointer filter = ItkToVtkFilter::New();
    bool releaseData = filter->GetReleaseDataFlag(); // Warning
    filter = NULL;
}

If you do not debug the portion of code around the managed 
ImageToVTKImageFilter (or use release mode) this warning goes away.


2. Const vtkDotNet object
As previously discussed, ManagedITK incorrectly created a const pointer to 
the vtkImageData. I will fix this in the next version of ManagedITK (due 
soon after ITK 3.6 is released).


3. Different versions of ManagedITK.VTK and vtkDotNet
I have confirmed that the same version of VTK must be used when compiling 
the ManagedITK.VTK external project and vtkDotNet (v5.0.1). This can be 
done by downloading and compiling the vtkDotNet source code, and then 
pointing the ManagedITK.VTK external project to this build path. Doing so 
results in correct interoperability. The below code works fine when VTK 
versions are synchronised (and with the above fix):

using System;
using itk;
using vtk;

public void ItkVtkTest()
{
    try
    {
        //  Read ITK image
        itkImage_UC2 input = itkImage_UC2.New();
        input.Read("C:/Temp/cthead1.png");
        input.DisconnectPipeline();

        // Import ITK image to VTK
        itkImageToVTKImageFilter itk2vtk = 
itkImageToVTKImageFilter.New(input);
        itk2vtk.SetInput(input);
        itk2vtk.Update();
        vtkImageData data = itk2vtk.GetOutput();

        // View VTK image
        vtkImageViewer viewer = new vtkImageViewer();
        viewer.SetInput(data);
        vtkRenderWindow renWin = viewer.GetRenderWindow();
        vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
        iren.SetRenderWindow(renWin);
        iren.Initialize();
        viewer.Render();
        iren.Start();

        // Clean up
        iren.Dispose();
        renWin.Dispose();
        viewer.Dispose();
        itk2vtk.Dispose();
        input.Dispose();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}

Check the IJ article in the next week or so for an update:
    http://insight-journal.org/midas/handle.php?handle=1926/501

HTH

Cheers, Dan

"Han Chunlei" <Chunlei.Han at tyks.fi> wrote on 04/07/2008 08:43:09 AM:

> Hei, Dan,
> 
> Thanks very much for your reply.
> 
> Now I think that I have understand the problem of
> itkImageTovtkImageFilter. And I believe that I have to wait the new
> version of it from you. 
> By the way I have learned the vtkImageImport and it works for me. I will
> use it currently until you have the new version of
> itkImageTovtkImageFilter.
> 
> With my best regards
> 
> Chunlei Han
> 
> 
> **************************************************
> 
> Chunlei Han (Hannu), Ph.D, Biophysicist Turku PET Center Turku 
> 
> University Central Hospital (TYKS) 20251, Kiinamyllykatu 4-8 
> 
> Turku,Finland.
> 
> tel. 358-2-3132796(office)/358-50-3419568(mobile)
> 
> fax. 358-2-2318191
> 
> e-mail: chunlei.han at tyks.fi http://www.turkupetcentre.fi/staff/chuhan/
> 
> *************************************************
> 
> -----Original Message-----
> From: Dan Mueller [mailto:dan.mueller at philips.com] 
> Sent: 7. huhtikuuta 2008 9:21
> To: Han Chunlei
> Cc: insight-users at itk.org
> Subject: Re: [Insight-users] problem for ITKimage to VTKimage in
> managedITKin C# in Visual Studio 2008
> 
> Hi Han,
> 
> I have looked into the problem and there seem to two issues:
> 
> 1. The first issue is with the wrapping of GetOutput(..) in 
> itk::ImageToVTKImageFilter for vtkDotNet. This method returns a *const* 
> pointer instead of a non-const one (it passes true to the vtkImageData 
> constructor instead of false). The const pointer can not be cast, 
> resulting in the discussed exception. I will upload a new version of 
> ManagedITK within the next few days which fixes this. In the meantime
> you 
> can fix this in your local copy by editing 
> ExternalProjects\VTK\managed_itkImageToVTKImageFilter.cmake
> 
> 2. With the above fix the ApplicationException "Could not downcast
> pointer 
> to native class" goes away. Calling vtkImageData.ToString() (the C# 
> equivalent of Print) operates as expected, displaying the correct
> imported 
> vtkImageData. However a new problem arises when *using* the resultant 
> vtkImageData as an input. In your case the vtkImageViewer believes it
> has 
> not received an inputs, even after calling SetInput(..). I am still 
> investigating this issue. I believe it may have something to do with the
> 
> fact that I compiled the vtkDotNet wrappers against a different version
> of 
> VTK as the ManagedITK.VTK wrappers. I'll synchronise the versions and
> let 
> you know if it does the trick.
> 
> In the meantime you can use the vtkImageImport filter to import a 
> ManagedITK image to vtkDotNet. See:
> 
> http://public.kitware.com/pipermail/insight-users/2007-August/023278.htm
> l
> 
> HTH
> 
> Cheers, Dan
> 
> insight-users-bounces at itk.org wrote on 04/06/2008 11:40:42 PM:
> > 
> > Hi Han,
> > 
> > Please try invoking Print( std::cout ) (or its equivalent in C#)
> > in i2v.GetOutput() just after you call i2v.Update(), and let us
> > know what you get as output.
> > 
> > 
> >     Thanks
> > 
> > 
> >         Luis
> > 
> > 
> > ------------------
> > Han Chunlei wrote:
> > > Hei, All,
> > > 
> > > 
> > > 
> > > I am a new user of ITK. I installed managedITK3.4.0.1 and tried to
> use 
> 
> > > it C# in visual studio 2008. I tried to transfer an image data from 
> ITK 
> > > to VTK using itkimagetovtkimagefilter. The problem is that I always 
> get 
> > > the wrong message like:
> > > 
> > > 
> > > 
> > > Warning: in ..\...\Code\common\itkProcessObject.cxx, line 520
> > > 
> > > ImagetoVTKimageFilter(04db21e0):Output doesn?t exist!
> > > 
> > > 
> > > 
> > > And debug information as:
> > > 
> > > ApplicationException was unhandled
> > > 
> > > Could not downcast pointer to native class
> > > 
> > > 
> > > 
> > > In the line of
> > > 
> > > View.SetInput(i2v.getOutput());
> > > 
> > > 
> > > 
> > > Any help is greatly appreciated.
> > > 
> > > 
> > > 
> > > Chunlei Han
> > > 
> > > 
> > > 
> > > 
> > 
> ************************************************************************
> ***************************
> > > 
> > > 
> > > 
> > > The codes are as follows:
> > > 
> > > using vtk;
> > > 
> > > using itk;
> > > 
> > > using itk2vtk = itk.itkImageToVTKImageFilter;
> > > 
> > > 
> > > 
> > >                       //  build an image
> > > 
> > >             itk.itkImage_UC2 itkimg = itk.itkImage_UC2.New();
> > > 
> > >             itk.itkSize itksz = new itk.itkSize(128, 128);
> > > 
> > >             itk.itkIndex itkind = new itk.itkIndex(0, 0);
> > > 
> > >             itk.itkImageRegion region = new 
> > > itk.itkImageRegion(itksz,itkind);
> > > 
> > > 
> > > 
> > >             itkimg.SetRegions(region);
> > > 
> > >             itkimg.Allocate();
> > > 
> > >             itkimg.FillBuffer(128);
> > > 
> > > 
> > > 
> > >            // confirm the image
> > > 
> > >             string filename = @"c:\temp\test.jpg";
> > > 
> > >             itkimg.Write(filename);
> > > 
> > >             itk.itkImageInformation info = 
> > > itk.itkImageBase.ReadInformation(filename);
> > > 
> > >             Console.WriteLine("infor= " + info.Size.ToString());
> > > 
> > > 
> > > 
> > >            // transfer itkImage to VTKimage by 
> itkImagetoVTKimageFilter
> > > 
> > >             itk2vtk i2v = itk2vtk.New("IUC2");
> > > 
> > >             i2v.SetInput(itkimg);
> > > 
> > >             i2v.Update();
> > > 
> > >             vtk.vtkImageViewer viewer = new vtkImageViewer();
> > > 
> > >             viewer.SetInput(i2v.GetOutput()); // always problem
> here.



More information about the Insight-users mailing list