[Insight-users] ITK example that sums multiple metaheader images

Dan Mueller dan.muel at gmail.com
Wed Oct 22 10:58:13 EDT 2008


Hi Stéphane,

I think you need to disconnect the reader output from the pipeline. I
would do something as follows:

MeanFilterType::Pointer mean = MeanFilterType::New();

std::cout << std::endl;

for (int i = 0 ; i < nbImages ; i++)
{
    reader->SetFileName( argv[i+1] );
    reader->Update();
    ImageType::Pointer input = reader->GetOutput();
    input->DisconnectPipeline();
    addition->SetInput( i , input );
    std::cout << "Image n°" << i+1 << " [" << argv[i+1] << "] ajoutée" <<
    std::endl << std::endl;
}

mean->Update();

I hope this works...

Cheers, Dan

2008/10/22 Stéphane CALANDE <scalande at gmail.com>:
> Dan,
>
> Thank you very much for the code you sent me. It is really very kind from
> your part.
>
> But there is a thing that I don't understand...
> Just to test, I modified the file and I added the line :
>
>
> std::cout << "static_cast< TOutput >(input[i]) = " << static_cast< TOutput
>>(input[i]) << std::endl;
>
> in the for :
>
>
> AccumulatorType mean = NumericTraits< TOutput >::Zero;
>    for( unsigned int i=0; i< input.size(); i++ )
>      {
>      std::cout << "static_cast< TOutput >(input[i]) = " << static_cast<
> TOutput >(input[i]) << std::endl;
>      mean += static_cast< TOutput >(input[i]);
>      }
>      std::cout << std::endl;
>    return static_cast<TOutput>( mean / input.size() );
>
>
> When I launch my program to calculate the average of multiple images, the
> line I've just added show me always the same pixel value for the 5 different
> images.
>
>
> Example :
>
>
> static_cast< TOutput >(input[i]) = 4294966307
> static_cast< TOutput >(input[i]) = 4294966307
> static_cast< TOutput >(input[i]) = 4294966307
> static_cast< TOutput >(input[i]) = 4294966307
> static_cast< TOutput >(input[i]) = 4294966307
>
> static_cast< TOutput >(input[i]) = 4294966311
> static_cast< TOutput >(input[i]) = 4294966311
> static_cast< TOutput >(input[i]) = 4294966311
> static_cast< TOutput >(input[i]) = 4294966311
> static_cast< TOutput >(input[i]) = 4294966311
>
> etc...
>
>
> Or, my 5 images are different... I think it's not normal. After testing, I
> think that the values are the value of the last images that I gave as input.
> For example, if I give as input multiple 'normal' images and then a
> BLACK-image, all the values are "0".
>
>
> Do you think it's an error in the program I created?
>
> Here's the source code :
>
> (Thank you very much, Stéphane)
>
>
>
>
> #include "itkImage.h"
>  #include "itkImageFileReader.h"
>  #include "itkImageFileWriter.h"
>  #include "itkImageIOBase.h"
>  #include "itkNaryMeanImageFilter.h"
>
>  int main( int argc, char * argv[] )
>
>  {
>
>  if( argc < 4 )
>
>  {
>
>  std::cerr << "Usage: " << std::endl;
>
>  std::cerr << argv[0] << " Image1.mhd Image2.mhd [ImageX.mhd]*
> NomImageOutput.mhd" << std::endl;
>
>  return EXIT_FAILURE;
>
>  }
>
>  typedef   unsigned int PixelType; // être sûr que c'est le bon type !!
>
>  typedef itk::Image< PixelType, 3 > ImageType;
>
>  typedef itk::ImageFileReader< ImageType > ReaderType;
>
>  typedef itk::ImageFileWriter< ImageType > WriterType;
>
>  typedef itk::NaryMeanImageFilter< ImageType,
>                                   ImageType > MeanFilterType;
>
>  int nbImages = argc - 2;
>
>  ReaderType::Pointer reader = ReaderType::New();
>
>  MeanFilterType::Pointer addition = MeanFilterType::New();
>
>  std::cout << std::endl;
>
>  for (int i = 0 ; i < nbImages ; i++)
> {
>     reader->SetFileName( argv[i+1] );
>     reader->Update();
>     addition->SetInput( i , reader->GetOutput() );
>     std::cout << "Image n°" << i+1 << " [" << argv[i+1] << "] ajoutée" <<
> std::endl << std::endl;
> }
>
>  addition-> Update();
>
>  WriterType::Pointer writer = WriterType::New();
>
>  writer->SetFileName( argv[nbImages+1] );
>
>  writer->SetInput(addition->GetOutput());
>
>  std::cout << "Ecriture du fichier..." << std::endl << std::endl;
>
>  writer->Update();
>
>  std::cout << "Fichier '" << argv[nbImages+1] << "' créé" << std::endl;
>
>  return EXIT_SUCCESS;
>
>  }
>
>
>
>
>
>
>
>
>
>


More information about the Insight-users mailing list