[Insight-users] Re: Problem using using smart pointer in itk Image class

Luis Ibanez luis.ibanez at kitware.com
Mon Nov 15 15:08:14 EST 2004


Hi Vijay,

You must *always* assign the result of a New() operator
to a SmartPointer.  Like in:

   typedef itk::Image<unsigned char, 2>  ImageType;

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



When creating an ITK image inside a function you have the
following two options from preventing the image from
being destroyed at the time the function returns:



Option A:

    Create the SmartPointer of the image type outside
    the function and pass it as a pointer. Then inside
    the function assign the image to that passed Smart
    Pointer.

    If you are doing this in a C-like interface, that will
    look like



   // before calling the function
   typedef itk::Image<unsigned char, 2>  ImageType;
   ImageType::Pointer img;
   void * pimg = &img;
   myFunctionForCreatingAnImage( pimg, k1, k2, k3,... kn );




  // inside the function
   void myFunctionForCreatingAnImage( void * pimg, k1, k2 ... )
   {
   typedef itk::Image<unsigned char, 2>  ImageType;

    // your code for the importer filter..

   ImageType::Pointer *kimg = (ImageType::Pointer *)pimg;
   *kimg = importer->GetOutput();
   return;
   }






Option B:

    You can artificially increase the reference count
    of the image before returning from the function,
    and return the raw pointer to the image in the form
    of a (void *).



    // Inside the function
    void * myFunction( k1, k2,...)
    {
     typedef itk::Image<unsigned char, 2>  ImageType;
     // do the import stuff...

     importer->Update(); // IMPORTANT !!!
     ImageType::Pointer image = importer->GetOutput();

     image->Register();  // Increment the reference count

     return image.GetPointer();
    }


    // Outside the function
    ImageType::Pointer image = (ImageType *)myFunction(...);
    image->UnRegister(); // Decrement the reference count

    // start happily using the image....




Option "A" is by far a better approach...



Please let us know if you have further questions,


    Thanks


      Luis



-----------------------
Vijay Prashanth wrote:

> Hi Luis,
>  
> I am  importing an 512*512 2D array of unsigned char into an
> itk::Image<unsigned char, 2> as below
> { 
> itk::ImportImageContainer<long unsigned int, unsigned char>::Pointer import;
>  import = itk::ImportImageContainer<long unsigned int, unsigned char>::New();
>  import->Initialize();
>  import->Reserve(512*512);
>  import->SetImportPointer(pointer_to_my_array,512,true);
>  
> typedef itk::Image<unsigned char, 2> ImageType;
> ImageType:: Pointer img = ImageType::New();
>  
> ImageType::IndexType start;
> start[0] = 0; // first index on X
> start[1] = 0; // first index on Y
> start[2] = 0; // first index on Z
>  
> ImageType::SizeType size;
> size[0] = 200; // size along X
> size[1] = 200; // size along Y
> size[2] = 200; // size along Z
>  
> ImageType::RegionType region;
> region.SetSize( size );
> region.SetIndex( start );
>  
> image->SetRegions( region );
> image->Allocate();
>  
> img->SetPixelContainer(import);
>  
> return img;
> }
>  
> The above code snippet is inside a function. when I returned the "img"
> pointer variable as void*, since it is a smart pointer the destructor is
> called automatically and the img is deleted.
>  
> If I do not use smart pointer and create a raw pointer as
> itk::Image<unsigned char, 2>*  img = itk::Image<unsigned char, 2>::New();
> then I cannot access the functions it is giving me a run time error.
> It is giving error in the accessing the functions inside Reserve() for
> importimagecontainer and inside the SetRegions() for image class.
>  
> So I am in need of a way of returning the img.
> 
>  pls could you help as ASAP.
> Thank you in advance
>  
> Vijay
> 
> 
> ------------------------------------------------------------------------
> 
> -----------------------------------------------------------------------------------------------------------------------------
> Disclaimer
> -----------------------------------------------------------------------------------------------------------------------------
> 
> "This message(including attachment if any)is confidential and may be privileged.Before opening attachments please check them
> for viruses and defects.MindTree Consulting Private Limited (MindTree)will not be responsible for any viruses or defects or
> any forwarded attachments emanating either from within MindTree or outside.If you have received this message by mistake please notify the sender by return  e-mail and delete this message from your system. Any unauthorized use or dissemination of this message in whole or in part is strictly prohibited.  Please note that e-mails are susceptible to change and MindTree shall not be liable for any improper, untimely or incomplete transmission."
> 
> -----------------------------------------------------------------------------------------------------------------------------






More information about the Insight-users mailing list