void mitk::MorphologicalOperations::FillHoles(mitk::Image::Pointer &image)
{
  MITK_INFO << "Start FillHole...";

  int timeSteps = static_cast<int>(image->GetTimeSteps());

  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;

      timeSelector->SetTimeNr(t);
      timeSelector->Update();

      mitk::Image::Pointer img3D = timeSelector->GetOutput();
      img3D->DisconnectPipeline();

      AccessByItk_1(img3D, itkFillHoles, img3D);

      mitk::ImageReadAccessor accessor(img3D);
      image->SetVolume(accessor.GetData(), t);
    }
  }
  else
  {
    AccessByItk_1(image, itkFillHoles, image);
  }

  MITK_INFO << "Finished FillHole";
}
void mitk::MorphologicalOperations::Closing(mitk::Image::Pointer &image,
                                            int factor,
                                            mitk::MorphologicalOperations::StructuralElementType structuralElement)
{
  MITK_INFO << "Start Closing...";

  int timeSteps = static_cast<int>(image->GetTimeSteps());

  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;

      timeSelector->SetTimeNr(t);
      timeSelector->Update();

      mitk::Image::Pointer img3D = timeSelector->GetOutput();
      img3D->DisconnectPipeline();

      AccessByItk_3(img3D, itkClosing, img3D, factor, structuralElement);

      mitk::ImageReadAccessor accessor(img3D);
      image->SetVolume(accessor.GetData(), t);
    }
  }
  else
  {
    AccessByItk_3(image, itkClosing, image, factor, structuralElement);
  }

  MITK_INFO << "Finished Closing";
}
      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::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::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());

  }