Exemplo n.º 1
0
static int
CalculateGlSZMatrix(itk::Image<TPixel, VImageDimension>* itkImage,
                    itk::Image<unsigned short, VImageDimension>* mask,
                    std::vector<itk::Offset<VImageDimension> > offsets,
                    bool estimateLargestRegion,
                    mitk::GreyLevelSizeZoneMatrixHolder &holder)
{
  typedef itk::Image<TPixel, VImageDimension> ImageType;
  typedef itk::Image<unsigned short, VImageDimension> MaskImageType;
  typedef typename ImageType::IndexType IndexType;

  typedef itk::ImageRegionIteratorWithIndex<ImageType> ConstIterType;
  typedef itk::ImageRegionIteratorWithIndex<MaskImageType> ConstMaskIterType;

  auto region = mask->GetLargestPossibleRegion();
  typename MaskImageType::RegionType newRegion;
  newRegion.SetSize(region.GetSize());
  newRegion.SetIndex(region.GetIndex());

  ConstIterType imageIter(itkImage, itkImage->GetLargestPossibleRegion());
  ConstMaskIterType maskIter(mask, mask->GetLargestPossibleRegion());

  typename MaskImageType::Pointer visitedImage = MaskImageType::New();
  visitedImage->SetRegions(newRegion);
  visitedImage->Allocate();
  visitedImage->FillBuffer(0);

  int largestRegion = 0;

  while (!maskIter.IsAtEnd())
  {
    if (maskIter.Value() > 0 )
    {
      auto startIntensityIndex = holder.IntensityToIndex(imageIter.Value());
      std::vector<IndexType> indices;
      indices.push_back(maskIter.GetIndex());
      unsigned int steps = 0;

      while (indices.size() > 0)
      {
        auto currentIndex = indices.back();
        indices.pop_back();

        if (!region.IsInside(currentIndex))
        {
          continue;
        }

        auto wasVisited = visitedImage->GetPixel(currentIndex);
        auto newIntensityIndex = holder.IntensityToIndex(itkImage->GetPixel(currentIndex));
        auto isInMask = mask->GetPixel(currentIndex);

        if ((isInMask > 0) &&
            (newIntensityIndex == startIntensityIndex) &&
            (wasVisited < 1))
        {
          ++steps;
          visitedImage->SetPixel(currentIndex, 1);
          for (auto offset : offsets)
          {
            auto newIndex = currentIndex + offset;
            indices.push_back(newIndex);
            newIndex = currentIndex - offset;
            indices.push_back(newIndex);
          }

        }
      }
      if (steps > 0)
      {
        largestRegion = std::max<int>(steps, largestRegion);
        steps = std::min<unsigned int>(steps, holder.m_MaximumSize);
        if (!estimateLargestRegion)
        {
          holder.m_Matrix(startIntensityIndex, steps - 1) += 1;
        }
      }
    }
    ++imageIter;
    ++maskIter;
  }
  return largestRegion;
}
      void PartialVolumeAnalysisClusteringCalculator::InternalQuantify(
          const itk::Image< TPixel, VImageDimension > *image,
          mitk::Image::Pointer clusteredImage, double* retval, mitk::Image::Pointer mask ) const
  {
    typedef itk::Image< TPixel, VImageDimension > ImageType;
    typedef itk::Image< float, VImageDimension > ProbImageType;
    typedef itk::Image< unsigned char, VImageDimension > MaskImageType;

    typedef mitk::ImageToItk<ProbImageType> CastFilterType;
    typename CastFilterType::Pointer castFilter = CastFilterType::New();
    castFilter->SetInput( clusteredImage );
    castFilter->Update();
    typename ProbImageType::Pointer clusterImage = castFilter->GetOutput();

    typename MaskImageType::Pointer itkmask = 0;
    if(mask.IsNotNull())
    {
      typedef mitk::ImageToItk<MaskImageType> CastFilterType2;
      typename CastFilterType2::Pointer castFilter2 = CastFilterType2::New();
      castFilter2->SetInput( mask );
      castFilter2->Update();
      itkmask = castFilter2->GetOutput();
    }
    else
    {
      itkmask = MaskImageType::New();
      itkmask->SetSpacing( clusterImage->GetSpacing() );   // Set the image spacing
      itkmask->SetOrigin( clusterImage->GetOrigin() );     // Set the image origin
      itkmask->SetDirection( clusterImage->GetDirection() );  // Set the image direction
      itkmask->SetRegions( clusterImage->GetLargestPossibleRegion() );
      itkmask->Allocate();
      itkmask->FillBuffer(1);
    }

    itk::ImageRegionConstIterator<ImageType>
        itimage(image, image->GetLargestPossibleRegion());

    itk::ImageRegionConstIterator<ProbImageType>
        itprob(clusterImage, clusterImage->GetLargestPossibleRegion());

    itk::ImageRegionConstIterator<MaskImageType>
        itmask(itkmask, itkmask->GetLargestPossibleRegion());

    itimage.GoToBegin();
    itprob.GoToBegin();
    itmask.GoToBegin();

    double totalProb = 0;
    double measurement = 0;
    double error = 0;

    while( !itimage.IsAtEnd() && !itprob.IsAtEnd() && !itmask.IsAtEnd() )
    {
      double valImag = itimage.Get();
      double valProb = itprob.Get();
      double valMask = itmask.Get();

      typename ProbImageType::PixelType prop = valProb * valMask;

      totalProb   += prop;
      measurement += valImag * prop;
      error       += valImag * valImag * prop;

      ++itimage;
      ++itprob;
      ++itmask;
    }

    measurement = measurement / totalProb;
    error       = error       / totalProb;
    retval[0]   = measurement;
    retval[1]   = sqrt( error - measurement*measurement );

  }