void DicomDiffusionImageReader<TPixelType, TDimension>
::GenerateData()
{
  typedef itk::ImageSeriesReader<InputImageType> ReaderType;

  typename OutputImageType::Pointer output = this->GetOutput();

  typedef typename OutputImageType::RegionType   RegionType;
  RegionType requestedRegion = output->GetRequestedRegion();

  // Each file must have the same size.
  SizeType validSize = requestedRegion.GetSize();

  int numberOfVolumes = static_cast<int>(m_Headers.size());

  // Allocate the output buffer
  output->SetBufferedRegion( requestedRegion );
  output->Allocate();

  itk::ProgressReporter progress(this, 0, 
                            m_Headers.size(),
                            m_Headers.size());

  itk::ImageRegionIterator<OutputImageType> ot (output, requestedRegion );

  // Clear the eventual previous content of the MetaDictionary array
  //if( m_MetaDataDictionaryArray.size() )
  //  {
  //  for(unsigned int i=0; i<m_MetaDataDictionaryArray.size(); i++)
  //    {
  //    // each element is a raw pointer, delete them.
  //    delete m_MetaDataDictionaryArray[i];
  //    }
  //  }
  //m_MetaDataDictionaryArray.clear();

  typename OutputImageType::PixelType vec;

  for (int i = 0; i < numberOfVolumes; i ++)
    {
    
    MITK_INFO << "Loading volume " << i+1 << "/" << numberOfVolumes;
    typename ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileNames(m_Headers[i]->m_DicomFilenames);
    reader->UpdateLargestPossibleRegion();

    if (reader->GetOutput()->GetRequestedRegion().GetSize() != validSize)
      {
      itkExceptionMacro(<< "Size mismatch!");
      }

    itk::ImageRegionConstIterator<InputImageType> it (reader->GetOutput(),
                                               reader->GetOutput()->GetLargestPossibleRegion());

    while (!it.IsAtEnd())
      {
        vec = ot.Get();
        vec.SetElement(i, it.Get());
        ot.Set(vec);
        ++it;
        ++ot;
      }
    ot = ot.Begin();
    progress.CompletedPixel();
    }
Exemplo n.º 2
0
void mitk::RegionGrowingTool::StartRegionGrowing(itk::Image<TPixel, imageDimension>* inputImage, itk::Index<imageDimension> seedIndex, std::array<ScalarType, 2> thresholds, mitk::Image::Pointer& outputImage)
{
    MITK_DEBUG << "Starting region growing at index " << seedIndex << " with lower threshold " << thresholds[0] << " and upper threshold " << thresholds[1];

    typedef itk::Image<TPixel, imageDimension> InputImageType;
    typedef itk::Image<DefaultSegmentationDataType, imageDimension> OutputImageType;

    typedef itk::ConnectedThresholdImageFilter<InputImageType, OutputImageType> RegionGrowingFilterType;
    typename RegionGrowingFilterType::Pointer regionGrower = RegionGrowingFilterType::New();

    // perform region growing in desired segmented region
    regionGrower->SetInput(inputImage);
    regionGrower->AddSeed(seedIndex);

    regionGrower->SetLower(thresholds[0]);
    regionGrower->SetUpper(thresholds[1]);

    try
    {
        regionGrower->Update();
    }
    catch(...)
    {
        return; // Should we do something?
    }

    typename OutputImageType::Pointer resultImage = regionGrower->GetOutput();

    // Smooth result: Every pixel is replaced by the majority of the neighborhood
    typedef itk::NeighborhoodIterator<OutputImageType> NeighborhoodIteratorType;
    typedef itk::ImageRegionIterator<OutputImageType> ImageIteratorType;

    typename NeighborhoodIteratorType::RadiusType radius;
    radius.Fill(2); // for now, maybe make this something the user can adjust in the preferences?

    NeighborhoodIteratorType neighborhoodIterator(radius, resultImage, resultImage->GetRequestedRegion());
    ImageIteratorType imageIterator(resultImage, resultImage->GetRequestedRegion());

    for (neighborhoodIterator.GoToBegin(), imageIterator.GoToBegin(); !neighborhoodIterator.IsAtEnd(); ++neighborhoodIterator, ++imageIterator)
    {
        DefaultSegmentationDataType voteYes(0);
        DefaultSegmentationDataType voteNo(0);

        for (unsigned int i = 0; i < neighborhoodIterator.Size(); ++i)
        {
            if (neighborhoodIterator.GetPixel(i) > 0)
            {
                voteYes += 1;
            }
            else
            {
                voteNo += 1;
            }
        }

        if (voteYes > voteNo)
        {
            imageIterator.Set(1);
        }
        else
        {
            imageIterator.Set(0);
        }
    }

    if (resultImage.IsNull())
    {
        MITK_DEBUG << "Region growing result is empty.";
    }

    // Can potentially have multiple regions, use connected component image filter to label disjunct regions
    typedef itk::ConnectedComponentImageFilter<OutputImageType, OutputImageType> ConnectedComponentImageFilterType;
    typename ConnectedComponentImageFilterType::Pointer connectedComponentFilter = ConnectedComponentImageFilterType::New();
    connectedComponentFilter->SetInput(resultImage);
    connectedComponentFilter->Update();
    typename OutputImageType::Pointer resultImageCC = connectedComponentFilter->GetOutput();
    m_ConnectedComponentValue = resultImageCC->GetPixel(seedIndex);

    outputImage = mitk::GrabItkImageMemory(resultImageCC);

}