[Insight-users] importing 2D images to itk

Luis Ibanez luis.ibanez@kitware.com
Mon, 16 Dec 2002 10:30:21 -0500


Hi Lydia,

Most ITK objects are managed using SmartPointers.

These pointers take care of releasing the memory
of the object when they are not longer being
referenced by anybody.

All ITK objects using SmartPointer are created
with the "New()" method and the result *must* be
stored in a SmartPointer. Like:

   ImageType::Pointer image = ImageType::New();

In your code you are assiging the result of New()
to a raw pointer, like:

   ImageType    *    image = ImageType::New();

The raw pointer is not able to increase the internal
reference count of the newly created image. That saddly
results in the image being destroyed just after its
creation by New().

By the time you get to the line with :

     image->SetPixelContainer(import);

the pointer to "image" is no longer valid.

Please use a SmartPointer to hold the pointer
of the image returned by New().

Note that, as opposed to VTK, you don't need
to invoke "Delete()" to release the objects.
SmartPointer will graciously take care of this
for your.



Please let us know if you find further problems.


    Thanks


     Luis


---------------------------------

lydia coin wrote:
> Hi folks
> 
> Can anyone tell me what's wrong with these lines of code? I'm new at itk 
> and I got an error at execution
> 
>  unsigned char *labeled_image= new unsigned char[size[0]*size[1]];
> 
> itk::ImportImageContainer<long unsigned int, unsigned char>::Pointer import;
>    import = itk::ImportImageContainer<long unsigned int, unsigned 
> char>::New();
>    import->Initialize();
>    import->Reserve(size[0]*size[1]);
>    import->SetImportPointer(labeled_image,size[0],true);
>    itk::Image<unsigned char, 2> *img = itk::Image<unsigned char, 2>::New();
>     img->SetPixelContainer(import);
> 
> it's compiles fine but stops at img->SetPixelContainer(import)
> 
> May I add other information about the inage as the pixel size ? or is 
> there another problem
> 
> Many thanks
> 
> 
> ------------------------------------------------------------------------
> Do You Yahoo!? -- Une adresse @yahoo.fr gratuite et en français !
> Testez le nouveau Yahoo! Mail <http://fr.mail.yahoo.com>