void mitk::BinaryThresholdULTool::CreateNewSegmentationFromThreshold(DataNode* node)
{
  if (node)
  {
    Image::Pointer feedBackImage = dynamic_cast<Image*>( m_ThresholdFeedbackNode->GetData() );
    if (feedBackImage.IsNotNull())
    {
      // create a new image of the same dimensions and smallest possible pixel type
      DataNode::Pointer emptySegmentation = GetTargetSegmentationNode();

      if (emptySegmentation)
      {
        // actually perform a thresholding and ask for an organ type
        for (unsigned int timeStep = 0; timeStep < feedBackImage->GetTimeSteps(); ++timeStep)
        {
          try
          {
            ImageTimeSelector::Pointer timeSelector = ImageTimeSelector::New();
            timeSelector->SetInput( feedBackImage );
            timeSelector->SetTimeNr( timeStep );
            timeSelector->UpdateLargestPossibleRegion();
            Image::Pointer feedBackImage3D = timeSelector->GetOutput();

            if (feedBackImage3D->GetDimension() == 2)
            {
              AccessFixedDimensionByItk_2( feedBackImage3D, ITKSetVolume, 2, dynamic_cast<Image*>(emptySegmentation->GetData()), timeStep );
            }
            else
            {
              AccessFixedDimensionByItk_2( feedBackImage3D, ITKSetVolume, 3, dynamic_cast<Image*>(emptySegmentation->GetData()), timeStep );
            }

          }
          catch(...)
          {
            Tool::ErrorMessage("Error accessing single time steps of the original image. Cannot create segmentation.");
          }
        }

        //since we are maybe working on a smaller image, pad it to the size of the original image
        if (m_OriginalImageNode.GetPointer() != m_NodeForThresholding.GetPointer())
        {
          mitk::PadImageFilter::Pointer padFilter = mitk::PadImageFilter::New();

          padFilter->SetInput(0, dynamic_cast<mitk::Image*> (emptySegmentation->GetData()));
          padFilter->SetInput(1, dynamic_cast<mitk::Image*> (m_OriginalImageNode->GetData()));
          padFilter->SetBinaryFilter(true);
          padFilter->SetUpperThreshold(1);
          padFilter->SetLowerThreshold(1);
          padFilter->Update();

          emptySegmentation->SetData(padFilter->GetOutput());
        }

        m_ToolManager->SetWorkingData( emptySegmentation );
        m_ToolManager->GetWorkingData(0)->Modified();
      }
    }
  }
}
  bool CalculateSegmentationVolume::ReadyToRun()
  {
    Image::Pointer image;
    GetPointerParameter("Input", image);

    return image.IsNotNull() && GetGroupNode();
  }
