diff --git a/Modules/Segmentation/SegmentationUtilities/MorphologicalOperations/mitkMorphologicalOperations.cpp b/Modules/Segmentation/SegmentationUtilities/MorphologicalOperations/mitkMorphologicalOperations.cpp index 31512f2240..002544b05c 100644 --- a/Modules/Segmentation/SegmentationUtilities/MorphologicalOperations/mitkMorphologicalOperations.cpp +++ b/Modules/Segmentation/SegmentationUtilities/MorphologicalOperations/mitkMorphologicalOperations.cpp @@ -1,380 +1,359 @@ /*=================================================================== 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 "mitkMorphologicalOperations.h" #include #include #include #include #include #include #include #include #include #include #include void mitk::MorphologicalOperations::Closing(mitk::Image::Pointer& image, int factor, mitk::MorphologicalOperations::StructuralElementType structuralElement) { - MITK_INFO << "Start Closing..."; + MITK_INFO << "Start Closing..."; - int timeSteps = static_cast(image->GetTimeSteps()); + int timeSteps = static_cast(image->GetTimeSteps()); - if (timeSteps > 1) - { - mitk::ImageTimeSelector::Pointer timeSelector = mitk::ImageTimeSelector::New(); - timeSelector->SetInput(image); + if (timeSteps > 1) + { + mitk::ImageTimeSelector::Pointer timeSelector = mitk::ImageTimeSelector::New(); + timeSelector->SetInput(image); - for (int t = 0; t < timeSteps; ++t) - { - MITK_INFO << " Processing time step " << t; + for (int t = 0; t < timeSteps; ++t) + { + MITK_INFO << " Processing time step " << t; - timeSelector->SetTimeNr(t); - timeSelector->Update(); + timeSelector->SetTimeNr(t); + timeSelector->Update(); - mitk::Image::Pointer img3D = timeSelector->GetOutput(); - img3D->DisconnectPipeline(); + mitk::Image::Pointer img3D = timeSelector->GetOutput(); + img3D->DisconnectPipeline(); - AccessFixedPixelTypeByItk_3(img3D, itkClosing, (unsigned char), img3D, factor, structuralElement); + AccessFixedPixelTypeByItk_3(img3D, itkClosing, (unsigned char), img3D, factor, structuralElement); - mitk::ImageReadAccessor accessor(img3D); - image->SetVolume(accessor.GetData(), t); - } - } - else - { - AccessFixedPixelTypeByItk_3(image, itkClosing, (unsigned char), image, factor, structuralElement); - } + mitk::ImageReadAccessor accessor(img3D); + image->SetVolume(accessor.GetData(), t); + } + } + else + { + AccessFixedPixelTypeByItk_3(image, itkClosing, (unsigned char), image, factor, structuralElement); + } - MITK_INFO << "Finished Closing"; + MITK_INFO << "Finished Closing"; } void mitk::MorphologicalOperations::Erode(mitk::Image::Pointer& image, int factor, mitk::MorphologicalOperations::StructuralElementType structuralElement) { - MITK_INFO << "Start Erode..."; + MITK_INFO << "Start Erode..."; - int timeSteps = static_cast(image->GetTimeSteps()); + int timeSteps = static_cast(image->GetTimeSteps()); - if (timeSteps > 1) - { - mitk::ImageTimeSelector::Pointer timeSelector = mitk::ImageTimeSelector::New(); - timeSelector->SetInput(image); + if (timeSteps > 1) + { + mitk::ImageTimeSelector::Pointer timeSelector = mitk::ImageTimeSelector::New(); + timeSelector->SetInput(image); - for (int t = 0; t < timeSteps; ++t) - { - MITK_INFO << " Processing time step " << t; + for (int t = 0; t < timeSteps; ++t) + { + MITK_INFO << " Processing time step " << t; - timeSelector->SetTimeNr(t); - timeSelector->Update(); + timeSelector->SetTimeNr(t); + timeSelector->Update(); - mitk::Image::Pointer img3D = timeSelector->GetOutput(); - img3D->DisconnectPipeline(); + mitk::Image::Pointer img3D = timeSelector->GetOutput(); + img3D->DisconnectPipeline(); - AccessFixedPixelTypeByItk_3(img3D, itkErode, (unsigned char), img3D, factor, structuralElement); + AccessFixedPixelTypeByItk_3(img3D, itkErode, (unsigned char), img3D, factor, structuralElement); - mitk::ImageReadAccessor accessor(img3D); - image->SetVolume(accessor.GetData(), t); - } - } - else - { - AccessFixedPixelTypeByItk_3(image, itkErode, (unsigned char), image, factor, structuralElement); - } + mitk::ImageReadAccessor accessor(img3D); + image->SetVolume(accessor.GetData(), t); + } + } + else + { + AccessFixedPixelTypeByItk_3(image, itkErode, (unsigned char), image, factor, structuralElement); + } - MITK_INFO << "Finished Erode"; + MITK_INFO << "Finished Erode"; } void mitk::MorphologicalOperations::Dilate(mitk::Image::Pointer& image, int factor, mitk::MorphologicalOperations::StructuralElementType structuralElement) { - MITK_INFO << "Start Dilate..."; + MITK_INFO << "Start Dilate..."; - int timeSteps = static_cast(image->GetTimeSteps()); + int timeSteps = static_cast(image->GetTimeSteps()); - if (timeSteps > 1) - { - mitk::ImageTimeSelector::Pointer timeSelector = mitk::ImageTimeSelector::New(); - timeSelector->SetInput(image); + if (timeSteps > 1) + { + mitk::ImageTimeSelector::Pointer timeSelector = mitk::ImageTimeSelector::New(); + timeSelector->SetInput(image); - for (int t = 0; t < timeSteps; ++t) - { - MITK_INFO << " Processing time step " << t; + for (int t = 0; t < timeSteps; ++t) + { + MITK_INFO << " Processing time step " << t; - timeSelector->SetTimeNr(t); - timeSelector->Update(); + timeSelector->SetTimeNr(t); + timeSelector->Update(); - mitk::Image::Pointer img3D = timeSelector->GetOutput(); - img3D->DisconnectPipeline(); + mitk::Image::Pointer img3D = timeSelector->GetOutput(); + img3D->DisconnectPipeline(); - AccessFixedPixelTypeByItk_3(img3D, itkDilate, (unsigned char), img3D, factor, structuralElement); + AccessFixedPixelTypeByItk_3(img3D, itkDilate, (unsigned char), img3D, factor, structuralElement); - mitk::ImageReadAccessor accessor(img3D); - image->SetVolume(accessor.GetData(), t); - } - } - else - { - AccessFixedPixelTypeByItk_3(image, itkDilate, (unsigned char), image, factor, structuralElement); - } + mitk::ImageReadAccessor accessor(img3D); + image->SetVolume(accessor.GetData(), t); + } + } + else + { + AccessFixedPixelTypeByItk_3(image, itkDilate, (unsigned char), image, factor, structuralElement); + } - MITK_INFO << "Finished Dilate"; + MITK_INFO << "Finished Dilate"; } void mitk::MorphologicalOperations::Opening(mitk::Image::Pointer& image, int factor, mitk::MorphologicalOperations::StructuralElementType structuralElement) { - MITK_INFO << "Start Opening..."; + MITK_INFO << "Start Opening..."; - int timeSteps = static_cast(image->GetTimeSteps()); + int timeSteps = static_cast(image->GetTimeSteps()); - if (timeSteps > 1) - { - mitk::ImageTimeSelector::Pointer timeSelector = mitk::ImageTimeSelector::New(); - timeSelector->SetInput(image); + if (timeSteps > 1) + { + mitk::ImageTimeSelector::Pointer timeSelector = mitk::ImageTimeSelector::New(); + timeSelector->SetInput(image); - for (int t = 0; t < timeSteps; ++t) - { - MITK_INFO << " Processing time step " << t; + for (int t = 0; t < timeSteps; ++t) + { + MITK_INFO << " Processing time step " << t; - timeSelector->SetTimeNr(t); - timeSelector->Update(); + timeSelector->SetTimeNr(t); + timeSelector->Update(); - mitk::Image::Pointer img3D = timeSelector->GetOutput(); - img3D->DisconnectPipeline(); + mitk::Image::Pointer img3D = timeSelector->GetOutput(); + img3D->DisconnectPipeline(); - AccessFixedPixelTypeByItk_3(img3D, itkOpening, (unsigned char), img3D, factor, structuralElement); + AccessFixedPixelTypeByItk_3(img3D, itkOpening, (unsigned char), img3D, factor, structuralElement); - mitk::ImageReadAccessor accessor(img3D); - image->SetVolume(accessor.GetData(), t); - } - } - else - { - AccessFixedPixelTypeByItk_3(image, itkOpening, (unsigned char), image, factor, structuralElement); - } + mitk::ImageReadAccessor accessor(img3D); + image->SetVolume(accessor.GetData(), t); + } + } + else + { + AccessFixedPixelTypeByItk_3(image, itkOpening, (unsigned char), image, factor, structuralElement); + } - MITK_INFO << "Finished Opening"; + MITK_INFO << "Finished Opening"; } void mitk::MorphologicalOperations::FillHoles(mitk::Image::Pointer &image) { - MITK_INFO << "Start FillHole..."; + MITK_INFO << "Start FillHole..."; - int timeSteps = static_cast(image->GetTimeSteps()); + int timeSteps = static_cast(image->GetTimeSteps()); - if (timeSteps > 1) - { - mitk::ImageTimeSelector::Pointer timeSelector = mitk::ImageTimeSelector::New(); - timeSelector->SetInput(image); + if (timeSteps > 1) + { + mitk::ImageTimeSelector::Pointer timeSelector = mitk::ImageTimeSelector::New(); + timeSelector->SetInput(image); - for (int t = 0; t < timeSteps; ++t) - { - MITK_INFO << " Processing time step " << t; + for (int t = 0; t < timeSteps; ++t) + { + MITK_INFO << " Processing time step " << t; - timeSelector->SetTimeNr(t); - timeSelector->Update(); + timeSelector->SetTimeNr(t); + timeSelector->Update(); - mitk::Image::Pointer img3D = timeSelector->GetOutput(); - img3D->DisconnectPipeline(); + mitk::Image::Pointer img3D = timeSelector->GetOutput(); + img3D->DisconnectPipeline(); - AccessFixedPixelTypeByItk_1(img3D, itkFillHoles, (unsigned char), img3D); + AccessFixedPixelTypeByItk_1(img3D, itkFillHoles, (unsigned char), img3D); - mitk::ImageReadAccessor accessor(img3D); - image->SetVolume(accessor.GetData(), t); - } - } - else - { - AccessFixedPixelTypeByItk_1(image, itkFillHoles, (unsigned char), image); - } + mitk::ImageReadAccessor accessor(img3D); + image->SetVolume(accessor.GetData(), t); + } + } + else + { + AccessFixedPixelTypeByItk_1(image, itkFillHoles, (unsigned char), image); + } - MITK_INFO << "Finished FillHole"; + MITK_INFO << "Finished FillHole"; } template -void mitk::MorphologicalOperations::itkClosing(itk::Image* sourceImage, mitk::Image::Pointer& resultImage, int factor, mitk::MorphologicalOperations::StructuralElementType structuralElement) +void mitk::MorphologicalOperations::itkClosing(itk::Image* sourceImage, mitk::Image::Pointer& resultImage, int factor, mitk::MorphologicalOperations::StructuralElementType structuralElementFlags) { - typedef itk::Image ImageType; - typedef itk::BinaryBallStructuringElement BallType; - typedef itk::BinaryCrossStructuringElement CrossType; - typedef typename itk::BinaryMorphologicalClosingImageFilter BallClosingFilterType; - typedef typename itk::BinaryMorphologicalClosingImageFilter CrossClosingFilterType; - - if (structuralElement == Ball) - { - BallType ball; - ball.SetRadius(factor); - ball.CreateStructuringElement(); - - typename BallClosingFilterType::Pointer closingFilter = BallClosingFilterType::New(); - closingFilter->SetKernel(ball); - closingFilter->SetInput(sourceImage); - closingFilter->SetForegroundValue(1); - closingFilter->UpdateLargestPossibleRegion(); - - mitk::CastToMitkImage(closingFilter->GetOutput(), resultImage); - } - else - { - CrossType cross; - cross.CreateStructuringElement(); - - typename CrossClosingFilterType::Pointer closingFilter = CrossClosingFilterType::New(); - closingFilter->SetKernel(cross); - closingFilter->SetInput(sourceImage); - closingFilter->SetForegroundValue(1); - closingFilter->UpdateLargestPossibleRegion(); - - mitk::CastToMitkImage(closingFilter->GetOutput(), resultImage); - } + typedef itk::Image ImageType; + typedef itk::BinaryBallStructuringElement BallType; + typedef itk::BinaryCrossStructuringElement CrossType; + typedef typename itk::BinaryMorphologicalClosingImageFilter BallClosingFilterType; + typedef typename itk::BinaryMorphologicalClosingImageFilter CrossClosingFilterType; + + if (structuralElementFlags & (Ball_Axial | Ball_Coronal | Ball_Sagital)) + { BallType ball = CreateStructuringElement(structuralElementFlags,factor); + + typename BallClosingFilterType::Pointer closingFilter = BallClosingFilterType::New(); + closingFilter->SetKernel(ball); + closingFilter->SetInput(sourceImage); + closingFilter->SetForegroundValue(1); + closingFilter->UpdateLargestPossibleRegion(); + + mitk::CastToMitkImage(closingFilter->GetOutput(), resultImage); + } + else + { CrossType cross = CreateStructuringElement(structuralElementFlags,factor); + + typename CrossClosingFilterType::Pointer closingFilter = CrossClosingFilterType::New(); + closingFilter->SetKernel(cross); + closingFilter->SetInput(sourceImage); + closingFilter->SetForegroundValue(1); + closingFilter->UpdateLargestPossibleRegion(); + + mitk::CastToMitkImage(closingFilter->GetOutput(), resultImage); + } } template -void mitk::MorphologicalOperations::itkErode(itk::Image* sourceImage, mitk::Image::Pointer& resultImage, int factor, mitk::MorphologicalOperations::StructuralElementType structuralElement) +void mitk::MorphologicalOperations::itkErode(itk::Image* sourceImage, mitk::Image::Pointer& resultImage, int factor, mitk::MorphologicalOperations::StructuralElementType structuralElementFlags) { - typedef itk::Image ImageType; - typedef itk::BinaryBallStructuringElement BallType; - typedef itk::BinaryCrossStructuringElement CrossType; - typedef typename itk::BinaryErodeImageFilter BallErodeFilterType; - typedef typename itk::BinaryErodeImageFilter CrossErodeFilterType; - - if (structuralElement == Ball) - { - BallType ball; - ball.SetRadius(factor); - ball.CreateStructuringElement(); - - typename BallErodeFilterType::Pointer erodeFilter = BallErodeFilterType::New(); - erodeFilter->SetKernel(ball); - erodeFilter->SetInput(sourceImage); - erodeFilter->SetErodeValue(1); - erodeFilter->UpdateLargestPossibleRegion(); - - mitk::CastToMitkImage(erodeFilter->GetOutput(), resultImage); - } - else - { - CrossType cross; - cross.CreateStructuringElement(); - - typename CrossErodeFilterType::Pointer erodeFilter = CrossErodeFilterType::New(); - erodeFilter->SetKernel(cross); - erodeFilter->SetInput(sourceImage); - erodeFilter->SetErodeValue(1); - erodeFilter->UpdateLargestPossibleRegion(); - - mitk::CastToMitkImage(erodeFilter->GetOutput(), resultImage); - } + typedef itk::Image ImageType; + typedef itk::BinaryBallStructuringElement BallType; + typedef itk::BinaryCrossStructuringElement CrossType; + typedef typename itk::BinaryErodeImageFilter BallErodeFilterType; + typedef typename itk::BinaryErodeImageFilter CrossErodeFilterType; + + if (structuralElementFlags & (Ball_Axial | Ball_Coronal | Ball_Sagital)) + { BallType ball = CreateStructuringElement(structuralElementFlags,factor); + + typename BallErodeFilterType::Pointer erodeFilter = BallErodeFilterType::New(); + erodeFilter->SetKernel(ball); + erodeFilter->SetInput(sourceImage); + erodeFilter->SetErodeValue(1); + erodeFilter->UpdateLargestPossibleRegion(); + + mitk::CastToMitkImage(erodeFilter->GetOutput(), resultImage); + } + else + { CrossType cross = CreateStructuringElement(structuralElementFlags,factor); + + typename CrossErodeFilterType::Pointer erodeFilter = CrossErodeFilterType::New(); + erodeFilter->SetKernel(cross); + erodeFilter->SetInput(sourceImage); + erodeFilter->SetErodeValue(1); + erodeFilter->UpdateLargestPossibleRegion(); + + mitk::CastToMitkImage(erodeFilter->GetOutput(), resultImage); + } } template -void mitk::MorphologicalOperations::itkDilate(itk::Image* sourceImage, mitk::Image::Pointer& resultImage, int factor, mitk::MorphologicalOperations::StructuralElementType structuralElement) +void mitk::MorphologicalOperations::itkDilate(itk::Image* sourceImage, mitk::Image::Pointer& resultImage, int factor, mitk::MorphologicalOperations::StructuralElementType structuralElementFlags) { - typedef itk::Image ImageType; - typedef itk::BinaryBallStructuringElement BallType; - typedef itk::BinaryCrossStructuringElement CrossType; - typedef typename itk::BinaryDilateImageFilter BallDilateFilterType; - typedef typename itk::BinaryDilateImageFilter CrossDilateFilterType; - - if (structuralElement == Ball) - { - BallType ball; - ball.SetRadius(factor); - ball.CreateStructuringElement(); - - typename BallDilateFilterType::Pointer dilateFilter = BallDilateFilterType::New(); - dilateFilter->SetKernel(ball); - dilateFilter->SetInput(sourceImage); - dilateFilter->SetDilateValue(1); - dilateFilter->UpdateLargestPossibleRegion(); - - mitk::CastToMitkImage(dilateFilter->GetOutput(), resultImage); - } - else - { - CrossType cross; - cross.CreateStructuringElement(); - - typename CrossDilateFilterType::Pointer dilateFilter = CrossDilateFilterType::New(); - - dilateFilter->SetKernel(cross); - dilateFilter->SetInput(sourceImage); - dilateFilter->SetDilateValue(1); - dilateFilter->UpdateLargestPossibleRegion(); - - mitk::CastToMitkImage(dilateFilter->GetOutput(), resultImage); - } + typedef itk::Image ImageType; + typedef itk::BinaryBallStructuringElement BallType; + typedef itk::BinaryCrossStructuringElement CrossType; + typedef typename itk::BinaryDilateImageFilter BallDilateFilterType; + typedef typename itk::BinaryDilateImageFilter CrossDilateFilterType; + + if (structuralElementFlags & (Ball_Axial | Ball_Coronal | Ball_Sagital)) + { BallType ball = CreateStructuringElement(structuralElementFlags,factor); + + typename BallDilateFilterType::Pointer dilateFilter = BallDilateFilterType::New(); + dilateFilter->SetKernel(ball); + dilateFilter->SetInput(sourceImage); + dilateFilter->SetDilateValue(1); + dilateFilter->UpdateLargestPossibleRegion(); + + mitk::CastToMitkImage(dilateFilter->GetOutput(), resultImage); + } + else + { CrossType cross = CreateStructuringElement(structuralElementFlags,factor); + + typename CrossDilateFilterType::Pointer dilateFilter = CrossDilateFilterType::New(); + dilateFilter->SetKernel(cross); + dilateFilter->SetInput(sourceImage); + dilateFilter->SetDilateValue(1); + dilateFilter->UpdateLargestPossibleRegion(); + + mitk::CastToMitkImage(dilateFilter->GetOutput(), resultImage); + } } template -void mitk::MorphologicalOperations::itkOpening(itk::Image* sourceImage, mitk::Image::Pointer& resultImage, int factor, mitk::MorphologicalOperations::StructuralElementType structuralElement) +void mitk::MorphologicalOperations::itkOpening(itk::Image* sourceImage, mitk::Image::Pointer& resultImage, int factor, mitk::MorphologicalOperations::StructuralElementType structuralElementFlags) { - typedef itk::Image ImageType; - typedef itk::BinaryBallStructuringElement BallType; - typedef itk::BinaryCrossStructuringElement CrossType; - typedef typename itk::BinaryMorphologicalOpeningImageFilter BallOpeningFiltertype; - typedef typename itk::BinaryMorphologicalOpeningImageFilter CrossOpeningFiltertype; - - if (structuralElement == Ball) - { - BallType ball; - ball.SetRadius(factor); - ball.CreateStructuringElement(); - - typename BallOpeningFiltertype::Pointer openingFilter = BallOpeningFiltertype::New(); - openingFilter->SetKernel(ball); - openingFilter->SetInput(sourceImage); - openingFilter->SetForegroundValue(1); - openingFilter->SetBackgroundValue(0); - openingFilter->UpdateLargestPossibleRegion(); - - mitk::CastToMitkImage(openingFilter->GetOutput(), resultImage); - } - else - { - CrossType cross; - cross.CreateStructuringElement(); - - typename CrossOpeningFiltertype::Pointer openingFilter = CrossOpeningFiltertype::New(); - openingFilter->SetKernel(cross); - openingFilter->SetInput(sourceImage); - openingFilter->SetForegroundValue(1); - openingFilter->SetBackgroundValue(0); - openingFilter->UpdateLargestPossibleRegion(); - - mitk::CastToMitkImage(openingFilter->GetOutput(), resultImage); - } + typedef itk::Image ImageType; + typedef itk::BinaryBallStructuringElement BallType; + typedef itk::BinaryCrossStructuringElement CrossType; + typedef typename itk::BinaryMorphologicalOpeningImageFilter BallOpeningFiltertype; + typedef typename itk::BinaryMorphologicalOpeningImageFilter CrossOpeningFiltertype; + + if (structuralElementFlags & (Ball_Axial | Ball_Coronal | Ball_Sagital)) + { BallType ball = CreateStructuringElement(structuralElementFlags,factor); + + typename BallOpeningFiltertype::Pointer openingFilter = BallOpeningFiltertype::New(); + openingFilter->SetKernel(ball); + openingFilter->SetInput(sourceImage); + openingFilter->SetForegroundValue(1); + openingFilter->SetBackgroundValue(0); + openingFilter->UpdateLargestPossibleRegion(); + + mitk::CastToMitkImage(openingFilter->GetOutput(), resultImage); + } + else + { CrossType cross = CreateStructuringElement(structuralElementFlags,factor); + + typename CrossOpeningFiltertype::Pointer openingFilter = CrossOpeningFiltertype::New(); + openingFilter->SetKernel(cross); + openingFilter->SetInput(sourceImage); + openingFilter->SetForegroundValue(1); + openingFilter->SetBackgroundValue(0); + openingFilter->UpdateLargestPossibleRegion(); + + mitk::CastToMitkImage(openingFilter->GetOutput(), resultImage); + } } template void mitk::MorphologicalOperations::itkFillHoles(itk::Image* sourceImage, mitk::Image::Pointer& resultImage) { - typedef itk::Image ImageType; - typedef typename itk::BinaryFillholeImageFilter FillHoleFilterType; + typedef itk::Image ImageType; + typedef typename itk::BinaryFillholeImageFilter FillHoleFilterType; - typename FillHoleFilterType::Pointer fillHoleFilter = FillHoleFilterType::New(); - fillHoleFilter->SetInput(sourceImage); - fillHoleFilter->SetForegroundValue(1); - fillHoleFilter->UpdateLargestPossibleRegion(); + typename FillHoleFilterType::Pointer fillHoleFilter = FillHoleFilterType::New(); + fillHoleFilter->SetInput(sourceImage); + fillHoleFilter->SetForegroundValue(1); + fillHoleFilter->UpdateLargestPossibleRegion(); - mitk::CastToMitkImage(fillHoleFilter->GetOutput(), resultImage); + mitk::CastToMitkImage(fillHoleFilter->GetOutput(), resultImage); }