Hi.<br><br>I just send this for anyone that could be interrested in the itkNaryMeanImageFilter created for my problem.<br><br>There was a little problem of types. <br>With the code of "itkNaryMeanImageFilter" that was given in the former mail, if you wanted to calculate the mean of a lot of images, there was an overflow of the variable "AccumulatorType mean = NumericTraits< TOutput >::Zero;" in the "for" :<br>
<br><span style="font-family: courier new,monospace;">for( unsigned int i=0; i< input.size(); i++ )</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> { </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> mean += static_cast< TOutput >(input[i])</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> } </span><br>
<br>Even I divied "static_cast< TOutput >(input[i])" by the number of images in the "for", I had till problems (some parts of the image were black)<br><br>Thats why I replace that part of the code by that one :<br>
<br><br><span style="font-family: courier new,monospace;">//AccumulatorType mean = NumericTraits< TOutput >::Zero;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">long double total = 0;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> for( unsigned int i=0; i< input.size(); i++ )</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> { </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> //mean += (static_cast< TOutput >(input[i])) / input.size() ;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> total += static_cast< TOutput >(input[i]) ;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> } </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> //return static_cast<TOutput>( mean );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> total = total / input.size();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return static_cast<TOutput>( total );</span><br>
<br><br><br>I give you here the final code of itkNaryMeanImageFilter that works + my program that take multiple mhd images and that give as output the mean of that images (Sorry the "cout" are in French ;-) ) <br>
<br>I hope it will be useful for anyone interrested...<br><br><br><br>Stéphane.<br><br><br><span style="font-family: courier new,monospace;">/*=========================================================================</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> Program: Insight Segmentation & Registration Toolkit</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> Module: $RCSfile: itkNaryMeanImageFilter.h,v $</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> Language: C++</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> Date: $Date$</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> Version: $Revision$</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> Copyright (c) Insight Software Consortium. All rights reserved.</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> See ITKCopyright.txt or <a href="http://www.itk.org/HTML/Copyright.htm">http://www.itk.org/HTML/Copyright.htm</a> for details.</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> This software is distributed WITHOUT ANY WARRANTY; without even</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> PURPOSE. See the above copyright notices for more information.</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">=========================================================================*/</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#ifndef __itkNaryMeanImageFilter_h</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#define __itkNaryMeanImageFilter_h</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#include "itkNaryFunctorImageFilter.h"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#include "itkNumericTraits.h"</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">namespace itk</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">{</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">/** \class NaryMeanImageFilter</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> * \brief Implements an operator for pixel-wise mean of N images.</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> *</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> * This class is parametrized over the types of the two</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> * input images and the type of the output image.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> * Numeric conversions (castings) are done by the C++ defaults.</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> *</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> * The pixel type of the input 1 image must have a valid defintion of</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> * the operator+ with a pixel type of the image 2. This condition is</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> * required because internally this filter will perform the operation</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> *</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> * sum of input pixels / number of inputs</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> *</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> * Additionally the type resulting from the mean, will be cast to</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> * the pixel type of the output image.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> *</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> * The total operation over one pixel will be</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> *</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> * output_pixel = static_cast<OutputPixelType>( sum of input pixels /</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">num inputs )</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> *</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> * \warning No numeric overflow checking is performed in this filter.</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> *</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> * \ingroup IntensityImageFilters Multithreaded</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> */</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">namespace Functor {</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">template< class TInput, class TOutput ></span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">class Mean</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">{</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">public:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> typedef typename NumericTraits< TInput >::AccumulateType AccumulatorType;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> Mean() {}</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ~Mean() {}</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> inline TOutput operator()( const std::vector< TInput > & input )</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> { </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> long double total = 0;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> for( unsigned int i=0; i< input.size(); i++ )</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> { </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> total += static_cast< TOutput >(input[i]) ;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> } </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> total = total / input.size();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return static_cast<TOutput>( total );</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> bool operator== (const Mean&) const</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return true;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> bool operator!= (const Mean&) const</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return false;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">};</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">}</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">template <class TInputImage, class TOutputImage></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">class ITK_EXPORT NaryMeanImageFilter :</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> public</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">NaryFunctorImageFilter<TInputImage,TOutputImage,</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> Functor::Mean<typename TInputImage::PixelType,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">typename TInputImage::PixelType > ></span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">{</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">public:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> /** Standard class typedefs. */</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> typedef NaryMeanImageFilter Self;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> typedef NaryFunctorImageFilter<TInputImage,TOutputImage,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> Functor::Mean<typename TInputImage::PixelType,</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> typename TInputImage::PixelType > ></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">Superclass;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> typedef SmartPointer<Self> Pointer;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> typedef SmartPointer<const Self> ConstPointer;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> /** Method for creation through the object factory. */</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> itkNewMacro(Self);</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> /** Runtime information support. */</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> itkTypeMacro(NaryMeanImageFilter,</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> NaryFunctorImageFilter);</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#ifdef ITK_USE_CONCEPT_CHECKING</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> /** Begin concept checking */</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> itkConceptMacro(InputConvertibleToOutputCheck,</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (Concept::Convertible<typename TInputImage::PixelType,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> typename TOutputImage::PixelType>));</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> itkConceptMacro(InputHasZeroCheck,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (Concept::HasZero<typename TInputImage::PixelType>));</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> /** End concept checking */</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#endif</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">protected:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> NaryMeanImageFilter() {}</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> virtual ~NaryMeanImageFilter() {}</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">private:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> NaryMeanImageFilter(const Self&); //purposely not implemented</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> void operator=(const Self&); //purposely not implemented</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">};</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">} // end namespace itk</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#endif</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">**********************************************************************************************************************************************<br>
**********************************************************************************************************************************************<br>**********************************************************************************************************************************************<br>
<br><br><br><span style="font-family: courier new,monospace;"> #include "itkImage.h"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #include "itkImageFileReader.h"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> #include "itkImageFileWriter.h" </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #include "itkImageIOBase.h"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> #include "itkNaryMeanImageFilter.h"</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> int main( int argc, char * argv[] )</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if( argc < 4 )</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> {</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> std::cerr << "Usage: " << std::endl;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> std::cerr << argv[0] << " Image1.mhd Image2.mhd [ImageX.mhd]* NomImageOutput.mhd" << std::endl;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return EXIT_FAILURE;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> typedef signed short PixelType; // être sûr que c'est le bon type !! </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> typedef itk::Image< PixelType, 3 > ImageType;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> typedef itk::ImageFileReader< ImageType > ReaderType;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> typedef itk::ImageFileWriter< ImageType > WriterType;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> typedef itk::NaryMeanImageFilter< ImageType,</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ImageType > MeanFilterType;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> int nbImages = argc - 2;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ReaderType::Pointer reader = ReaderType::New();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> MeanFilterType::Pointer addition = MeanFilterType::New();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> std::cout << std::endl;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> for (int i = 0 ; i < nbImages ; i++)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">{ </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> reader->SetFileName( argv[i+1] );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> reader->Update();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ImageType::Pointer input = reader->GetOutput();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> input->DisconnectPipeline();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> addition->SetInput( i , input ); </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> std::cout << "Image n°" << i+1 << " [" << argv[i+1] << "] ajoutée" << std::endl << std::endl;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">}</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> addition-> Update();</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> WriterType::Pointer writer = WriterType::New();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> writer->SetFileName( argv[nbImages+1] );</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> writer->SetInput(addition->GetOutput());</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> std::cout << "Ecriture du fichier..." << std::endl << std::endl;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> writer->Update();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> std::cout << "Fichier '" << argv[nbImages+1] << "' créé" << std::endl;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return EXIT_SUCCESS;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><br>