[Insight-developers] (no subject)

kent williams norman-k-williams at uiowa.edu
Fri Oct 29 10:13:09 EDT 2010


First of all:

OutputImageType::Pointer tmp_pic = OutputimageType::New();

All you need is 

OutputImageType::Pointer tmp_pic;

The image you allocated with OutputImageType::New() is destroyed without
being used for anything when you do this:
tmp_pic->SetRegions(input->GetLargestPossibleRegion());
tmp_pic->Allocate();
tmp_pic->GetOutput();

Second: 
> vec contains 2 times the same pics, which seems logical since a ImagetoImage
> Filter has only one pic as output, so the same memory is also used when i ==
> 1;
I don't think this is the case; each time through the loop you're allocating
a new instance of the MaskFilter, so the 2 elements in the vector are the
output images allocated in different instances of the filter.

I think you need to read up on SmartPointers; there is a fundamental
difference between:

X *x = new X;
and
X::Pointer x = X::New();

In the first case x is a C++ pointer to memory allocated by new; if x goes
out of scope without a call to

delete x;

then the allocated memory is leaked.

SmartPointers are a C++ template class; when you construct a smartPointer,
it initially points to no object and x.IsNull() is true.  When you assign a
pointer to X to the SmartPointer, it increments a reference count in the
instance.  When a SmartPointer goes out of scope, it's destructor is called;
this decrements the object's reference count, and if the reference count
goes to zero, the memory is freed.

Every time through your loop, you allocate a new filter. When the new filter
object is assigned to mask_f, the SmartPointer will delete the filter
instance to which it previously pointed.


On 10/29/10 4:44 AM, "Martin Waitzbauer" <mazzok at gmx.at> wrote:

> Hello,
> 
> Im looking for a way to save images in a std::vector for later use
> 
> so what i did was this
> 
> double sigmas[] = {64,128};
> 
>   for(int i =0; i <2;i++){
> 
>   MaskFilterType::Pointer mask_f = MaskFilterType::New();
>   mask_f->SetInput(input);
>  mask_f->setHeight(input->GetLargestPossibleRegion().GetSize()[1]);
> mask_f->setWidth(input->GetLargestPossibleRegion().GetSize()[0]);
>   OutputImageType::Pointer tmp_pic = OutputImageType::New();
>   mask_f->SetVariance(sigmas[i]);
>   tmp_pic->SetRegions(input->GetLargestPossibleRegion());
>   tmp_pic->Allocate();
>   tmp_pic= mask_f->GetOutput();
>   vec.push_back(tmp_pic);
>   
>   }
> 
> vec contains 2 times the same pics, which seems logical since a ImagetoImage
> Filter has only one pic as output, so the same memory is also used when i ==
> 1;
> 
> I tried to set the tmp_pic.Delete(), but this one will give me an error
> How could i create a structure like the above, a filter that creates a new
> Picture(in the sense of new memory allocation), so that vec will contain 2
> different images, depedning on the Filter option
> mask_f->SetVariance(sigmas[i]);
> 
> Thanks
> 
> M



More information about the Insight-developers mailing list