[Insight-users] Creating an image pointer and setting it to output of a filter

Luis Ibanez luis . ibanez at kitware . com
Mon, 08 Dec 2003 21:44:37 -0500


Hi Radhika,

In ITK the output of filters is owned by the filter.
That is, a filter allocates the memory for its output and holds it
until the filter is destroyed.

When you assing GetOutput() to another smart pointer, you are
creating a second pointer to the same data, but the filter is still
holding a pointer to the output image. The ownership is not transferred
in any way.  As a consequence, if you do something like

    for(int i=0; i<N; i++)
       {
       filter->Update();
       image[i] = filter->GetOutput();
       }

all the image[i] smartpointers will be assigned to the *same* image
which is the single output of "filter".  They will have all show you
the same content of the last iteration of the for() loop.

Allocating the SmartPointer outside of the for/while loop will not
make any difference.

You could force the allocation of a new image by using a NULL filter
like the CastImageFilter instantiated over the same input and output
image types:

    typedef itk::CastImageFilter<
                          ImageType,
                          ImageType >     CopyFilterType;

    std::vector< ImageType::Pointer > images;

    for(unsigned int i=0; i<N; i++)
      {
      CopyFilterType::Pointer copier = CopyFilterType::New();
      copier->SetInput( filter->GetOutput() );
      copier->Update();
      images->push_back( copier->GetOutput() );
      }


Note that the copier filter is created and destroyed at every
iteration of the for() loop. By pushing the smart pointer of
its output into an std::vector we prevent the copier filter
from destroying the output image.


For iterators the situation is different since you visit the
image right away. (Unless you were planning to visit the images
once the loop is finished).

You can combine the code above with another for loop for
visiting the image pixels using iterators.


Please let us know if you have further questions,



Thanks,


     Luis



----------------------------
Radhika Sivaramakrishna wrote:

> Hi Luis,
> 
>     In my program I have a while loop where I create an intermediate 
> image which is the output of a sequence of filters and I perform some
> 
> calculations on this image. Let me call this image1. Let me assume that 
> I want to set this as the output of a filter for which FinalFilter 
> within the while
> 
> loop.
> 
>  
> 
> Would I need to set a New pointer to image1 outside the while loop like:
> 
>  
> 
> ImageType::Pointer image1 = ImageType::New();
> 
>  
> 
> And then within the while loop I do:
> 
> while(..)
> 
> {
> 
> ...
> 
> ...
> 
> ...
> 
> image1 = FinalFilter->GetOutput();
> 
> ...
> 
> ...
> 
> }
> 
>  
> 
> Is the assignment statement in the while loop correct, or would I need 
> to set the result of FinalFilter to image1 in some other way?
> 
> I also have a similar question for iterators. If my iterator is imit, 
> how would I create an iterator outside the while loop and then set it
> 
> to an image and corresponding region inside while loop?
> 
>  
> 
> Hope my questions are clear.
> 
>  
> 
> Thanks
> 
> Radhika
> 
> -----------------------------------------------------
> 
> Confidentiality Notice.
> 
> This email message is for the sole use of the intended recipient(s) and 
> may contain confidential and privileged information. Any unauthorized 
> review, use, disclosure or distribution is prohibited. If you are not 
> the intended recipient, please contact the sender by reply email and 
> destroy all copies of the original message. If you are the intended 
> recipient, please be advised that the content of this message is subject 
> to access, review and disclosure by the sender's Email System Administrator.
>