void mitk::BinaryThresholdTool::SetupPreviewNode()
{
  if (m_NodeForThresholding.IsNotNull())
  {
    Image::Pointer image = dynamic_cast<Image*>(m_NodeForThresholding->GetData());
    Image::Pointer originalImage = dynamic_cast<Image*> (m_OriginalImageNode->GetData());

    if (image.IsNotNull())
    {
      mitk::Image* workingimage = dynamic_cast<mitk::Image*>(m_ToolManager->GetWorkingData(0)->GetData());

      if (workingimage)
      {
        m_ThresholdFeedbackNode->SetData(workingimage->Clone());

        //Let's paint the feedback node green...
        mitk::LabelSetImage::Pointer previewImage = dynamic_cast<mitk::LabelSetImage*> (m_ThresholdFeedbackNode->GetData());

        itk::RGBPixel<float> pixel;
        pixel[0] = 0.0f;
        pixel[1] = 1.0f;
        pixel[2] = 0.0f;
        previewImage->GetActiveLabel()->SetColor(pixel);
        previewImage->GetActiveLabelSet()->UpdateLookupTable(previewImage->GetActiveLabel()->GetValue());
      }
      else
        m_ThresholdFeedbackNode->SetData(mitk::Image::New());

      int layer(50);
      m_NodeForThresholding->GetIntProperty("layer", layer);
      m_ThresholdFeedbackNode->SetIntProperty("layer", layer + 1);

      if (DataStorage* ds = m_ToolManager->GetDataStorage())
      {
        if (!ds->Exists(m_ThresholdFeedbackNode))
          ds->Add(m_ThresholdFeedbackNode, m_OriginalImageNode);
      }

      if (image.GetPointer() == originalImage.GetPointer())
      {
        Image::StatisticsHolderPointer statistics = originalImage->GetStatistics();
        m_SensibleMinimumThresholdValue = static_cast<double>(statistics->GetScalarValueMin());
        m_SensibleMaximumThresholdValue = static_cast<double>(statistics->GetScalarValueMax());
      }

      if ((originalImage->GetPixelType().GetPixelType() == itk::ImageIOBase::SCALAR)
        && (originalImage->GetPixelType().GetComponentType() == itk::ImageIOBase::FLOAT || originalImage->GetPixelType().GetComponentType() == itk::ImageIOBase::DOUBLE))
        m_IsFloatImage = true;
      else
        m_IsFloatImage = false;


      m_CurrentThresholdValue = (m_SensibleMaximumThresholdValue + m_SensibleMinimumThresholdValue) / 2.0;

      IntervalBordersChanged.Send(m_SensibleMinimumThresholdValue, m_SensibleMaximumThresholdValue, m_IsFloatImage);
      ThresholdingValueChanged.Send(m_CurrentThresholdValue);
    }
  }
}
void mitk::BinaryThresholdULTool::CreateNewSegmentationFromThreshold(DataNode* node, const std::string& organName, const Color& color)
{
  if (node)
  {
    Image::Pointer image = dynamic_cast<Image*>( m_NodeForThresholding->GetData() );
    if (image.IsNotNull())
    {
      // create a new image of the same dimensions and smallest possible pixel type
      DataNode::Pointer emptySegmentation = Tool::CreateEmptySegmentationNode( image, organName, color );

      if (emptySegmentation)
      {
        // actually perform a thresholding and ask for an organ type
        for (unsigned int timeStep = 0; timeStep < image->GetTimeSteps(); ++timeStep)
        {
          try
          {
            ImageTimeSelector::Pointer timeSelector = ImageTimeSelector::New();
            timeSelector->SetInput( image );
            timeSelector->SetTimeNr( timeStep );
            timeSelector->UpdateLargestPossibleRegion();
            Image::Pointer image3D = timeSelector->GetOutput();

            AccessFixedDimensionByItk_2( image3D, ITKThresholding, 3, dynamic_cast<Image*>(emptySegmentation->GetData()), timeStep );
          }
          catch(...)
          {
            Tool::ErrorMessage("Error accessing single time steps of the original image. Cannot create segmentation.");
          }
        }

        //since we are maybe working on a smaller image, pad it to the size of the original image
        if (m_OriginalImageNode.GetPointer() != m_NodeForThresholding.GetPointer())
        {
          mitk::PadImageFilter::Pointer padFilter = mitk::PadImageFilter::New();

          padFilter->SetInput(0, dynamic_cast<mitk::Image*> (emptySegmentation->GetData()));
          padFilter->SetInput(1, dynamic_cast<mitk::Image*> (m_OriginalImageNode->GetData()));
          padFilter->SetBinaryFilter(true);
          padFilter->SetUpperThreshold(1);
          padFilter->SetLowerThreshold(1);
          padFilter->Update();

          emptySegmentation->SetData(padFilter->GetOutput());
        }

        if (DataStorage* ds = m_ToolManager->GetDataStorage())
        {
          ds->Add( emptySegmentation, m_OriginalImageNode );
        }

        m_ToolManager->SetWorkingData( emptySegmentation );
      }
    }
  }
}
  bool SegmentationSink::ReadyToRun()
  {
    Image::Pointer image;
    GetPointerParameter("Input", image);

    DataNode::Pointer groupNode;
    GetPointerParameter("Group node", groupNode);

    return image.IsNotNull() && groupNode.IsNotNull();
  }
  bool ShowSegmentationAsSurface::ReadyToRun()
  {
    try
    {
      Image::Pointer image;
      GetPointerParameter("Input", image);

      return image.IsNotNull() && GetGroupNode();
    }
    catch (std::invalid_argument &)
    {
      return false;
    }
  }
