diff --git a/Modules/Segmentation/SegmentationUtilities/BooleanOperations/mitkBooleanOperation.cpp b/Modules/Segmentation/SegmentationUtilities/BooleanOperations/mitkBooleanOperation.cpp index 96c3591165..14dd616c44 100644 --- a/Modules/Segmentation/SegmentationUtilities/BooleanOperations/mitkBooleanOperation.cpp +++ b/Modules/Segmentation/SegmentationUtilities/BooleanOperations/mitkBooleanOperation.cpp @@ -1,183 +1,192 @@ /*=================================================================== 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 "mitkBooleanOperation.h" #include #include #include #include #include #include -typedef itk::Image ImageType; +typedef itk::Image< mitk::Label::PixelType, 3> ImageType; static mitk::Image::Pointer Get3DSegmentation(mitk::Image::Pointer segmentation, unsigned int time) { if (segmentation->GetDimension() != 4) return segmentation; mitk::ImageTimeSelector::Pointer imageTimeSelector = mitk::ImageTimeSelector::New(); imageTimeSelector->SetInput(segmentation); imageTimeSelector->SetTimeNr(static_cast(time)); imageTimeSelector->UpdateLargestPossibleRegion(); return imageTimeSelector->GetOutput(); } static ImageType::Pointer CastTo3DItkImage(mitk::Image::Pointer segmentation, unsigned int time) { ImageType::Pointer result; mitk::CastToItkImage(Get3DSegmentation(segmentation, time), result); return result; } mitk::BooleanOperation::BooleanOperation(Type type, mitk::Image::Pointer segmentation0, mitk::Image::Pointer segmentation1, unsigned int time) : m_Type(type), m_Segmentation0(segmentation0), m_Segmentation1(segmentation1), m_Time(time) { this->ValidateSegmentations(); } mitk::BooleanOperation::~BooleanOperation() { } -mitk::Image::Pointer mitk::BooleanOperation::GetResult() const +mitk::LabelSetImage::Pointer mitk::BooleanOperation::GetResult() const { - Image::Pointer result; + mitk::LabelSetImage::Pointer result; switch (m_Type) { case Difference: result = this->GetDifference(); break; case Intersection: result = this->GetIntersection(); break; case Union: result = this->GetUnion(); break; default: mitkThrow() << "Unknown boolean operation type '" << m_Type << "'!"; } return result; } -mitk::Image::Pointer mitk::BooleanOperation::GetDifference() const +mitk::LabelSetImage::Pointer mitk::BooleanOperation::GetDifference() const { ImageType::Pointer input1 = CastTo3DItkImage(m_Segmentation0, m_Time); ImageType::Pointer input2 = CastTo3DItkImage(m_Segmentation1, m_Time); itk::NotImageFilter::Pointer notFilter = itk::NotImageFilter::New(); notFilter->SetInput(input2); itk::AndImageFilter::Pointer andFilter = itk::AndImageFilter::New(); andFilter->SetInput1(input1); andFilter->SetInput2(notFilter->GetOutput()); andFilter->UpdateLargestPossibleRegion(); - Image::Pointer result = Image::New(); - CastToMitkImage(andFilter->GetOutput(), result); + Image::Pointer tempResult = Image::New(); + CastToMitkImage(andFilter->GetOutput(), tempResult); - result->DisconnectPipeline(); + tempResult->DisconnectPipeline(); + + mitk::LabelSetImage::Pointer result = mitk::LabelSetImage::New(); + result->InitializeByLabeledImage(tempResult ); return result; } -mitk::Image::Pointer mitk::BooleanOperation::GetIntersection() const +mitk::LabelSetImage::Pointer mitk::BooleanOperation::GetIntersection() const { ImageType::Pointer input1 = CastTo3DItkImage(m_Segmentation0, m_Time); ImageType::Pointer input2 = CastTo3DItkImage(m_Segmentation1, m_Time); itk::AndImageFilter::Pointer andFilter = itk::AndImageFilter::New(); andFilter->SetInput1(input1); andFilter->SetInput2(input2); andFilter->UpdateLargestPossibleRegion(); - Image::Pointer result = Image::New(); - CastToMitkImage(andFilter->GetOutput(), result); + Image::Pointer tempResult = Image::New(); + CastToMitkImage(andFilter->GetOutput(), tempResult); - result->DisconnectPipeline(); + tempResult->DisconnectPipeline(); + mitk::LabelSetImage::Pointer result = mitk::LabelSetImage::New(); + result->InitializeByLabeledImage(tempResult); return result; } -mitk::Image::Pointer mitk::BooleanOperation::GetUnion() const +mitk::LabelSetImage::Pointer mitk::BooleanOperation::GetUnion() const { ImageType::Pointer input1 = CastTo3DItkImage(m_Segmentation0, m_Time); ImageType::Pointer input2 = CastTo3DItkImage(m_Segmentation1, m_Time); itk::OrImageFilter::Pointer orFilter = itk::OrImageFilter::New(); orFilter->SetInput1(input1); orFilter->SetInput2(input2); orFilter->UpdateLargestPossibleRegion(); - Image::Pointer result = Image::New(); - CastToMitkImage(orFilter->GetOutput(), result); + Image::Pointer tempResult = Image::New(); + CastToMitkImage(orFilter->GetOutput(), tempResult); - result->DisconnectPipeline(); + tempResult->DisconnectPipeline(); + mitk::LabelSetImage::Pointer result = mitk::LabelSetImage::New(); + result->InitializeByLabeledImage(tempResult); return result; } void mitk::BooleanOperation::ValidateSegmentation(mitk::Image::Pointer segmentation) const { if (segmentation.IsNull()) mitkThrow() << "Segmentation is NULL!"; if (segmentation->GetImageDescriptor()->GetNumberOfChannels() != 1) mitkThrow() << "Segmentation has more than one channel!"; mitk::PixelType pixelType = segmentation->GetImageDescriptor()->GetChannelDescriptor().GetPixelType(); - if (pixelType.GetPixelType() != itk::ImageIOBase::SCALAR || pixelType.GetComponentType() != itk::ImageIOBase::UCHAR) - mitkThrow() << "Segmentation is not a scalar image of type 'unsigned char'!"; + if (pixelType.GetPixelType() != itk::ImageIOBase::SCALAR || ( pixelType.GetComponentType() != itk::ImageIOBase::UCHAR && pixelType.GetComponentType() != itk::ImageIOBase::USHORT ) ) + { + mitkThrow() << "Segmentation is not of a supported type. Supported are scalar images of type 'unsigned char' or 'unsigned short'."; + } unsigned int dimension = segmentation->GetDimension(); if (dimension > 4) mitkThrow() << "Segmentation has more than four dimensions!"; if (m_Time != 0) { if(dimension < 4) mitkThrow() << "Expected four-dimensional segmentation!"; if (segmentation->GetDimension(3) < m_Time) mitkThrow() << "Extend of fourth dimension of segmentation is less than specified time!"; } else if (dimension < 3) { mitkThrow() << "Segmentation has less than three dimensions!"; } } void mitk::BooleanOperation::ValidateSegmentations() const { this->ValidateSegmentation(m_Segmentation0); this->ValidateSegmentation(m_Segmentation1); if (m_Segmentation0->GetDimension() != m_Segmentation1->GetDimension()) mitkThrow() << "Segmentations have different dimensions!"; } diff --git a/Modules/Segmentation/SegmentationUtilities/BooleanOperations/mitkBooleanOperation.h b/Modules/Segmentation/SegmentationUtilities/BooleanOperations/mitkBooleanOperation.h index fb378bd833..f4e73569f9 100644 --- a/Modules/Segmentation/SegmentationUtilities/BooleanOperations/mitkBooleanOperation.h +++ b/Modules/Segmentation/SegmentationUtilities/BooleanOperations/mitkBooleanOperation.h @@ -1,77 +1,77 @@ /*=================================================================== 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. ===================================================================*/ #ifndef mitkBooleanOperation_h #define mitkBooleanOperation_h -#include +#include #include namespace mitk { /** \brief Executes a boolean operation on two different segmentations. * * All parameters of the boolean operations must be specified during construction. * The actual operation is executed when calling GetResult(). */ class MITKSEGMENTATION_EXPORT BooleanOperation { public: enum Type { None, Difference, Intersection, Union }; /* \brief Construct a boolean operation. * * Throws an mitk::Exception when segmentations are somehow invalid. * * \param[in] type The type of the boolean operation. * \param[in] segmentation1 The first operand of the boolean operation. * \param[in] segmentation2 The second operand of the boolean operation. * \param[in] The time step at which the operation will be executed. */ BooleanOperation(Type type, Image::Pointer segmentation1, Image::Pointer segmentation2, unsigned int time = 0); ~BooleanOperation(); /* \brief Execute boolean operation and return resulting segmentation. * * \return The resulting segmentation. */ - Image::Pointer GetResult() const; + LabelSetImage::Pointer GetResult() const; private: BooleanOperation(const BooleanOperation &); BooleanOperation & operator=(const BooleanOperation &); - Image::Pointer GetDifference() const; - Image::Pointer GetIntersection() const; - Image::Pointer GetUnion() const; + LabelSetImage::Pointer GetDifference() const; + LabelSetImage::Pointer GetIntersection() const; + LabelSetImage::Pointer GetUnion() const; void ValidateSegmentation(Image::Pointer segmentation) const; void ValidateSegmentations() const; Type m_Type; Image::Pointer m_Segmentation0; Image::Pointer m_Segmentation1; unsigned int m_Time; }; } #endif