template <class ImagePixelType, class MaskPixelType> int update()
    {
        typedef itk::Image<ImagePixelType, 3> ImageType;
        typedef itk::Image<MaskPixelType,  3> MaskType;

        if ( !input ||!input->data() || !mask ||!mask->data())
            return EXIT_FAILURE;

        typedef itk::MaskImageFilter< ImageType,  MaskType> MaskFilterType;
        typename MaskFilterType::Pointer maskFilter = MaskFilterType::New();

        typename ImageType::Pointer imgInput = dynamic_cast<ImageType *> ( ( itk::Object* ) ( input->data() )) ;
        typename MaskType::Pointer maskInput = dynamic_cast<MaskType *>  ( ( itk::Object* ) ( mask->data() )) ;

        try
        {
            maskInput->SetOrigin(imgInput->GetOrigin());
            maskInput->SetSpacing(imgInput->GetSpacing());
            maskFilter->SetInput(imgInput);
            maskFilter->SetMaskingValue(maskBackgroundValue);
            maskFilter->SetMaskImage(maskInput);

            //Outside values set to the lowest reachable value

            typedef itk::MinimumMaximumImageCalculator <ImageType> ImageCalculatorFilterType;
 
            typename ImageCalculatorFilterType::Pointer imageCalculatorFilter
                    = ImageCalculatorFilterType::New ();
            imageCalculatorFilter->SetImage(imgInput);
            imageCalculatorFilter->ComputeMinimum();
            maskFilter->SetOutsideValue(std::min(double(imageCalculatorFilter->GetMinimum()), 0.0));
            maskFilter->Update();
            output->setData(maskFilter->GetOutput());
        }
        catch( itk::ExceptionObject & err )
        {
            std::cerr << "ExceptionObject caught in medMaskApplication!" << std::endl;
            std::cerr << err << std::endl;
            return EXIT_FAILURE;
        }

        medUtilities::setDerivedMetaData(output, input, "masked");

        return EXIT_SUCCESS;
    }
Esempio n. 2
0
void CreateNewMask(const itk::Image< TPixel, VImageDimension > *image, MaskParameter param, mitk::Image::Pointer &output)
{
  int transform[3][2];
  transform[0][0] = 1; transform[0][1] = 2;
  transform[1][0] = 0; transform[1][1] = 2;
  transform[2][0] = 0; transform[2][1] = 1;

  typedef itk::Image<unsigned short, VImageDimension> MaskType;
  typedef itk::Image<unsigned short, 2> Mask2DType;
  typename Mask2DType::Pointer mask = Mask2DType::New();
  mitk::CastToItkImage(param.mask, mask);


  typename MaskType::Pointer mask3D = MaskType::New();
  mask3D->SetRegions(image->GetLargestPossibleRegion());
  mask3D->SetSpacing(image->GetSpacing());
  mask3D->SetOrigin(image->GetOrigin());
  mask3D->Allocate();

  itk::ImageRegionIteratorWithIndex<MaskType> iter(mask3D, mask3D->GetLargestPossibleRegion());
  while (!iter.IsAtEnd())
  {
    auto index = iter.GetIndex();
    iter.Set(0);
    if (index[param.axis] == param.slice)
    {
      Mask2DType::IndexType index2D;
      index2D[0] = index[transform[param.axis][0]];
      index2D[1] = index[transform[param.axis][1]];
      auto pixel = mask->GetPixel(index2D);
      iter.Set(pixel);
    }
    ++iter;
  }

  mitk::CastToMitkImage(mask3D, output);

}