void mitk::BinaryThresholdULTool::SetupPreviewNode()
{
  if (m_NodeForThresholding.IsNotNull())
  {
    Image::Pointer image = dynamic_cast<Image*>( m_NodeForThresholding->GetData() );
    Image::Pointer originalImage = dynamic_cast<Image*> (m_OriginalImageNode->GetData());

    if (image.IsNotNull())
    {
      mitk::Image* workingimage = dynamic_cast<mitk::Image*>(m_ToolManager->GetWorkingData(0)->GetData());

      if(workingimage)
        m_ThresholdFeedbackNode->SetData( workingimage->Clone() );
      else
        m_ThresholdFeedbackNode->SetData( mitk::Image::New() );

      int layer(50);
      m_NodeForThresholding->GetIntProperty("layer", layer);
      m_ThresholdFeedbackNode->SetIntProperty("layer", layer+1);

      if (DataStorage* ds = m_ToolManager->GetDataStorage())
      {
        if (!ds->Exists(m_ThresholdFeedbackNode))
          ds->Add( m_ThresholdFeedbackNode, m_OriginalImageNode );
      }

      if (image.GetPointer() == originalImage.GetPointer())
      {
        m_SensibleMinimumThresholdValue = static_cast<double>( originalImage->GetScalarValueMin() );
        m_SensibleMaximumThresholdValue = static_cast<double>( originalImage->GetScalarValueMax() );
      }

      m_CurrentLowerThresholdValue = (m_SensibleMaximumThresholdValue + m_SensibleMinimumThresholdValue) / 3.0;
      m_CurrentUpperThresholdValue = 2.0 * m_CurrentLowerThresholdValue;

      IntervalBordersChanged.Send(m_SensibleMinimumThresholdValue, m_SensibleMaximumThresholdValue);
      ThresholdingValuesChanged.Send(m_CurrentLowerThresholdValue, m_CurrentUpperThresholdValue);
    }
  }
}
bool mitk::AutoCropTool::ProcessOneWorkingData( DataNode* node )
{
  if (node)
  {
    Image::Pointer image = dynamic_cast<Image*>( node->GetData() );
    if (image.IsNull()) return false;
      
//     if (image->GetDimension() == 4)
//     {
//       Tool::ErrorMessage.Send("Cropping 3D+t segmentations is not implemented. Sorry. Bug #1281");
//       return false;
//     }

    AutoCropImageFilter::Pointer cropFilter = AutoCropImageFilter::New();
    cropFilter->SetInput( image );
    cropFilter->SetBackgroundValue( 0 );
    try
    {
      cropFilter->Update();

      image = cropFilter->GetOutput();
      if (image.IsNotNull())
      {
        node->SetData( image );
      }
      else
      {
        return false;
      }
    }
    catch(...)
    {
      return false;
    }
  }

  return true;
}
void mitk::RegionGrow3DTool::SetupPreviewNodeFor( DataNode* nodeToProceed)
{
    if (nodeToProceed)
    {
        Image::Pointer image = dynamic_cast<Image*>( nodeToProceed->GetData() );

        if (image.IsNotNull())
        {
            m_FeedbackNode->SetData( image );
            int layer(50);
            nodeToProceed->GetIntProperty("layer", layer);
            m_FeedbackNode->SetIntProperty("layer", layer+1);
            m_FeedbackNode->SetLevelWindow(NULL);

            if (DataStorage* storage = m_ToolManager->GetDataStorage())
            {
                if (storage->Exists(m_FeedbackNode))
                    storage->Remove(m_FeedbackNode);
                storage->Add( m_FeedbackNode, nodeToProceed );
            }
        }
    }
    mitk::RenderingManager::GetInstance()->RequestUpdateAll();
}
void mitk::BinaryThresholdULTool::SetupPreviewNode()
{
  if (m_NodeForThresholding.IsNotNull())
  {
    Image::Pointer image = dynamic_cast<Image*>( m_NodeForThresholding->GetData() );
    Image::Pointer originalImage = dynamic_cast<Image*> (m_OriginalImageNode->GetData());

    if (image.IsNotNull())
    {
      // initialize and a new node with the same image as our reference image
      // use the level window property of this image copy to display the result of a thresholding operation
      m_ThresholdFeedbackNode->SetData( image );
      int layer(50);
      m_NodeForThresholding->GetIntProperty("layer", layer);
      m_ThresholdFeedbackNode->SetIntProperty("layer", layer+1);

      if (DataStorage* ds = m_ToolManager->GetDataStorage())
      {
        if (!ds->Exists(m_ThresholdFeedbackNode))
          ds->Add( m_ThresholdFeedbackNode, m_OriginalImageNode );
      }

      if (image.GetPointer() == originalImage.GetPointer())
      {
        m_SensibleMinimumThresholdValue = static_cast<int>( originalImage->GetScalarValueMin() );
        m_SensibleMaximumThresholdValue = static_cast<int>( originalImage->GetScalarValueMax() );
      }

      m_CurrentLowerThresholdValue = (m_SensibleMaximumThresholdValue + m_SensibleMinimumThresholdValue) / 3;
      m_CurrentUpperThresholdValue = 2*m_CurrentLowerThresholdValue;

      IntervalBordersChanged.Send(m_SensibleMinimumThresholdValue, m_SensibleMaximumThresholdValue);
      ThresholdingValuesChanged.Send(m_CurrentLowerThresholdValue, m_CurrentUpperThresholdValue);
    }
  }
}
/**Documentation
 *  Test for gibbs tracking filter
 */
