void
ResampleImage(itk::Image<TPixel, VImageDimension>* itkImage, float resolution, mitk::Image::Pointer& newImage)
{
  typedef itk::Image<TPixel, VImageDimension> ImageType;
  typedef itk::ResampleImageFilter<ImageType, ImageType> ResampleFilterType;

  typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
  auto spacing = itkImage->GetSpacing();
  auto size = itkImage->GetLargestPossibleRegion().GetSize();

  for (unsigned int i = 0; i < VImageDimension; ++i)
  {
    size[i] = size[i] / (1.0*resolution)*(1.0*spacing[i])+1.0;
  }
  spacing.Fill(resolution);

  resampler->SetInput(itkImage);
  resampler->SetSize(size);
  resampler->SetOutputSpacing(spacing);
  resampler->SetOutputOrigin(itkImage->GetOrigin());
  resampler->SetOutputDirection(itkImage->GetDirection());
  resampler->Update();

  newImage->InitializeByItk(resampler->GetOutput());
  mitk::GrabItkImageMemory(resampler->GetOutput(), newImage);
}
static void
ResampleMask(itk::Image<TPixel, VImageDimension>* itkMoving, mitk::Image::Pointer ref, mitk::Image::Pointer& newMask)
{
  typedef itk::Image< TPixel, VImageDimension>          LMaskImageType;
  typedef itk::NearestNeighborInterpolateImageFunction< LMaskImageType> NearestNeighborInterpolateImageFunctionType;
  typedef itk::ResampleImageFilter<LMaskImageType, LMaskImageType> ResampleFilterType;

  typename NearestNeighborInterpolateImageFunctionType::Pointer nn_interpolator = NearestNeighborInterpolateImageFunctionType::New();
  typename LMaskImageType::Pointer itkRef = LMaskImageType::New();
  mitk::CastToItkImage(ref, itkRef);


  typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
  resampler->SetInput(itkMoving);
  resampler->SetReferenceImage(itkRef);
  resampler->UseReferenceImageOn();
  resampler->SetInterpolator(nn_interpolator);
  resampler->Update();

  newMask->InitializeByItk(resampler->GetOutput());
  mitk::GrabItkImageMemory(resampler->GetOutput(), newMask);
}
static void
CreateNoNaNMask(itk::Image<TPixel, VImageDimension>* itkValue, mitk::Image::Pointer mask, mitk::Image::Pointer& newMask)
{
  typedef itk::Image< TPixel, VImageDimension>                 LFloatImageType;
  typedef itk::Image< unsigned short, VImageDimension>          LMaskImageType;
  typename LMaskImageType::Pointer itkMask = LMaskImageType::New();

  mitk::CastToItkImage(mask, itkMask);

  typedef itk::ImageDuplicator< LMaskImageType > DuplicatorType;
  typename DuplicatorType::Pointer duplicator = DuplicatorType::New();
  duplicator->SetInputImage(itkMask);
  duplicator->Update();

  auto tmpMask = duplicator->GetOutput();

  itk::ImageRegionIterator<LMaskImageType> mask1Iter(itkMask, itkMask->GetLargestPossibleRegion());
  itk::ImageRegionIterator<LMaskImageType> mask2Iter(tmpMask, tmpMask->GetLargestPossibleRegion());
  itk::ImageRegionIterator<LFloatImageType> imageIter(itkValue, itkValue->GetLargestPossibleRegion());
  while (!mask1Iter.IsAtEnd())
  {
    mask2Iter.Set(0);
    if (mask1Iter.Value() > 0)
    {
      // Is not NaN
      if (imageIter.Value() == imageIter.Value())
      {
        mask2Iter.Set(1);
      }
    }
    ++mask1Iter;
    ++mask2Iter;
    ++imageIter;
  }

  newMask->InitializeByItk(tmpMask);
  mitk::GrabItkImageMemory(tmpMask, newMask);
}
      void PartialVolumeAnalysisClusteringCalculator::InternalGenerateQuantileImage(
          const itk::Image< TPixel, VImageDimension > *image, double* q,
          mitk::Image::Pointer outImage1, mitk::Image::Pointer outImage2 ) const
  {

    typedef itk::Image< TPixel, VImageDimension > ImageType;
    typedef itk::Image< itk::RGBAPixel<unsigned char>, VImageDimension > DisplayImageType;
    typedef itk::Image< float, VImageDimension > ProbImageType;

    typename ProbImageType::Pointer probimage = ProbImageType::New();
    probimage->SetSpacing( image->GetSpacing() );   // Set the image spacing
    probimage->SetOrigin( image->GetOrigin() );     // Set the image origin
    probimage->SetDirection( image->GetDirection() );  // Set the image direction
    probimage->SetRegions( image->GetLargestPossibleRegion() );
    probimage->Allocate();
    probimage->FillBuffer(0);

    typename DisplayImageType::Pointer displayimage = DisplayImageType::New();
    displayimage->SetSpacing( image->GetSpacing() );   // Set the image spacing
    displayimage->SetOrigin( image->GetOrigin() );     // Set the image origin
    displayimage->SetDirection( image->GetDirection() );  // Set the image direction
    displayimage->SetRegions( image->GetLargestPossibleRegion() );
    displayimage->Allocate();

    typename DisplayImageType::PixelType rgba;
    rgba.Set(0.0f, 0.0f, 0.0f, 0.0f);
    displayimage->FillBuffer(rgba);

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

    itk::ImageRegionIterator<ProbImageType>
        itprob(probimage, probimage->GetLargestPossibleRegion());

    itk::ImageRegionIterator<DisplayImageType>
        itdisp(displayimage, displayimage->GetLargestPossibleRegion());

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

    while( !itimage.IsAtEnd() )
    {
      if(itimage.Get() > q[0] && itimage.Get() < q[1])
      {
        itprob.Set(1.0f);
      }

      ++itimage;
      ++itprob;
    }

    itprob.GoToBegin();
    itdisp.GoToBegin();

    while( !itprob.IsAtEnd() )
    {
      if(itprob.Get())
      {
        typename DisplayImageType::PixelType rgba;
        rgba.Set(255.0f, 0.0f, 0.0f, 255.0f);
        itdisp.Set( rgba );
      }
      ++itprob;
      ++itdisp;
    }

    outImage1->InitializeByItk(probimage.GetPointer());
    outImage1->SetVolume(probimage->GetBufferPointer());

    outImage2->InitializeByItk(displayimage.GetPointer());
    outImage2->SetVolume(displayimage->GetBufferPointer());

  }
      void PartialVolumeAnalysisClusteringCalculator::InternalGenerateProbabilityImage(
          const itk::Image< TPixel, VImageDimension > *image,
          const HelperStructClusteringResults clusterResults,
          mitk::Image::Pointer outImage1, mitk::Image::Pointer outImage2 ) const
  {

    typedef itk::Image< TPixel, VImageDimension > ImageType;
    typedef itk::Image< itk::RGBAPixel<unsigned char>, VImageDimension > DisplayImageType;
    typedef itk::Image< float, VImageDimension > ProbImageType;

    typename ProbImageType::Pointer probimage = ProbImageType::New();
    probimage->SetSpacing( image->GetSpacing() );   // Set the image spacing
    probimage->SetOrigin( image->GetOrigin() );     // Set the image origin
    probimage->SetDirection( image->GetDirection() );  // Set the image direction
    probimage->SetRegions( image->GetLargestPossibleRegion() );
    probimage->Allocate();
    probimage->FillBuffer(0);

    typename DisplayImageType::Pointer displayimage = DisplayImageType::New();
    displayimage->SetSpacing( image->GetSpacing() );   // Set the image spacing
    displayimage->SetOrigin( image->GetOrigin() );     // Set the image origin
    displayimage->SetDirection( image->GetDirection() );  // Set the image direction
    displayimage->SetRegions( image->GetLargestPossibleRegion() );
    displayimage->Allocate();

    typename DisplayImageType::PixelType rgba;
    rgba.Set(0.0f, 0.0f, 0.0f, 0.0f);
    displayimage->FillBuffer(rgba);

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

    itk::ImageRegionIterator<ProbImageType>
        itprob(probimage, probimage->GetLargestPossibleRegion());

    itk::ImageRegionIterator<DisplayImageType>
        itdisp(displayimage, displayimage->GetLargestPossibleRegion());

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

    MitkHistType::IndexType index(1);
    float maxp = 0;
    while( !itimage.IsAtEnd() )
    {
      if(itimage.Get())
      {
        MitkHistType::MeasurementVectorType meas(1);
        meas.Fill(itimage.Get());
        double aposteriori = 0;
        bool success = clusterResults.interestingHist->GetIndex(meas, index );
        if(success)
        {
          double aprioriProb = clusterResults.interestingHist->GetFrequency(index);
          double intensityProb = clusterResults.totalHist->GetFrequency(index);
          double p_interesting = clusterResults.p_interesting;
          aposteriori = p_interesting * aprioriProb / intensityProb;
        }
        else
        {
          MITK_ERROR << "index not found in histogram";
        }

        if(aposteriori > 0.0000000000000001)
        {
          itprob.Set( aposteriori );
          maxp = aposteriori > maxp ? aposteriori : maxp;
        }
        else
        {
          itprob.Set(0.0f);
        }
      }

      ++itimage;
      ++itprob;
    }

    itprob.GoToBegin();
    itdisp.GoToBegin();

    while( !itprob.IsAtEnd() )
    {
      if(itprob.Get())
      {
        typename DisplayImageType::PixelType rgba;
        rgba.Set(255.0f, 0.0f, 0.0f, 255.0f*(itprob.Get()/maxp));
        itdisp.Set( rgba );
      }
      ++itprob;
      ++itdisp;
    }

    outImage1->InitializeByItk(probimage.GetPointer());
    outImage1->SetVolume(probimage->GetBufferPointer());

    outImage2->InitializeByItk(displayimage.GetPointer());
    outImage2->SetVolume(displayimage->GetBufferPointer());

  }
  void PartialVolumeAnalysisClusteringCalculator::InternalGenerateRGB( HelperStructRGBChannels *rgbin, mitk::Image::Pointer retval ) const
  {
    typedef itk::Image< float, VImageDimension > ProbImageType;
    typedef itk::Image< typename itk::RGBAPixel<unsigned char>, VImageDimension > RGBImageType;

    typedef mitk::ImageToItk<ProbImageType> CastFilterType;
    typename CastFilterType::Pointer castFilter = CastFilterType::New();
    castFilter->SetInput( rgbin->r );
    castFilter->Update();

    typename ProbImageType::Pointer r = castFilter->GetOutput();

    castFilter = CastFilterType::New();
    castFilter->SetInput( rgbin->g );
    castFilter->Update();
    typename ProbImageType::Pointer g = castFilter->GetOutput();

    typename RGBImageType::Pointer rgb = RGBImageType::New();
    rgb->SetSpacing( g->GetSpacing() );   // Set the image spacing
    rgb->SetOrigin( g->GetOrigin() );     // Set the image origin
    rgb->SetDirection( g->GetDirection() );  // Set the image direction
    rgb->SetRegions( g->GetLargestPossibleRegion() );
    rgb->Allocate();

    itk::ImageRegionConstIterator<ProbImageType>
        itr(r, r->GetLargestPossibleRegion());
    itk::ImageRegionConstIterator<ProbImageType>
        itg(g, g->GetLargestPossibleRegion());

    itk::ImageRegionIterator<RGBImageType>
        itrgb(rgb, rgb->GetLargestPossibleRegion());

    itr.GoToBegin();
    itg.GoToBegin();

    float maxr = 0;
    float maxg = 0;

    while( !itr.IsAtEnd() )
    {
      typename ProbImageType::PixelType pr = itr.Get();
      typename ProbImageType::PixelType pg = itg.Get();

      if(pr > maxr)
      {
        maxr = pr;
      }

      if(pg > maxg)
      {
        maxg = pg;
      }

      ++itr;
      ++itg;
    }

    itr.GoToBegin();
    itg.GoToBegin();
    itrgb.GoToBegin();

    while( !itr.IsAtEnd() )
    {
      typename ProbImageType::PixelType pr = itr.Get();
      typename ProbImageType::PixelType pg = itg.Get();

      typename RGBImageType::PixelType prgb;

      float valr = (pr/maxr)*255.0f;
      float valg = (pg/maxg)*255.0f;
      float alpha = valr>valg ? valr : valg;
      prgb.Set(valr, valg, 0.0f, alpha);

      itrgb.Set(prgb);

      ++itr;
      ++itg;
      ++itrgb;
    }

    retval->InitializeByItk(rgb.GetPointer());
    retval->SetVolume(rgb->GetBufferPointer());

  }