static void EnsureDataImageInCollection(mitk::DataCollection::Pointer collection, std::string origin, std::string target)
{
  typedef itk::Image<double, 3> FeatureImage;

  if (collection->HasElement(origin))
  {
    mitk::Image::Pointer originImage = dynamic_cast<mitk::Image*>(collection->GetMitkImage(origin).GetPointer());
    FeatureImage::Pointer itkOriginImage = FeatureImage::New();
    mitk::CastToItkImage(originImage,itkOriginImage);

    if (!collection->HasElement(target) && itkOriginImage.IsNotNull())
    {
      FeatureImage::Pointer image = FeatureImage::New();
      image->SetRegions(itkOriginImage->GetLargestPossibleRegion());
      image->SetSpacing(itkOriginImage->GetSpacing());
      image->SetOrigin(itkOriginImage->GetOrigin());
      image->SetDirection(itkOriginImage->GetDirection());
      image->Allocate();

      collection->AddData(dynamic_cast<itk::DataObject*>(image.GetPointer()),target,"");
    }
  }
  for (std::size_t i = 0; i < collection->Size();++i)
  {
    mitk::DataCollection* newCol = dynamic_cast<mitk::DataCollection*>(collection->GetData(i).GetPointer());
    if (newCol != 0)
    {
      EnsureDataImageInCollection(newCol, origin, target);
    }
  }
}
Esempio n. 2
0
int main(int argc, char *argv[])
{
  mitkCommandLineParser parser;
  parser.setTitle("Mri Normalization");
  parser.setCategory("Preprocessing Tools");
  parser.setDescription(
    "Normalizes an MRI volume between medians of two given masks (e.g. ventricles and brain matter)");
  parser.setContributor("MBI");

  parser.setArgumentPrefix("--", "-");

  // Add command line argument names
  parser.addArgument("input", "i", mitkCommandLineParser::Image, "input image", "", us::Any(), false, false, false, mitkCommandLineParser::Input);
  parser.addArgument("output", "o", mitkCommandLineParser::File, "output image", "", us::Any(), false, false, false, mitkCommandLineParser::Output);
  parser.addArgument(
    "maxMask", "m", mitkCommandLineParser::Image, "mask of which median is set as maximal value (1)", "", us::Any(), false, false, false, mitkCommandLineParser::Input);
  parser.addArgument(
    "minMask", "l", mitkCommandLineParser::Image, "mask of which median is set as minimal value (0)", "", us::Any(), false, false, false, mitkCommandLineParser::Input);
  parser.addArgument(
    "excludeMask", "e", mitkCommandLineParser::Image, "region which is exluded from the other two", "", us::Any(), false, false, false, mitkCommandLineParser::Input);

  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);

  // Show a help message
  if (parsedArgs.size() == 0 || parsedArgs.count("help") || parsedArgs.count("h"))
  {
    std::cout << parser.helpText();
    return EXIT_SUCCESS;
  }

  std::string outFile = us::any_cast<string>(parsedArgs["output"]);
  mitk::Image::Pointer inputFile = mitk::IOUtil::Load<mitk::Image>(us::any_cast<string>(parsedArgs["input"]));
  mitk::Image::Pointer maxMask = mitk::IOUtil::Load<mitk::Image>(us::any_cast<string>(parsedArgs["maxMask"]));
  mitk::Image::Pointer minMask = mitk::IOUtil::Load<mitk::Image>(us::any_cast<string>(parsedArgs["minMask"]));
  mitk::Image::Pointer excludeMask = mitk::IOUtil::Load<mitk::Image>(us::any_cast<string>(parsedArgs["excludeMask"]));

  SeedImage::Pointer itkMaxImage = SeedImage::New();
  SeedImage::Pointer itkMinImage = SeedImage::New();
  SeedImage::Pointer itkExcludeImage = SeedImage::New();

  FeatureImage::Pointer itkInputImage = FeatureImage::New();

  mitk::CastToItkImage(inputFile, itkInputImage);
  mitk::CastToItkImage(maxMask, itkMaxImage);
  mitk::CastToItkImage(minMask, itkMinImage);
  mitk::CastToItkImage(excludeMask, itkExcludeImage);

  std::vector<mitk::ScalarType> medianMin;
  std::vector<mitk::ScalarType> medianMax;

  itk::ImageRegionIterator<FeatureImage> inputIt(itkInputImage, itkInputImage->GetLargestPossibleRegion());
  itk::ImageRegionIterator<SeedImage> excludeIt(itkExcludeImage, itkExcludeImage->GetLargestPossibleRegion());
  itk::ImageRegionIterator<SeedImage> minIt(itkMinImage, itkMinImage->GetLargestPossibleRegion());
  itk::ImageRegionIterator<SeedImage> maxIt(itkMaxImage, itkMaxImage->GetLargestPossibleRegion());

  while (!inputIt.IsAtEnd())
  {
    if (excludeIt.Get() == 0)
    {
      if (minIt.Get() != 0)
        medianMin.push_back(inputIt.Get());

      if (maxIt.Get() != 0)
        medianMax.push_back(inputIt.Get());
    }
    ++inputIt;
    ++excludeIt;
    ++minIt;
    ++maxIt;
  }

  std::sort(medianMax.begin(), medianMax.end());
  std::sort(medianMin.begin(), medianMin.end());

  mitk::ScalarType minVal = medianMin.at(medianMin.size() / 2);
  mitk::ScalarType maxVal = medianMax.at(medianMax.size() / 2);

  inputIt.GoToBegin();
  // Create mapping
  while (!inputIt.IsAtEnd())
  {
    inputIt.Set(1.0 * (inputIt.Get() - minVal) / (maxVal - minVal));
    ++inputIt;
  }

  mitk::Image::Pointer mitkResult = mitk::Image::New();
  mitkResult = mitk::GrabItkImageMemory(itkInputImage);

  mitk::IOUtil::Save(mitkResult, outFile);

  return EXIT_SUCCESS;
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
  mitkCommandLineParser parser;
  parser.setTitle("Mri Normalization");
  parser.setCategory("Preprocessing Tools");
  parser.setDescription("Normalizes an MRI volume based on regions determined by Otsu.");
  parser.setContributor("MBI");

  parser.setArgumentPrefix("--", "-");

  // Add command line argument names
  parser.addArgument("input", "i", mitkCommandLineParser::InputImage, "input image", "input image");
  parser.addArgument("output", "o", mitkCommandLineParser::OutputFile, "output image", "output image");
  parser.addArgument("bins", "b", mitkCommandLineParser::Int, "number of regions (bins)", "number of regions (bins)");

  parser.addArgument(
    "minBin", "l", mitkCommandLineParser::Int, "bin of which median is set to 0", "bin of which median is set to 0");
  parser.addArgument(
    "maxBin", "h", mitkCommandLineParser::Int, "bin of which median is set to 1", "bin of which median is set to 1");

  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);

  // Show a help message
  if (parsedArgs.size() == 0 || parsedArgs.count("help") || parsedArgs.count("h"))
  {
    std::cout << parser.helpText();
    return EXIT_SUCCESS;
  }

  std::string outFile = us::any_cast<string>(parsedArgs["output"]);
  mitk::Image::Pointer inputFile = mitk::IOUtil::Load<mitk::Image>(us::any_cast<string>(parsedArgs["input"]));
  int numberOfThresholds = us::any_cast<int>(parsedArgs["bins"]);
  int minBin = us::any_cast<int>(parsedArgs["minBin"]);
  int maxBin = us::any_cast<int>(parsedArgs["maxBin"]);

  FeatureImage::Pointer itkInputImage = FeatureImage::New();
  mitk::CastToItkImage(inputFile, itkInputImage);

  typedef itk::OtsuMultipleThresholdsImageFilter<FeatureImage, FeatureImage> FilterType;
  FilterType::Pointer otsuFilter = FilterType::New();
  otsuFilter->SetInput(itkInputImage);
  otsuFilter->SetNumberOfThresholds(numberOfThresholds - 1);
  otsuFilter->Update();

  FeatureImage::Pointer itkLabelImage = otsuFilter->GetOutput();

  std::vector<mitk::ScalarType> medianMin;
  std::vector<mitk::ScalarType> medianMax;

  itk::ImageRegionIterator<FeatureImage> inputIt(itkInputImage, itkInputImage->GetLargestPossibleRegion());
  itk::ImageRegionIterator<FeatureImage> labelIt(itkLabelImage, itkLabelImage->GetLargestPossibleRegion());

  while (!inputIt.IsAtEnd())
  {
    if (labelIt.Get() == minBin)
    {
      medianMin.push_back(inputIt.Get());
    }
    else if (labelIt.Get() == maxBin)
    {
      medianMax.push_back(inputIt.Get());
    }
    ++inputIt;
    ++labelIt;
  }

  std::sort(medianMax.begin(), medianMax.end());
  std::sort(medianMin.begin(), medianMin.end());

  mitk::ScalarType minVal = medianMin.at(medianMin.size() / 2);
  mitk::ScalarType maxVal = medianMax.at(medianMax.size() / 2);

  inputIt.GoToBegin();
  // labelIt.GoToBegin();
  // Create mapping
  while (!inputIt.IsAtEnd())
  {
    inputIt.Set(1.0 * (inputIt.Get() - minVal) / (maxVal - minVal));
    // inputIt.Set(labelIt.Get());
    ++inputIt;

    //++labelIt;
  }

  mitk::Image::Pointer mitkResult = mitk::Image::New();
  mitkResult = mitk::GrabItkImageMemory(itkInputImage);

  mitk::IOUtil::Save(mitkResult, outFile);

  return EXIT_SUCCESS;
}