[Insight-users] Multiply an Image by Constant

Benjamin King king.benjamin at mh-hannover.de
Tue, 17 Feb 2004 16:47:02 +0100


Hi Sergio,

you can add a constant to an image by using itk::AddImageAdaptor. It's not 
a real filter but performs the addition when it's accessed. ITK defines 
such Adaptors for cases where a real filter (with a seperate output image 
in memory) would be overkill. Take a look at the documentation for 
itk::ImageAdaptor for an overview 
(http://www.itk.org/Doxygen/html/group__ImageAdaptors.html). You will find 
that there is no Adaptor to multiply with a scalar value, though. I think 
that it has been forgotten but you can roll your own.

To do that, first take a look at the file 
Code/Common/itkAddPixelAccessor.h. Besides substituting "Add" by 
"Multiply" everywhere, you also have to change the '+' to '*' in the Get() 
method.

And that's it! In you code, you can now create a suitable adaptor as 
follows:

   typedef itk::ImageAdaptor< ImageType, Accessor::MultiplyImageAccessor< 
ImageType::PixelType > > MultiplyAdaptorType;

   MultiplyAdaptorType::Pointer multiplyAdaptor = 
MultiplyAdaptorType::New();
   multiplyAdaptor->GetPixelAccessor().SetValue(42);

The class itk::AddImageAdaptor simply hides the Accessor from the user. 
You can see how that's done in Code/Common/itkAddImageAdaptor.h


cheers,
   Benjamin

On Tue, 17 Feb 2004 15:34:11 +0100, Sergio Andres <sergio at unizar.es> wrote:

> Hi all,
>
> I think is a little stupid question, but
> could anyone tell me how can multiply a given image by a constant??
>
> I am using filters like AddImageFilter, or MultiplyImageFilter.. for 
> doing things like:
> Image3 = Image1 + Image2
> Image4 = Image1 * Image2
>
> But I did not found a filter or function to do:
> Image2 = Image1 * K
>
> I thought about iterate over pixels and multiply each pixel by K.
> But I don't know if there is a better solution....
>
> Thanks for your possible answers..
>
> Sergio.
>
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users



-- 
Benjamin King
Institut für Medizinische Informatik
Medizinische Hochschule Hannover
Tel.: +49  511  532-2663