[Insight-users] How to add a constant to an image?

Luis Ibanez luis.ibanez at kitware.com
Sat Nov 7 11:28:43 EST 2009


Hi Jim,


              Welcome to ITK !


The filter that you need to use is:

   Insight/Code/Review/
        itkAddConstantToImageFilter.h


The code that you put together using ImageIterators is fine,
except for the fact that you are running it in an image that
is own by a filter and that is connected to the middle of a
pipeline.

The problem with modifying the image at that point, is that
your changes will be lost when the pipeline is run.

In practice, the solution is to take the code that you have
for the iterators, and put it inside of an ITK  filter, that then
you connect to the pipeline.

This is in fact, what the itkAddConstantToImageFilter.h is.

Please not that in order to use this filter you have to
reconfigure ITK to turn on the flag ITK_USE_REVIEW.
(you do this by re-running CMake in your binary build of
ITK).


Please give it a try to this filter and let us know if you
find any problem.


     Thanks


           Luis


----------------------------------------------------------------------------------------
On Tue, Nov 3, 2009 at 12:41 PM, Jim WJ Chi <wen.chi at magd.ox.ac.uk> wrote:
> Dear Helpers,
>
> I am a new guy who has been learning ITK for a couple of months but still
> puzzling. I have never received any formal training on C/C++ either, which
> may or may not be the reason that I found it so difficult to handle.
> Therefore I wish so much that there is a helper out there who can give me
> advice.
>
> To start with an example, I want to ask two questions. [please see
> attachment for my codes]
> 1.) [About Iterators] am I using the ImageRegionConstIterator and
> ImageRegionIterator correctly?
> 2.) [About adding a number (e.g. 777) to an image] is it the right way to do
> it using Iterators?
>
> Your answer will be valuable to me. Many thanks in advance!
>
> Best regards,
> Jim
>
>  typedef   float           InternalPixelType;
>  const     unsigned int    Dimension = 2;
>  typedef itk::Image< InternalPixelType, Dimension >  InternalImageType;
>
>  typedef unsigned char                            OutputPixelType;
>  typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
>
>  typedef  itk::ImageFileReader< InternalImageType > ReaderType;
>  typedef  itk::ImageFileWriter<  OutputImageType  > WriterType;
>  typedef  itk::RescaleIntensityImageFilter<
>                               InternalImageType,
>                               OutputImageType >   CastFilterType;
>
>  ReaderType::Pointer reader = ReaderType::New();
>  WriterType::Pointer writer = WriterType::New();
>  CastFilterType::Pointer caster = CastFilterType::New();
>
>  reader->SetFileName( "Input.bmp" );
>  reader->Update();
>
>  typedef itk::ImageRegionConstIterator< InternalImageType >
> ConstIteratorType;
>  typedef itk::ImageRegionIterator< InternalImageType > IteratorType;
>  InternalImageType::Pointer outputImage = InternalImageType::New();
>
>  outputImage->SetRegions( reader->GetOutput()->GetLargestPossibleRegion() );
>  outputImage->Allocate();
>
>  ConstIteratorType inputIt( reader->GetOutput(),
> reader->GetOutput()->GetLargestPossibleRegion() );
>  IteratorType outputIt( outputImage,
> reader->GetOutput()->GetLargestPossibleRegion() );
>
>  for ( inputIt.GoToBegin(), outputIt.GoToBegin(); !inputIt.IsAtEnd();
> ++inputIt, ++outputIt)
>  {
>          outputIt.Set( inputIt.Get() + 777); //Add 777 to every pixel.
>  }
>
>  caster->SetInput( outputImage );
>  writer->SetInput( caster->GetOutput() );
>  writer->SetFileName("Output.png");
>  caster->SetOutputMinimum(   0 );
>  caster->SetOutputMaximum( 255 );
>  writer->Update();
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
>


More information about the Insight-users mailing list