[Insight-users] "In place" filtering

Dan Mueller dan.muel at gmail.com
Mon Oct 25 01:44:00 EDT 2010


Hi David,

As Karthik says, a true "in-place" filter overwrites each pixel of the
input buffer with the resultant output pixel value and then passes the
input buffer as its output. All filters capable of this should inherit
from itk::InPlaceImageFilter (if you find one that doesn't, please let
us know). You to turn on/off this functionality eg.
filter->InPlaceOn(), filter->InPlaceOff().

The itk::MeanImageFilter does not inherit from
itk::InPlaceImageFilter, the reason being it is a neighbourhood
operation; neighbourhood operations *cannot* be run in-place (the
output for one pixel may be used as the input for another pixel!).

The best you can do is use the ReleaseDataFlagOn() method Karthik
described first.

HTH

Cheers, Dan

On 25 October 2010 07:18, Karthik Krishnan <karthik.krishnan at kitware.com> wrote:
> On Mon, Oct 25, 2010 at 10:43 AM, Karthik Krishnan
> <karthik.krishnan at kitware.com> wrote:
>>
>> On Mon, Oct 25, 2010 at 3:49 AM, David Doria <daviddoria at gmail.com> wrote:
>>>
>>> I don't believe the following will work. The idea is to pass an image to
>>> a function and smooth it "in place".
>>> ImageType::Pointer image = get image from somewhere...
>>> SmoothImage(image);
>>> where:
>>> void SmoothImage(ImageType::Pointer image)
>>> {
>>>  // Create and setup a mean filter
>>>  typedef itk::MeanImageFilter<
>>>  ImageType, ImageType >  filterType;
>>>
>>>   filterType::Pointer meanFilter = filterType::New();
>>>   meanFilter->SetInput(image);
>>>   meanFilter->Update();
>>>   image = meanFilter->GetOutput();
>>> }
>>> Of course you can do this:
>>> ImageType::Pointer image = get image from somewhere...
>>> ImageType::Pointer output = ImageType::Pointer::New();
>>> SmoothImage(image, output);
>>> void SmoothImage(ImageType::Pointer image, ImageType::Pointer output)
>>> {
>>>  // Create and setup a mean filter
>>>  typedef itk::MeanImageFilter<
>>>  ImageType, ImageType >  filterType;
>>>
>>>   filterType::Pointer meanFilter = filterType::New();
>>>   meanFilter->SetInput(image);
>>>   meanFilter->Update();
>>>   output = meanFilter->GetOutput();
>>> }
>>
>> That's not an inplace processing of the data. What is being done above is
>> to allocate the output buffer, in addition to the input buffer - executing
>> the filter - and then taking a reference the newly created output buffer
>> into the variable "output".
>>
>> You might as well just set ReleaseDataFlagOn() on the meanFilter. That
>> way, the memory footprint remains the same as above, and the code is
>> cleaner. Its input gets released after a downstream filter has consumed its
>> output.
>>
>> A in-place filter overwrites its input buffer. The snippet below should do
>> that
>>
>>   meanFilter->SetInput(image);
>>   meanFilter->GraftOutput(image);
>>   meanFilter->Update();
>>
>
> Ascertain this by printing the image object to and after execution..
> something like:
>
>   meanFilter->SetInput(image);
>   meanFilter->GraftOutput(image);
>
>   meanFilter->GetInput()->Print(std::cout);
>   meanFilter->Update();
>   meanFilter->GetOutput()->Print(std::cout);
>
> ... and the internal PixelContainer object on the input as well as the
> output should (hopefully :) ) have the same address.


More information about the Insight-users mailing list