1 #ifndef MITKMASKUTIL_CPP
2 #define MITKMASKUTIL_CPP
7 #include <itkExtractImageFilter.h>
8 #include <itkChangeInformationImageFilter.h>
13 template <
class TPixel,
unsigned int VImageDimension>
22 template <
class TPixel,
unsigned int VImageDimension>
31 template <
class TPixel,
unsigned int VImageDimension>
34 if (m_Mask==
nullptr || m_Image==
nullptr)
39 typedef itk::Image< TPixel, VImageDimension >
ImageType;
41 typedef typename ImageType::DirectionType DirectionType;
43 bool maskSanity =
true;
48 MITK_ERROR <<
"Something went wrong when casting the mitk mask image to an itk mask image. Do the mask and the input image have the same dimension?";
52 DirectionType imageDirection = m_Image->GetDirection();
53 DirectionType maskDirection = m_Mask->GetDirection();
54 for(
int i = 0; i < imageDirection.ColumnDimensions; ++i )
56 for(
int j = 0; j < imageDirection.ColumnDimensions; ++j )
58 double differenceDirection = imageDirection[i][j] - maskDirection[i][j];
59 if ( fabs( differenceDirection ) >
mitk::eps )
61 double differenceDirection = imageDirection[i][j] - maskDirection[i][j];
62 if ( fabs( differenceDirection ) > 0.001 )
65 MITK_INFO <<
"Mask needs to have same direction as image! (Image direction: " << imageDirection <<
"; Mask direction: " << maskDirection <<
")";
72 PointType imageSpacing = m_Image->GetSpacing();
73 PointType maskSpacing = m_Mask->GetSpacing();
74 for (
unsigned int i = 0; i < VImageDimension; i++)
76 if ( fabs( maskSpacing[i] - imageSpacing[i] ) >
mitk::eps )
79 MITK_INFO <<
"Spacing of mask and image is not equal. Mask: " << maskSpacing <<
" image: " << imageSpacing;
85 PointType imageOrigin = m_Image->GetOrigin();
86 PointType maskOrigin = m_Mask->GetOrigin();
88 typedef itk::ContinuousIndex<double, VImageDimension> ContinousIndexType;
89 ContinousIndexType maskOriginContinousIndex, imageOriginContinousIndex;
91 m_Image->TransformPhysicalPointToContinuousIndex(maskOrigin, maskOriginContinousIndex);
92 m_Image->TransformPhysicalPointToContinuousIndex(imageOrigin, imageOriginContinousIndex);
94 for (
unsigned int i = 0; i < ImageType::ImageDimension; ++i )
96 double misalignment = maskOriginContinousIndex[i] - floor( maskOriginContinousIndex[i] + 0.5 );
98 if ( fmod(misalignment,imageSpacing[i]) >
mitk::eps )
101 MITK_INFO <<
"Pixels/voxels of mask and image are not sufficiently aligned! (Misalignment: " << fmod(misalignment,imageSpacing[i]) <<
")";
107 if ( m_Mask!=
nullptr &&
108 !m_Image->GetLargestPossibleRegion().IsInside( m_Mask->GetLargestPossibleRegion() ) )
111 MITK_INFO <<
"Mask region needs to be inside of image region! (Image region: "
112 << m_Image->GetLargestPossibleRegion() <<
"; Mask region: " << m_Mask->GetLargestPossibleRegion() <<
")";
117 template <
class TPixel,
unsigned int VImageDimension>
120 if (m_Mask==
nullptr || m_Image==
nullptr)
122 MITK_ERROR <<
"Set an image and a mask first";
125 bool maskSanity = CheckMaskSanity();
129 MITK_ERROR <<
"Mask and image are not compatible";
132 typedef itk::Image< TPixel, VImageDimension >
ImageType;
133 typedef itk::Image< unsigned short, VImageDimension >
MaskType;
134 typedef itk::ExtractImageFilter< ImageType, ImageType > ExtractImageFilterType;
136 typename ImageType::SizeType imageSize = m_Image->GetBufferedRegion().GetSize();
137 typename ImageType::SizeType maskSize = m_Mask->GetBufferedRegion().GetSize();
141 bool maskSmallerImage =
false;
142 for (
unsigned int i = 0; i < ImageType::ImageDimension; ++i )
144 if ( maskSize[i] < imageSize[i] )
146 maskSmallerImage =
true;
150 if ( maskSmallerImage )
155 typename MaskType::SpacingType maskSpacing = m_Mask->GetSpacing();
156 typename ImageType::RegionType extractionRegion;
157 typename ImageType::IndexType extractionRegionIndex;
160 for (
unsigned int i=0; i < maskOrigin.GetPointDimension(); i++)
162 extractionRegionIndex[i] = (maskOrigin[i] - imageOrigin[i]) / maskSpacing[i];
165 extractionRegion.SetIndex(extractionRegionIndex);
166 extractionRegion.SetSize(m_Mask->GetLargestPossibleRegion().GetSize());
168 extractImageFilter->SetInput( m_Image );
169 extractImageFilter->SetExtractionRegion( extractionRegion );
170 extractImageFilter->SetCoordinateTolerance( 0.001 );
171 extractImageFilter->SetDirectionTolerance( 0.001 );
172 extractImageFilter->Update();
173 extractedImg = extractImageFilter->GetOutput();
174 extractedImg->SetOrigin(m_Mask->GetOrigin());
175 extractedImg->SetLargestPossibleRegion(m_Mask->GetLargestPossibleRegion());
176 extractedImg->SetBufferedRegion(m_Mask->GetBufferedRegion());
181 extractedImg = m_Image;
itk::SmartPointer< Self > Pointer
itk::Image< TPixel, VImageDimension >::Pointer ExtractMaskImageRegion()
Crops the image to the LargestPossibleRegion of the mask.
DataCollection - Class to facilitate loading/accessing structured data.
map::core::discrete::Elements< 3 >::InternalImageType ImageType
bool CheckMaskSanity()
Checks whether mask and image are compatible for joint access (as via iterators). Spacing and directi...
void SetImage(ImageType *image)
Set image.
itk::Image< unsigned short, VImageDimension > MaskType
itk::Image< TPixel, VImageDimension > ImageType
void SetMask(MaskType *mask)
Set mask.
MITKCORE_EXPORT const ScalarType eps
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.