int mitkGibbsTrackingTest(int argc, char* argv[])
{
  MITK_TEST_BEGIN("mitkGibbsTrackingTest");

  MITK_TEST_CONDITION_REQUIRED(argc>4,"check for input data")

  QBallImage::Pointer mitkQballImage;
  Image::Pointer mitkMaskImage;
  mitk::FiberBundleX::Pointer fib1;

  try{

    MITK_INFO << "Q-Ball image: " << argv[1];
    MITK_INFO << "Mask image: " << argv[2];
    MITK_INFO << "Parameter file: " << argv[3];
    MITK_INFO << "Reference bundle: " << argv[4];

    RegisterDiffusionCoreObjectFactory();
    RegisterFiberTrackingObjectFactory();

    const std::string s1="", s2="";
    std::vector<mitk::BaseData::Pointer> infile = mitk::BaseDataIO::LoadBaseDataFromFile( argv[1], s1, s2, false );
    mitkQballImage = dynamic_cast<mitk::QBallImage*>(infile.at(0).GetPointer());
    MITK_TEST_CONDITION_REQUIRED(mitkQballImage.IsNotNull(),"check qball image")

    infile = mitk::BaseDataIO::LoadBaseDataFromFile( argv[2], s1, s2, false );
    mitkMaskImage = dynamic_cast<mitk::Image*>(infile.at(0).GetPointer());
    MITK_TEST_CONDITION_REQUIRED(mitkMaskImage.IsNotNull(),"check mask image")

    infile = mitk::BaseDataIO::LoadBaseDataFromFile( argv[4], s1, s2, false );
    fib1 = dynamic_cast<mitk::FiberBundleX*>(infile.at(0).GetPointer());
    MITK_TEST_CONDITION_REQUIRED(fib1.IsNotNull(),"check fiber bundle")

    typedef itk::Vector<float, QBALL_ODFSIZE> OdfVectorType;
    typedef itk::Image<OdfVectorType,3> OdfVectorImgType;
    typedef itk::Image<float,3> MaskImgType;
    typedef itk::GibbsTrackingFilter<OdfVectorImgType> GibbsTrackingFilterType;

    OdfVectorImgType::Pointer itk_qbi = OdfVectorImgType::New();
    mitk::CastToItkImage<OdfVectorImgType>(mitkQballImage, itk_qbi);

    MaskImgType::Pointer itk_mask = MaskImgType::New();
    mitk::CastToItkImage<MaskImgType>(mitkMaskImage, itk_mask);

    GibbsTrackingFilterType::Pointer gibbsTracker = GibbsTrackingFilterType::New();
    gibbsTracker->SetQBallImage(itk_qbi.GetPointer());
    gibbsTracker->SetMaskImage(itk_mask);
    gibbsTracker->SetDuplicateImage(false);
    gibbsTracker->SetRandomSeed(1);
    gibbsTracker->SetLoadParameterFile(argv[3]);
    gibbsTracker->Update();

    mitk::FiberBundleX::Pointer fib2 = mitk::FiberBundleX::New(gibbsTracker->GetFiberBundle());
    MITK_TEST_CONDITION_REQUIRED(fib1->Equals(fib2), "check if gibbs tracking has changed");

    gibbsTracker->SetRandomSeed(0);
    gibbsTracker->Update();
    fib2 = mitk::FiberBundleX::New(gibbsTracker->GetFiberBundle());
    MITK_TEST_CONDITION_REQUIRED(!fib1->Equals(fib2), "check if gibbs tracking has changed after wrong seed");
  }
  catch(...)
  {
    return EXIT_FAILURE;
  }

  // always end with this!
  MITK_TEST_END();
}