diff --git a/Modules/ImageStatistics/Testing/itkMultiGaussianImageSource.h b/Modules/ImageStatistics/Testing/itkMultiGaussianImageSource.h index 98a236789c..b869d50bc8 100644 --- a/Modules/ImageStatistics/Testing/itkMultiGaussianImageSource.h +++ b/Modules/ImageStatistics/Testing/itkMultiGaussianImageSource.h @@ -1,200 +1,219 @@ /*========================================================================= * * Copyright Insight Software Consortium * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * *=========================================================================*/ /*========================================================================= * * Portions of this file are subject to the VTK Toolkit Version 3 copyright. * * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen * * For complete copyright, license and disclaimer of warranty information * please refer to the NOTICE file at the top of the ITK source tree. * *=========================================================================*/ #ifndef __itkMultiGaussianImageSource_h #define __itkMultiGaussianImageSource_h #include "itkImageSource.h" #include "itkNumericTraits.h" #include "itkImageRegionIteratorWithIndex.h" #include "itkImageFileWriter.h" namespace itk { /** \class MultiGaussianImageSource * \brief Generate an 3-dimensional multigaussian image. * This class defines an 3-dimensional Image, in which the value at one voxel equals the value of a multigaussian function evaluated at the voxel's * coordinates. The multigaussian function is build as a sum of N gaussian function. This is defined by the following parameters: * 1. CenterX, CenterY, CenterZ - vectors of the size of N determining the expectancy value at the x-, y- and the z-axis. That means: The i-th * gaussian bell curve takes its maximal value at the voxel with index [CenterX(i); CenterY(i); Centerz(i)]. * 2. SigmaX, SigmaY, SigmaZ - vectors of the size of N determining the deviation at the x-, y- and the z-axis. That means: The width of the i-th * gaussian bell curve in the x-axis is SigmaX(i), in the y-axis is SigmaY(i) and in the z-axis is SigmaZ(i). * 3. Altitude - vector of the size of N determining the altitude: the i-th gaussian bell curve has a height of Altitude(i). * This class allows by the method CalculateMidpointAndMeanValue() to find a sphere with a specified radius that has a maximal mean value over all * sphere with that radius with midpoint inside or at the boundary of the image. * * \ingroup DataSources * \ingroup ITKTestKernel * * \wiki * \wikiexample{} * \endwiki */ template< typename TOutputImage > class ITK_EXPORT MultiGaussianImageSource:public ImageSource< TOutputImage > { public: /** Standard class typedefs. */ typedef MultiGaussianImageSource Self; typedef ImageSource< TOutputImage > Superclass; typedef SmartPointer< Self > Pointer; typedef SmartPointer< const Self > ConstPointer; /** Typedef for the output image PixelType. */ typedef typename TOutputImage::PixelType OutputImagePixelType; /** Typedef to describe the output image region type. */ typedef typename TOutputImage::RegionType OutputImageRegionType; /** Method for creation through the object factory. */ itkNewMacro(Self); /** Basic types from the OutputImageType */ typedef typename TOutputImage::SizeType SizeType; typedef typename TOutputImage::IndexType IndexType; typedef typename TOutputImage::SpacingType SpacingType; typedef typename TOutputImage::PointType PointType; typedef typename SizeType::SizeValueType SizeValueType; typedef SizeValueType SizeValueArrayType[TOutputImage::ImageDimension]; typedef typename TOutputImage::SpacingValueType SpacingValueType; typedef SpacingValueType SpacingValueArrayType[TOutputImage::ImageDimension]; typedef typename TOutputImage::PointValueType PointValueType; typedef PointValueType PointValueArrayType[TOutputImage::ImageDimension]; /** Typedef to describe the sphere radius type. */ typedef double RadiusType; /** Typedef to describe the standard vector type. */ typedef std::vector VectorType; /** Typedef to describe the itk vector type. */ typedef itk::Vector ItkVectorType; /** Typedef to describe the ImageRegionIteratorWithIndex type. */ typedef ImageRegionIteratorWithIndex IteratorType; /** Typedef to describe the Poiner type at the output image. */ typedef typename TOutputImage::Pointer ImageType; /** Set/Get size of the output image */ itkSetMacro(Size, SizeType); virtual void SetSize(SizeValueArrayType sizeArray); virtual const SizeValueType * GetSize() const; /** Set/Get spacing of the output image */ itkSetMacro(Spacing, SpacingType); virtual void SetSpacing(SpacingValueArrayType spacingArray); virtual const SpacingValueType * GetSpacing() const; /** Set/Get origin of the output image */ itkSetMacro(Origin, PointType); virtual void SetOrigin(PointValueArrayType originArray); virtual const PointValueType * GetOrigin() const; /** Get the number of gaussian functions in the output image */ virtual unsigned int GetNumberOfGaussians() const; - + /** Set the number of gaussian function*/ virtual void SetNumberOfGausssians( unsigned int ); /** Set/Get the radius of the sphere */ virtual const RadiusType GetRadius() const; virtual void SetRadius( RadiusType radius ); /** Set/Get the number of steps to traverse the radius of the sphere */ virtual const int GetRadiusStepNumber() const; + /** Set the number of steps to traverse the radius */ virtual void SetRadiusStepNumber( unsigned int stepNumber ); /** Get the maximal mean value in a sphere over all possible spheres with midpoint in the image */ virtual const double GetMaxMeanValue() const; /** Get the index of the midpoint of a sphere with the maximal mean value */ virtual const ItkVectorType GetSphereMidpoint() const; /** Calculates the value of the multigaussian function at a Point given by its coordinates [x;y;z] */ virtual const double MultiGaussianFunctionValueAtPoint(double , double, double); /** Adds a multigaussian defined by the parameter: CenterX, CenterY, CenterZ, SigmaX, SigmaY, SigmaZ, Altitude. All parameters should have the same size, which determinates the number of the gaussian added. */ virtual void AddGaussian( VectorType, VectorType, VectorType, VectorType, VectorType, VectorType, VectorType); - /** Calculates and set the index of the midpoint of the sphere with the maximal mean value as well as the mean value*/ + /** Calculates and set the index of the midpoint of the sphere with the maximal mean value as well as the mean value*/ virtual void CalculateMidpointAndMeanValue(); - + /** Calculates and set the index an the value of maximulm and minimum in the wanted sphere*/ + virtual void CalculateMaxAndMinInSphere(); + /** Get the index in the sphere with maximal value*/ + virtual const ItkVectorType GetMaxValueIndexInSphere() const; + /** Get the maximal value in the sphere*/ + virtual const double GetMaxValueInSphere() const; + /** Get the index in the sphere with minimal value*/ + virtual const ItkVectorType GetMinValueIndexInSphere() const; + /** Get the minimal value in the sphere*/ + virtual const double GetMinValueInSphere() const; + /** Set the region of interest */ + virtual void SetRegionOfInterest(ItkVectorType, ItkVectorType); + /** Optimize the mean value in the wanted sphere*/ + virtual void OptimizeMeanValue(); + /** Check whether the point is in the region of interest */ + virtual const bool IsInRegionOfInterest(unsigned int, unsigned int, unsigned int); /** Set the minimum possible pixel value. By default, it is * NumericTraits::min(). */ itkSetClampMacro( Min, OutputImagePixelType, NumericTraits< OutputImagePixelType >::NonpositiveMin(), NumericTraits< OutputImagePixelType >::max() ); /** Get the minimum possible pixel value. */ itkGetConstMacro(Min, OutputImagePixelType); /** Set the maximum possible pixel value. By default, it is * NumericTraits::max(). */ itkSetClampMacro( Max, OutputImagePixelType, NumericTraits< OutputImagePixelType >::NonpositiveMin(), NumericTraits< OutputImagePixelType >::max() ); /** Get the maximum possible pixel value. */ itkGetConstMacro(Max, OutputImagePixelType); protected: MultiGaussianImageSource(); ~MultiGaussianImageSource(); void PrintSelf(std::ostream & os, Indent indent) const; virtual void GenerateData(); virtual void GenerateOutputInformation(); private: MultiGaussianImageSource(const MultiGaussianImageSource &); //purposely not implemented void operator=(const MultiGaussianImageSource &); //purposely not implemented - SizeType m_Size; //size of the output image - SpacingType m_Spacing; //spacing - PointType m_Origin; //origin - - - unsigned int m_NumberOfGaussians; //number of Gaussians - RadiusType m_Radius; //radius of the sphere - unsigned int m_RadiusStepNumber; //number of steps to traverse the sphere radius - OutputImagePixelType m_MeanValue; //mean value in the wanted sphere - ItkVectorType m_SphereMidpoint; //midpoint of the wanted sphere - VectorType m_SigmaX; //deviation in the x-axis - VectorType m_SigmaY; //deviation in the y-axis - VectorType m_SigmaZ; //deviation in the z-axis - VectorType m_CenterX; //x-coordinate of the mean value of Gaussians - VectorType m_CenterY; //y-coordinate of the mean value of Gaussians - VectorType m_CenterZ; //z-coordinate of the mean value of Gaussians - VectorType m_Altitude; //amplitude - - typename TOutputImage::PixelType m_Min; //minimum possible value - typename TOutputImage::PixelType m_Max; //maximum possible value + SizeType m_Size; //size of the output image + SpacingType m_Spacing; //spacing + PointType m_Origin; //origin + OutputImagePixelType m_MaxValueInSphere; //maximal value in the wanted sphere + ItkVectorType m_MaxValueIndexInSphere; //index of the maximal value in the wanted sphere + OutputImagePixelType m_MinValueInSphere; //minimal value in the wanted sphere + ItkVectorType m_MinValueIndexInSphere; //index of the minimal value in the wanted sphere + unsigned int m_NumberOfGaussians; //number of Gaussians + RadiusType m_Radius; //radius of the sphere + unsigned int m_RadiusStepNumber; //number of steps to traverse the sphere radius + OutputImagePixelType m_MeanValue; //mean value in the wanted sphere + ItkVectorType m_SphereMidpoint; //midpoint of the wanted sphere + VectorType m_SigmaX; //deviation in the x-axis + VectorType m_SigmaY; //deviation in the y-axis + VectorType m_SigmaZ; //deviation in the z-axis + VectorType m_CenterX; //x-coordinate of the mean value of Gaussians + VectorType m_CenterY; //y-coordinate of the mean value of Gaussians + VectorType m_CenterZ; //z-coordinate of the mean value of Gaussians + VectorType m_Altitude; //amplitude + ItkVectorType m_RegionOfInterestMax; // maximal values for the coordinates in the region of interest + ItkVectorType m_RegionOfInterestMin; // minimal values for the coordinates in the region of interest + typename TOutputImage::PixelType m_Min; //minimum possible value + typename TOutputImage::PixelType m_Max; //maximum possible value // The following variables are deprecated, and provided here just for // backward compatibility. It use is discouraged. mutable PointValueArrayType m_OriginArray; mutable SpacingValueArrayType m_SpacingArray; }; } // end namespace itk #ifndef ITK_MANUAL_INSTANTIATION #include "itkMultiGaussianImageSource.hxx" #endif #endif diff --git a/Modules/ImageStatistics/Testing/itkMultiGaussianImageSource.hxx b/Modules/ImageStatistics/Testing/itkMultiGaussianImageSource.hxx index f39a2b7122..9caaafe472 100644 --- a/Modules/ImageStatistics/Testing/itkMultiGaussianImageSource.hxx +++ b/Modules/ImageStatistics/Testing/itkMultiGaussianImageSource.hxx @@ -1,446 +1,585 @@ /*========================================================================= * * Copyright Insight Software Consortium * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * *=========================================================================*/ /*========================================================================= * * Portions of this file are subject to the VTK Toolkit Version 3 copyright. * * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen * * For complete copyright, license and disclaimer of warranty information * please refer to the NOTICE file at the top of the ITK source tree. * *=========================================================================*/ #ifndef __itkMultiGaussianImageSource_hxx #define __itkMultiGaussianImageSource_hxx #include #include #include #include "itkMultiGaussianImageSource.h" #include "itkImageRegionIterator.h" #include "itkObjectFactory.h" #include "itkProgressReporter.h" #include "stdlib.h" namespace itk { /** * */ template< class TOutputImage > MultiGaussianImageSource< TOutputImage > ::MultiGaussianImageSource() { //Initial image is 100 wide in each direction. for ( unsigned int i = 0; i < TOutputImage::GetImageDimension(); i++ ) { m_Size[i] = 100; m_Spacing[i] = 1.0; m_Origin[i] = 0.0; m_SphereMidpoint[i] = 0; } m_NumberOfGaussians = 0; m_Radius = 1; m_RadiusStepNumber = 5; m_MeanValue = 0; m_Min = NumericTraits< OutputImagePixelType >::NonpositiveMin(); m_Max = NumericTraits< OutputImagePixelType >::max(); } template< class TOutputImage > MultiGaussianImageSource< TOutputImage > ::~MultiGaussianImageSource() {} template< class TOutputImage > void MultiGaussianImageSource< TOutputImage > ::SetSize(SizeValueArrayType sizeArray) { const unsigned int count = TOutputImage::ImageDimension; unsigned int i; for ( i = 0; i < count; i++ ) { if ( sizeArray[i] != this->m_Size[i] ) { break; } } if ( i < count ) { this->Modified(); for ( i = 0; i < count; i++ ) { this->m_Size[i] = sizeArray[i]; } } } template< class TOutputImage > const typename MultiGaussianImageSource< TOutputImage >::SizeValueType * MultiGaussianImageSource< TOutputImage > ::GetSize() const { return this->m_Size.GetSize(); } template< class TOutputImage > void MultiGaussianImageSource< TOutputImage > ::SetSpacing(SpacingValueArrayType spacingArray) { const unsigned int count = TOutputImage::ImageDimension; unsigned int i; for ( i = 0; i < count; i++ ) { if ( spacingArray[i] != this->m_Spacing[i] ) { break; } } if ( i < count ) { this->Modified(); for ( i = 0; i < count; i++ ) { this->m_Spacing[i] = spacingArray[i]; } } } template< class TOutputImage > void MultiGaussianImageSource< TOutputImage > ::SetOrigin(PointValueArrayType originArray) { const unsigned int count = TOutputImage::ImageDimension; unsigned int i; for ( i = 0; i < count; i++ ) { if ( originArray[i] != this->m_Origin[i] ) { break; } } if ( i < count ) { this->Modified(); for ( i = 0; i < count; i++ ) { this->m_Origin[i] = originArray[i]; } } } template< class TOutputImage > const typename MultiGaussianImageSource< TOutputImage >::PointValueType * MultiGaussianImageSource< TOutputImage > ::GetOrigin() const { for ( unsigned int i = 0; i < TOutputImage::ImageDimension; i++ ) { this->m_OriginArray[i] = this->m_Origin[i]; } return this->m_OriginArray; } template< class TOutputImage > const typename MultiGaussianImageSource< TOutputImage >::SpacingValueType * MultiGaussianImageSource< TOutputImage > ::GetSpacing() const { for ( unsigned int i = 0; i < TOutputImage::ImageDimension; i++ ) { this->m_SpacingArray[i] = this->m_Spacing[i]; } return this->m_SpacingArray; } /** * */ template< class TOutputImage > void MultiGaussianImageSource< TOutputImage > ::PrintSelf(std::ostream & os, Indent indent) const { Superclass::PrintSelf(os, indent); os << indent << "Max: " << static_cast< typename NumericTraits< OutputImagePixelType >::PrintType >( m_Max ) << std::endl; os << indent << "Min: " << static_cast< typename NumericTraits< OutputImagePixelType >::PrintType >( m_Min ) << std::endl; os << indent << "Origin: ["; unsigned int ii = 0; while( ii < TOutputImage::ImageDimension - 1 ) { os << m_Origin[ii] << ", "; ++ii; } os << m_Origin[ii] << "]" << std::endl; os << indent << "Spacing: ["; ii = 0; while( ii < TOutputImage::ImageDimension - 1 ) { os << m_Spacing[ii] << ", "; ++ii; } os << m_Spacing[ii] << "]" << std::endl; os << indent << "Size: ["; ii = 0; while( ii < TOutputImage::ImageDimension - 1 ) { os << m_Size[ii] << ", "; ++ii; } os << m_Size[ii] << "]" << std::endl; } template< class TOutputImage > unsigned int MultiGaussianImageSource< TOutputImage > ::GetNumberOfGaussians() const { return this->m_NumberOfGaussians; } template< class TOutputImage > const typename MultiGaussianImageSource< TOutputImage >::RadiusType MultiGaussianImageSource< TOutputImage > ::GetRadius() const { return this->m_Radius; } template< class TOutputImage > void MultiGaussianImageSource< TOutputImage > ::SetRadius( RadiusType radius ) { this->m_Radius = radius; } template< class TOutputImage > const int MultiGaussianImageSource< TOutputImage > ::GetRadiusStepNumber() const { return this->m_RadiusStepNumber; } template< class TOutputImage > void MultiGaussianImageSource< TOutputImage > ::SetRadiusStepNumber( unsigned int stepNumber ) { this->m_RadiusStepNumber = stepNumber; } template< class TOutputImage > void MultiGaussianImageSource< TOutputImage > ::SetNumberOfGausssians( unsigned int n ) { this->m_NumberOfGaussians = n; } + +template< class TOutputImage > +void +MultiGaussianImageSource< TOutputImage > +::SetRegionOfInterest( ItkVectorType roiMin, ItkVectorType roiMax ) +{ + m_RegionOfInterestMax.operator=(roiMax); + m_RegionOfInterestMin.operator=(roiMin); +} + + template< class TOutputImage > const double MultiGaussianImageSource< TOutputImage > ::GetMaxMeanValue() const { return m_MeanValue; } +template< class TOutputImage > +const double +MultiGaussianImageSource< TOutputImage > +::GetMaxValueInSphere() const +{ + return m_MaxValueInSphere; +} + +template< class TOutputImage > +const typename MultiGaussianImageSource< TOutputImage >::ItkVectorType +MultiGaussianImageSource< TOutputImage > +::GetMaxValueIndexInSphere() const +{ + return m_MaxValueIndexInSphere; +} + +template< class TOutputImage > +const double +MultiGaussianImageSource< TOutputImage > +::GetMinValueInSphere() const +{ + return m_MinValueInSphere; +} +template< class TOutputImage > +const typename MultiGaussianImageSource< TOutputImage >::ItkVectorType +MultiGaussianImageSource< TOutputImage > +::GetMinValueIndexInSphere() const +{ + return m_MinValueIndexInSphere; +} //---------------------------------------------------------------------------- template< class TOutputImage > const typename MultiGaussianImageSource< TOutputImage >::ItkVectorType MultiGaussianImageSource< TOutputImage > ::GetSphereMidpoint() const { return m_SphereMidpoint; } template< class TOutputImage > const double MultiGaussianImageSource< TOutputImage > ::MultiGaussianFunctionValueAtPoint(double x, double y, double z) { double summand0, summand1, summand2, power, value = 0; // the for-loop represent the sum of the gaussian function for(unsigned int n =0; n < m_NumberOfGaussians; ++n) { summand0 = (x - m_CenterX[n]) / m_SigmaX[n]; summand1 = (y - m_CenterY[n]) / m_SigmaY[n]; summand2 = (z - m_CenterZ[n]) / m_SigmaZ[n]; power = summand0 * summand0 + summand1 * summand1 + summand2 * summand2; value = value + m_Altitude[n] * pow(itk::Math::e, -0.5 * power); } return value; } template< class TOutputImage > void MultiGaussianImageSource< TOutputImage > ::AddGaussian( VectorType x, VectorType y, VectorType z, VectorType sx, VectorType sy, VectorType sz, VectorType altitude) { for(unsigned int i = 0; i < x.size(); ++i) { m_CenterX.push_back(x[i]); m_CenterY.push_back(y[i]); m_CenterZ.push_back(z[i]); m_SigmaX.push_back(sx[i]); m_SigmaY.push_back(sy[i]); m_SigmaZ.push_back(sz[i]); m_Altitude.push_back(altitude[i]); } } +template< class TOutputImage > +const bool +MultiGaussianImageSource< TOutputImage > +::IsInRegionOfInterest(unsigned int ind0, unsigned int ind1, unsigned int ind2 ) +{ + bool isInROI = 0; + if(ind0 >= m_RegionOfInterestMin[0] && ind0 <=m_RegionOfInterestMax[0] && + ind1 >= m_RegionOfInterestMin[1] && ind1 <=m_RegionOfInterestMax[1] && + ind2 >= m_RegionOfInterestMin[2] && ind2 <=m_RegionOfInterestMax[2]) + { + isInROI = 1; + } + return isInROI; +} //---------------------------------------------------------------------------- template< typename TOutputImage > void MultiGaussianImageSource< TOutputImage > ::GenerateOutputInformation() { TOutputImage *output; IndexType index; index.Fill(0); output = this->GetOutput(0); typename TOutputImage::RegionType largestPossibleRegion; largestPossibleRegion.SetSize(this->m_Size); largestPossibleRegion.SetIndex(index); output->SetLargestPossibleRegion(largestPossibleRegion); output->SetSpacing(m_Spacing); output->SetOrigin(m_Origin); } //---------------------------------------------------------------------------- template< typename TOutputImage > void MultiGaussianImageSource< TOutputImage > ::GenerateData() { itkDebugMacro(<< "Generating a image of scalars "); double valueReal; IndexType index; typedef typename TOutputImage::PixelType scalarType; typename TOutputImage::Pointer image = this->GetOutput(0); image = this->GetOutput(0); image->SetBufferedRegion( image->GetRequestedRegion() ); image->Allocate(); IteratorType imageIt(image, image->GetLargestPossibleRegion()); PointType globalCoordinate; - for(imageIt.GoToBegin(); !imageIt.IsAtEnd(); ++imageIt) { valueReal = 0.0; index = imageIt.GetIndex(); image->TransformIndexToPhysicalPoint(imageIt.GetIndex(), globalCoordinate); valueReal = MultiGaussianFunctionValueAtPoint(globalCoordinate[0], globalCoordinate[1] ,globalCoordinate[2]); imageIt.Set(valueReal); } } //---------------------------------------------------------------------------- /* - This class allows by the method CalculateMidpointAndMeanValue() to find a sphere with a specified radius that has a maximal mean value over all sphere with that radius with midpoint inside or at the boundary of the image. The parameter RadiusStepNumber controls the accuracy of that calculation (the higher the value the higher the exactness). + This class allows by the method CalculateMidpointAndMeanValue() to find a sphere with a specified radius that has a maximal mean value over all sphere with that radius with midpoint inside or at the boundary of the image. The parameter RadiusStepNumber controls the accuracy of that calculation (the higher the value the higher the exactness). The algorithm works as follows: - 1. the first three for-loops traverse the image and assume the current point to be the wanted sphere midpoint + 1. the first three for-loops traverse the region of interest and assume the current point to be the wanted sphere midpoint 2. calculate the mean value for that sphere (use sphere coordinates): 2.1. traverse the radius of the sphere with step size Radius divided by RadiusStepNumber (the for-loop with index i) 2.2. define a variable dist, which gives a approximately distance between the points at the sphere surface (here we take such a distance, that on the smallest sphere are located 8 points) 2.3. calculate the angles so that the points are equally spaced on the surface (for-loops with indexes j and k) 2.3. for all radius length add the values at the points on the sphere and divide by the number of points added (the values at each point is calculate with the method MultiGaussianFunctionValueAtPoint()) 3. Compare with the until-now-found-maximum and take the bigger one */ template< typename TOutputImage > void MultiGaussianImageSource< TOutputImage > ::CalculateMidpointAndMeanValue() { itkDebugMacro(<< "Generating a image of scalars "); int numberSummand = 0, angleStepNumberOverTwo; double meanValueTemp, valueAdd, value, x, y, z, temp; double riStep, fijStep, psikStep, ri, fij, psik; double dist = itk::Math::pi * m_Radius / (2 * m_RadiusStepNumber); m_MeanValue = 0; riStep = m_Radius / m_RadiusStepNumber; for(unsigned int index0 = 0; index0 < m_Size[0]; ++index0) { for(unsigned int index1 = 0; index1 < m_Size[1]; ++index1) { for(unsigned int index2 = 0; index2 < m_Size[2]; ++index2) { - numberSummand = 0; - value = 0.0; - ri = riStep; - for(unsigned int i = 0; i < m_RadiusStepNumber; ++i) + if(IsInRegionOfInterest(index0, index1, index2)) { - angleStepNumberOverTwo = static_cast( itk::Math::pi * ri / dist); - fij = 0; - fijStep = itk::Math::pi / angleStepNumberOverTwo; - for(unsigned int j = 0; j <= angleStepNumberOverTwo; ++j) // from 0 to pi + m_MaxValueInSphere = 0.0; + m_MinValueInSphere = 1000.0; + numberSummand = 0; + value = 0.0; + ri = riStep; + for(unsigned int i = 0; i < m_RadiusStepNumber; ++i) { - z = ri * cos(fij); - psikStep = 2.0 * itk::Math::pi / (2.0 * angleStepNumberOverTwo); - psik = -itk::Math::pi + psikStep; - - temp = ri * sin(fij); - for(unsigned int k = 0; k < 2 * angleStepNumberOverTwo; ++k) // from -pi to pi + angleStepNumberOverTwo = static_cast( itk::Math::pi * ri / dist); + fij = 0.0; + fijStep = itk::Math::pi / angleStepNumberOverTwo; + for(unsigned int j = 0; j <= angleStepNumberOverTwo; ++j) // from 0 to pi { - x = temp * cos(psik); - y = temp * sin(psik); - numberSummand++; - valueAdd = MultiGaussianFunctionValueAtPoint(x + index0, y + index1, z + index2); - value = value + valueAdd; - psik = psik + psikStep; + z = ri * cos(fij); + psikStep = 2.0 * itk::Math::pi / (2.0 * angleStepNumberOverTwo); + psik = -itk::Math::pi + psikStep; + + temp = ri * sin(fij); + for(unsigned int k = 0; k < 2 * angleStepNumberOverTwo; ++k) // from -pi to pi + { + x = temp * cos(psik); + y = temp * sin(psik); + numberSummand++; + valueAdd = MultiGaussianFunctionValueAtPoint(x + index0, y + index1, z + index2); + value = value + valueAdd; + psik = psik + psikStep; + + } + fij = fij + fijStep; } - fij = fij + fijStep; + ri = ri + riStep; + } + + meanValueTemp = value / numberSummand; + if(meanValueTemp > m_MeanValue) + { + m_MeanValue = meanValueTemp; + m_SphereMidpoint.SetElement( 0, index0 ); + m_SphereMidpoint.SetElement( 1, index1 ); + m_SphereMidpoint.SetElement( 2, index2 ); } - ri = ri + riStep; } + } + } + } +} + +template< typename TOutputImage > +void +MultiGaussianImageSource< TOutputImage > +::OptimizeMeanValue() +{ + int radiusStepNumberOptimized = m_RadiusStepNumber * 4; + int numberSummand = 0, angleStepNumberOverTwo; + double valueAdd, value, x, y, z, temp; + double riStep, fijStep, psikStep, ri, fij, psik; + double dist = itk::Math::pi * m_Radius / (2 * radiusStepNumberOptimized); + m_MeanValue = 0; + riStep = m_Radius / radiusStepNumberOptimized; + + int index0 = m_SphereMidpoint[0]; + int index1 = m_SphereMidpoint[1]; + int index2 = m_SphereMidpoint[2]; + ri = riStep; + value = MultiGaussianFunctionValueAtPoint(index0, index1, index2); + for(unsigned int i = 0; i < radiusStepNumberOptimized; ++i) + { + angleStepNumberOverTwo = static_cast( itk::Math::pi * ri / dist); + fij = 0.0; + fijStep = itk::Math::pi / angleStepNumberOverTwo; + for(unsigned int j = 0; j <= angleStepNumberOverTwo; ++j) // from 0 to pi + { + z = ri * cos(fij); + psikStep = 2.0 * itk::Math::pi / (2.0 * angleStepNumberOverTwo); + psik = -itk::Math::pi + psikStep; + temp = ri * sin(fij); + for(unsigned int k = 0; k < 2 * angleStepNumberOverTwo; ++k) // from -pi to pi + { + x = temp * cos(psik); + y = temp * sin(psik); + numberSummand++; + valueAdd = MultiGaussianFunctionValueAtPoint(x + index0, y + index1, z + index2); + value = value + valueAdd; + psik = psik + psikStep; + } + fij = fij + fijStep; + } + ri = ri + riStep; + } + m_MeanValue = value / numberSummand; + +} - meanValueTemp = value / numberSummand; - if(meanValueTemp > m_MeanValue) +template< typename TOutputImage > +void +MultiGaussianImageSource< TOutputImage > +::CalculateMaxAndMinInSphere() +{ + double value; + m_MaxValueInSphere = 0.0; + m_MinValueInSphere = 100 * m_MeanValue; + int radInt = static_cast(m_Radius); + for(int index0 = -radInt; index0 <= radInt; ++index0 ){ + for(int index1 = -radInt; index1 <= radInt; ++index1 ){ + for(int index2 = -radInt; index2 <= radInt; ++index2 ){ + if( index0 * index0 + index1 * index1 + index2 * index2 < m_Radius * m_Radius ) { - m_MeanValue = meanValueTemp; - m_SphereMidpoint.SetElement( 0, index0 ); - m_SphereMidpoint.SetElement( 1, index1 ); - m_SphereMidpoint.SetElement( 2, index2 ); + value = MultiGaussianFunctionValueAtPoint( m_SphereMidpoint[0] + index0, m_SphereMidpoint[1] + index1, m_SphereMidpoint[2] + index2); + if(m_MaxValueInSphere < value){ + m_MaxValueInSphere = value; + m_MaxValueIndexInSphere.SetNthComponent( 0, m_SphereMidpoint[0] + index0 ); + m_MaxValueIndexInSphere.SetNthComponent( 1, m_SphereMidpoint[1] + index1 ); + m_MaxValueIndexInSphere.SetNthComponent( 2, m_SphereMidpoint[2] + index2 ); + } + if(m_MinValueInSphere > value){ + m_MinValueInSphere = value; + m_MinValueIndexInSphere.SetNthComponent( 0, m_SphereMidpoint[0] + index0 ); + m_MinValueIndexInSphere.SetNthComponent( 1, m_SphereMidpoint[1] + index1 ); + m_MinValueIndexInSphere.SetNthComponent( 2, m_SphereMidpoint[2] + index2 ); + } } } } } } } // end namespace itk #endif diff --git a/Modules/ImageStatistics/Testing/mitkMultiGaussianTest.cpp b/Modules/ImageStatistics/Testing/mitkMultiGaussianTest.cpp index dd0787010e..274b9f7ab1 100644 --- a/Modules/ImageStatistics/Testing/mitkMultiGaussianTest.cpp +++ b/Modules/ImageStatistics/Testing/mitkMultiGaussianTest.cpp @@ -1,93 +1,285 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkTestingMacros.h" #include "itkMultiGaussianImageSource.h" - +#include #include #include +#include +#include +#include + +// Aufruf z.B. mit: bin\Debug\ImageStatisticsTestDriver.exe mitkMultiGaussianTest C:/temp/image 1 50 50 50 10 5 10 20 200 1 1 1 -int mitkMultiGaussianTest(int, char* []) +int mitkMultiGaussianTest(int argc, char* argv[]) { + if ( argc !=14 ) + { + std::cerr << " 14 arguments expected: [outputFilename, numberOfImages, imageSizeX, imageSizeY, imageSizeZ, numberOfGaussians, minWidthOfGaussian, maxWidthOfGaussian, minAltitudeOfGaussian, maxAltitudeOfGaussian, spacingX, spacingY, spacingZ ] " << std::endl; + return EXIT_FAILURE; + } + // argv = [outputFilename, numberOfImages, imageSizeX, imageSizeY, imageSizeZ, numberOfGaussians, minWidthOfGaussian, maxWidthOfGaussian, minAltitudeOfGaussian, maxAltitudeOfGaussian] // always start with this! - MITK_TEST_BEGIN("mitkMultiGaussianTest") + MITK_TEST_BEGIN("mitkMultiGaussianTest"); + MITK_TEST_CONDITION_REQUIRED(argc == 14, "Test called with 14 parameters"); - typedef double PixelType; + typedef double PixelType; + std::string outputFilename = argv[1]; + int numberOfImages; + double centerX, centerY, centerZ, sigmaX, sigmaY, sigmaZ, altitude; + unsigned int numberOfGaussians, minWidthOfGaussian, maxWidthOfGaussian, minAltitudeOfGaussian, maxAltitudeOfGaussian; const unsigned int Dimension = 3; typedef itk::Image ImageType; typedef itk::MultiGaussianImageSource< ImageType > MultiGaussianImageSource; - itk::MultiGaussianImageSource< ImageType >::VectorType centerXVec, centerYVec, centerZVec, sigmaXVec, sigmaYVec, sigmaZVec, altitudeVec; - MultiGaussianImageSource::Pointer gaussianGenerator = MultiGaussianImageSource::New(); + itk::MultiGaussianImageSource< ImageType >::VectorType centerXVec, centerYVec, centerZVec, sigmaXVec, sigmaYVec, sigmaZVec, altitudeVec; + itk::MultiGaussianImageSource< ImageType >::ItkVectorType regionOfInterestMax, regionOfInterestMin; + itk::MultiGaussianImageSource< ImageType >::ItkVectorType sphereMidpt, maxValueIndexInSphere, minValueIndexInSphere; + MultiGaussianImageSource::Pointer gaussianGenerator ; + itk::DOMNodeXMLWriter::Pointer xmlWriter; + typedef itk::DOMNode::Pointer DOMNodeType; + itk::MultiGaussianImageSource< ImageType >::SpacingValueArrayType spacing; + DOMNodeType domTestCase, domTestImage, domGaussian, domStatistics, domROI; ImageType::SizeValueType size[3]; - size[0] = 50; - size[1] = 50; - size[2] = 50; - srand (time(NULL)); - unsigned int numberOfGaussians = 7; - unsigned int minWidthOfGaussian = (size[0] + size[1] + size[2]) / 27; // A ninth of the mean image size - unsigned int maxWidthOfGaussian = (size[0] + size[1] + size[2]) / 9; // One-third of the mean image size - unsigned int minAltitudeOfGaussian = 5; - unsigned int maxAltitudeOfGaussian = 200; - double centerX, centerY, centerZ, sigmaX, sigmaY, sigmaZ, altitude; + std::string name; + std::stringstream ss; + char * fileNamePointer; + if ( ! (std::istringstream(argv[2]) >> numberOfImages) ) numberOfImages = 0; + if ( ! (std::istringstream(argv[3]) >> size[0]) ) size[0] = 0; + if ( ! (std::istringstream(argv[4]) >> size[1]) ) size[1] = 0; + if ( ! (std::istringstream(argv[5]) >> size[2]) ) size[2] = 0; + if ( ! (std::istringstream(argv[6]) >> numberOfGaussians) ) numberOfGaussians = 0; + if ( ! (std::istringstream(argv[7]) >> minWidthOfGaussian) ) minWidthOfGaussian = (size[0] + size[1] + size[2]) / 27; + if ( ! (std::istringstream(argv[8]) >> maxWidthOfGaussian) ) maxWidthOfGaussian = (size[0] + size[1] + size[2]) / 9; + if ( ! (std::istringstream(argv[9]) >> minAltitudeOfGaussian) ) minAltitudeOfGaussian = 5; + if ( ! (std::istringstream(argv[10]) >> maxAltitudeOfGaussian) ) maxAltitudeOfGaussian = 200; + if ( ! (std::istringstream(argv[11]) >> spacing[0]) ) spacing[0] = 1; + if ( ! (std::istringstream(argv[12]) >> spacing[1]) ) spacing[1] = 1; + if ( ! (std::istringstream(argv[13]) >> spacing[2]) ) spacing[2] = 1; + + regionOfInterestMax.SetElement(0, size[0]); + regionOfInterestMax.SetElement(1, size[1]); + regionOfInterestMax.SetElement(2, size[2]); + regionOfInterestMin.SetElement(0, 0); + regionOfInterestMin.SetElement(1, 0); + regionOfInterestMin.SetElement(2, 0); - gaussianGenerator->SetSize( size ); - gaussianGenerator->SetSpacing( 1 ); - gaussianGenerator->SetRadiusStepNumber(5); - gaussianGenerator->SetRadius(pow(itk::Math::one_over_pi * 0.75 , 1.0 / 3.0) * 10); - gaussianGenerator->SetNumberOfGausssians(numberOfGaussians); - // std::ofstream myfile; - // myfile.open ("C:/temp/tempParameter3.txt"); - // myfile << " CentX \t" << "Y \t" << "Z \t" << "SigX \t" << "Y \t" << "Z \t" << "Altit\n"; + srand (time(NULL)); int numberAddGaussian = numberOfGaussians; - for( unsigned int i = 0; i < numberAddGaussian; ++i) + for(unsigned int k = 1; k <= numberOfImages; ++k) { - centerX = rand() % size[0]; - centerY = rand() % size[1]; - centerZ = rand() % size[2]; - sigmaX = minWidthOfGaussian + rand() % (maxWidthOfGaussian - minWidthOfGaussian); - sigmaY = minWidthOfGaussian + rand() % (maxWidthOfGaussian - minWidthOfGaussian); - sigmaZ = minWidthOfGaussian + rand() % (maxWidthOfGaussian - minWidthOfGaussian); - altitude = minAltitudeOfGaussian + rand() % (maxAltitudeOfGaussian - minAltitudeOfGaussian); - //gaussianGenerator->AddGaussian(centerX, centerY, centerZ, sigmaX, sigmaY, sigmaZ, altitude); - centerXVec.push_back(centerX); - centerYVec.push_back(centerY); - centerZVec.push_back(centerZ); - sigmaXVec.push_back(sigmaX); - sigmaYVec.push_back(sigmaY); - sigmaZVec.push_back(sigmaZ); - altitudeVec.push_back(altitude); - // myfile <SetSize( size ); + gaussianGenerator->SetSpacing( spacing ); + gaussianGenerator->SetRadiusStepNumber(8); + gaussianGenerator->SetRadius(pow(itk::Math::one_over_pi * 0.75 , 1.0 / 3.0) * 10); + gaussianGenerator->SetNumberOfGausssians(numberOfGaussians); + gaussianGenerator->SetRegionOfInterest(regionOfInterestMin, regionOfInterestMax); + // DOM Node Writer + xmlWriter = itk::DOMNodeXMLWriter::New(); + domTestCase = itk::DOMNode::New(); + domTestCase->SetName("testcase"); + domTestImage = itk::DOMNode::New(); + domTestImage->SetName("testimage"); + ss.str(""); + ss << size[0]; + domTestImage->SetAttribute("image-rows", ss.str()); + ss.str(""); + ss << size[1]; + domTestImage->SetAttribute("image-columns", ss.str()); + ss.str(""); + ss << size[2]; + domTestImage->SetAttribute("image-slices", ss.str()); + ss.str(""); + ss << numberOfGaussians; + domTestImage->SetAttribute("numberOfGaussians", ss.str()); + domTestCase->AddChildAtBegin(domTestImage); + + for( unsigned int i = 0; i < numberAddGaussian; ++i) + { + + domGaussian = itk::DOMNode::New() ; + domGaussian->SetName("gaussian"); + domTestImage->AddChildAtEnd(domGaussian); + + centerX = 7 + rand() % (size[0] - 14); + ss.str(""); + ss << centerX; + domGaussian->SetAttribute("centerIndexX", ss.str()); + + centerY = 7 + rand() % (size[1] - 14); + ss.str(""); + ss << centerY; + domGaussian->SetAttribute("centerIndexY", ss.str()); + + centerZ = 7 + rand() % (size[2] - 14); + ss.str(""); + ss << centerZ; + domGaussian->SetAttribute("centerIndexZ", ss.str()); + + sigmaX = minWidthOfGaussian + rand() % (maxWidthOfGaussian - minWidthOfGaussian); + ss.str(""); + ss << sigmaX; + domGaussian->SetAttribute("deviationX", ss.str()); + + sigmaY = minWidthOfGaussian + rand() % (maxWidthOfGaussian - minWidthOfGaussian); + ss.str(""); + ss << sigmaY; + domGaussian->SetAttribute("deviationY", ss.str()); - gaussianGenerator->AddGaussian(centerXVec, centerYVec, centerZVec, sigmaXVec, sigmaYVec, sigmaZVec, altitudeVec); - gaussianGenerator->Update(); - gaussianGenerator->CalculateMidpointAndMeanValue(); - std::cout << "Sphere radius is: " << gaussianGenerator->GetRadius() << std::endl; - std::cout << "Sphere midpoint is: " << gaussianGenerator->GetSphereMidpoint() << std::endl; - std::cout << "Mean value is: " << gaussianGenerator->GetMaxMeanValue() << std::endl; - ImageType::Pointer gaussianImage = gaussianGenerator->GetOutput(); - - //File writer - typedef itk::ImageFileWriter< ImageType > WriterType; - WriterType::Pointer writer = WriterType::New(); - writer->SetFileName( "C:/temp/tempImage33.nrrd" ); - writer->SetInput( gaussianImage ); - writer->Update(); - - MITK_TEST_END() + sigmaZ = minWidthOfGaussian + rand() % (maxWidthOfGaussian - minWidthOfGaussian); + ss.str(""); + ss << sigmaZ; + domGaussian->SetAttribute("deviationZ", ss.str()); + + altitude = minAltitudeOfGaussian + rand() % (maxAltitudeOfGaussian - minAltitudeOfGaussian); + ss.str(""); + ss << altitude; + domGaussian->SetAttribute("altitude", ss.str()); + + centerXVec.push_back(centerX); + centerYVec.push_back(centerY); + centerZVec.push_back(centerZ); + sigmaXVec.push_back(sigmaX); + sigmaYVec.push_back(sigmaY); + sigmaZVec.push_back(sigmaZ); + altitudeVec.push_back(altitude); + + } + + gaussianGenerator->AddGaussian(centerXVec, centerYVec, centerZVec, sigmaXVec, sigmaYVec, sigmaZVec, altitudeVec); + centerXVec.clear(); + centerYVec.clear(); + centerZVec.clear(); + sigmaXVec.clear(); + sigmaYVec.clear(); + sigmaZVec.clear(); + altitudeVec.clear(); + gaussianGenerator->Update(); + gaussianGenerator->CalculateMidpointAndMeanValue(); + + //region of interest + domROI = itk::DOMNode::New(); + domROI->SetName("roi"); + domTestCase->AddChildAtEnd(domROI); + ss.str(""); + ss << regionOfInterestMax[0]; + domROI->SetAttribute("maximumX", ss.str()); + ss.str(""); + ss << regionOfInterestMin[0]; + domROI->SetAttribute("minimumX", ss.str()); + ss.str(""); + ss << regionOfInterestMax[1]; + domROI->SetAttribute("maximumY", ss.str()); + ss.str(""); + ss << regionOfInterestMin[1]; + domROI->SetAttribute("minimumY", ss.str()); + ss.str(""); + ss << regionOfInterestMax[2]; + domROI->SetAttribute("maximumZ", ss.str()); + ss.str(""); + ss << regionOfInterestMin[2]; + domROI->SetAttribute("minimumZ", ss.str()); + + //peak and peak coordinate + domStatistics = itk::DOMNode::New(); + domStatistics->SetName("statistic"); + domTestCase->AddChildAtEnd(domStatistics); + sphereMidpt = gaussianGenerator->GetSphereMidpoint(); + ss.str(""); + ss << sphereMidpt[0]; + domStatistics->SetAttribute("peakIndexX", ss.str()); + ss.str(""); + ss << sphereMidpt[1]; + domStatistics->SetAttribute("peakIndexY", ss.str()); + ss.str(""); + ss << sphereMidpt[2]; + domStatistics->SetAttribute("peakIndexZ", ss.str()); + ss.str(""); + ss << gaussianGenerator->GetMaxMeanValue(); + domStatistics->SetAttribute("peak", ss.str()); + + //optimize the mean value in the sphere + gaussianGenerator->OptimizeMeanValue(); + ss.str(""); + ss << gaussianGenerator->GetMaxMeanValue(); + domStatistics->SetAttribute("peakOptimized", ss.str()); + + + //maximum and maximum coordinate + gaussianGenerator->CalculateMaxAndMinInSphere(); + maxValueIndexInSphere = gaussianGenerator->GetMaxValueIndexInSphere(); + ss.str(""); + ss << maxValueIndexInSphere[0]; + domStatistics->SetAttribute("maximumIndexX", ss.str()); + ss.str(""); + ss << maxValueIndexInSphere[1]; + domStatistics->SetAttribute("maximumIndexY", ss.str()); + ss.str(""); + ss << maxValueIndexInSphere[2]; + domStatistics->SetAttribute("maximumIndexZ", ss.str()); + ss.str(""); + ss << gaussianGenerator->GetMaxValueInSphere(); + domStatistics->SetAttribute("maximum", ss.str()); + + //minimum and minimum coordinate + minValueIndexInSphere = gaussianGenerator->GetMinValueIndexInSphere(); + ss.str(""); + ss << minValueIndexInSphere[0]; + domStatistics->SetAttribute("minimumIndexX", ss.str()); + ss.str(""); + ss << minValueIndexInSphere[1]; + domStatistics->SetAttribute("minimumIndexY", ss.str()); + ss.str(""); + ss << minValueIndexInSphere[2]; + domStatistics->SetAttribute("minimumIndexZ", ss.str()); + ss.str(""); + ss << gaussianGenerator->GetMinValueInSphere(); + domStatistics->SetAttribute("minimum", ss.str()); + + // .xml (Data) + ss.str(""); + if(k < 10){ + ss << outputFilename <<"00"<< k <<".xml"; + }else if(k < 100){ + ss << outputFilename <<"0"<< k <<".xml"; + }else{ ss << outputFilename << k <<".xml";} + name = ss.str(); + fileNamePointer = (char*) name.c_str(); + xmlWriter->SetFileName( fileNamePointer); + xmlWriter->SetInput( domTestCase ); + xmlWriter->Update(); + ImageType::Pointer gaussianImage = gaussianGenerator->GetOutput(); + + //.nrrd (Image) + typedef itk::ImageFileWriter< ImageType > WriterType; + WriterType::Pointer writer = WriterType::New(); + ss.str(""); + if(k < 10){ + ss << outputFilename <<"00"<< k <<".nrrd"; + }else if(k < 100){ + ss << outputFilename <<"0"<< k <<".nrrd"; + }else{ ss << outputFilename << k <<".nrrd";} + name = ss.str(); + fileNamePointer = (char*) name.c_str(); + writer->SetFileName( fileNamePointer); + writer->SetInput( gaussianImage ); + writer->Update(); + + } + MITK_TEST_END() } \ No newline at end of file