From 9c51b38bca98b1cfa58808f71368113bd14fa9b7 Mon Sep 17 00:00:00 2001 From: Danial Saruji Date: Mon, 10 Oct 2011 16:50:59 +0200 Subject: [PATCH] fixed misallignment check for rotated geometries --- .../mitkImageStatisticsCalculator.cpp | 30 ++++++++++++++++--- 1 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Modules/ImageStatistics/mitkImageStatisticsCalculator.cpp b/Modules/ImageStatistics/mitkImageStatisticsCalculator.cpp index 24941ef..df0b4da 100644 --- a/Modules/ImageStatistics/mitkImageStatisticsCalculator.cpp +++ b/Modules/ImageStatistics/mitkImageStatisticsCalculator.cpp @@ -56,8 +56,6 @@ PURPOSE. See the above copyright notices for more information. #include - - namespace mitk { @@ -764,20 +762,42 @@ void ImageStatisticsCalculator::InternalCalculateStatisticsMasked( itkExceptionMacro( << "Mask needs to have same spacing as image! (Image spacing: " << imageSpacing << "; Mask spacing: " << maskSpacing << ")" ); } + // Make sure that orientation of mask and image are the same + typedef typename ImageType::DirectionType DirectionType; + DirectionType imageDirection = image->GetDirection(); + DirectionType maskDirection = maskImage->GetDirection(); + for( int i = 0; i < imageDirection.ColumnDimensions; ++i ) + { + for( int j = 0; j < imageDirection.ColumnDimensions; ++j ) + { + double differenceDirection = imageDirection[i][j] - maskDirection[i][j]; + if ( fabs( differenceDirection ) > mitk::eps ) + { + itkExceptionMacro( << "Mask needs to have same direction as image! (Image direction: " << imageDirection << "; Mask direction: " << maskDirection << ")" ); + } + } + } // Make sure that the voxels of mask and image are correctly "aligned", i.e., voxel boundaries are the same in both images PointType imageOrigin = image->GetOrigin(); PointType maskOrigin = maskImage->GetOrigin(); long offset[ImageType::ImageDimension]; + + typedef itk::ContinuousIndex ContinousIndexType; + ContinousIndexType maskOriginContinousIndex, imageOriginContinousIndex; + + image->TransformPhysicalPointToContinuousIndex(maskOrigin, maskOriginContinousIndex); + image->TransformPhysicalPointToContinuousIndex(imageOrigin, imageOriginContinousIndex); + for ( unsigned int i = 0; i < ImageType::ImageDimension; ++i ) { - double indexCoordDistance = (maskOrigin[i] - imageOrigin[i]) / imageSpacing[i]; - double misalignment = indexCoordDistance - floor( indexCoordDistance + 0.5 ); - if ( fabs( misalignment ) > imageSpacing[i] / 20.0 ) + double misalignment = maskOriginContinousIndex[i] - floor( maskOriginContinousIndex[i] + 0.5 ); + if ( fabs( misalignment ) > mitk::eps ) { itkExceptionMacro( << "Pixels/voxels of mask and image are not sufficiently aligned! (Misalignment: " << misalignment << ")" ); } + double indexCoordDistance = maskOriginContinousIndex[i] - imageOriginContinousIndex[i]; offset[i] = (int) indexCoordDistance + image->GetBufferedRegion().GetIndex()[i]; } -- 1.7.0.4