[Insight-users] `Image Memory Management

David Holmes holmesd3@yahoo.com
Tue, 10 Dec 2002 21:15:01 -0800 (PST)


Sorry about that--silly mail program.

Luis-

I appreciate your input.  You are correct, I am trying
to "hide"  the itk objects.  In my application, I just
want the data array itself.  Let's see if I can write
out the pseudo code in a way that make sense:


<template T> T*  Threshold(T* in, W,H,D,LowT, HiT) {
//W,H,D are dimension  
//LowT and HiT are thresholds

//Create Image and ImageImport so I dont have to copy
mem
ITK_in  <-  Create Image Pointer
import  <- Create Image Import Container
import->Initialize()
import->SetImportPointer( in,W*H*D,false)
size = {W,H,D}
region <- Create Region
region.SetSize(size)
ITK_in->SetRegions(region);
ITK_in->SetPixelContainer(import);

//Setup and run filter
myFilter <-  Create Binary Filter Object
myFilter->SetInput(ITK_in)
myFilter ->SetLowerThreshold(LowT) and
SetUpperThreshold(HiT)
myFilter->Update()

//Here is what I tried, but it fails -- Although I
generate a seperate ImageImportContainer Pointer`, I
could do this inline with the container within the
object

ITK_out = myFilter->GetOutput();

outimport <- Create another import image containter
outimport->Initialize();
outimport->SetImportPointer(ITK_out->GetPixelContainer()->GetImportPointer(),ITK_out->GetPixelContainer()->Size(),false);
ITK_out->SetPixelContainer(outimport);

return
ITK_out->GetPixelContainer()->GetBufferPointer()

}


As you can see, the input side of things isnt a
problem because I can set that flag that controls the
memory.  On the output side, you can see there is a
problem.  My code attempted to change the flag by
passing the container back to the object through the
SetImportPointer().  If you use the Print() statement
before and after, the only change that you will
observe is that the memory manage flag changes from
true to false.  However, upon exiting the function,
the array is deleted.  Not sure if that is a bug or
how it is suppose to be.  Proir to destructing all of
the objects, the data is there.

Although I would very much appreciate any additional
help that you may be able to give, I see only a few
options:

(1)  Predefine and allocate the output data array. 
Then I can use the SetOutput() method for the
function.  There are three problems that I see.  First
of all, am I certain that all of the filters (or other
tools in ITK) support the SetOutput() method?  Second,
SetOutput() is slated to be removed from the library. 
Third, what if the filter needs to change the data
array size?  Is that re-allocated and I can check it
after the fact, or is there a problem with this?

(2)  Simply copy the memory after the filter runs. 
Although memcpy seems like it would be slow and double
the memory requirements, it does avoid my problem.

Well, there you go, everything you didn't want to
hear.  Like I said, I'd appreciate any thoughts that
you may have.

Thanks

david

--- Luis Ibanez <luis.ibanez@kitware.com> wrote:
> 
> Hi David,
> 
> Well, the fact that the Data is available only
> in the scope of the ImportImageContainer is actually
> an advantage since it will take care of memory
> release.
> 
> If what you want is to use this pixel data in
> another
> image, you may want to instantiate another image and
> use the SetPixelContainer() method like:
> 
>    newimage->SetPixelContainer(  pixelContainer );
> 
> The memory will be available as long as the
> pixelContainter SmartPointer or the newimage
> SmartPointer exist.
> 
> 
> The flag that controls memory release in the
> ImportImageContainer is :
> 
>    bool  m_ContainerManageMemory;
> 
> and it is set internally by the container.
> 
> We could consider adding a public Set() method to
> the
> class API so you could put set the flag to off, then
> let die the container and keep the memory...
> 
> This may probably make sense if you are exporting
> the
> data to another library where you don't whant to
> manage
> ITK objects anymore.
> 
> Is that the case ?
> 
> Please let us know,
> 
> 
>    Thanks
> 
> 
>     Luis
> 
> 
> =====================================
> 
> David Holmes wrote:
> 
> > Luis-
> > 
> > thank you for your quick response.  I was actually
> > looking into the ImportImageContainer when I
> received
> > your note.  I implemented your suggestions, but I
> > think that I still have a problem.  Now the data
> is
> > limited to the scope of the ImportImageContainer
> > Pointer.  Because I am simply grabbing the
> container
> > out of the Image, the flag that controls memory
> > management has already been set to TRUE
> (presumably by
> > the filter object), so the data will still be
> > destroyed outside the scope of the
> > ImportImageContainer Pointer.  As such, when I try
> and
> > access the array (by way of the pointer to the
> data
> > outside the function), it core dumps.  I've tried
> to
> > find a way to reset that flag, but haven't found
> it
> > yet.
> > 
> > does this make sense?
> > 
> > I don't know, but if you can help me, I'd
> appreciate
> > it.
> > 
> > Thanks
> > 
> > david
> > 
> > 
> > 
> 
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com