Пример #1
0
 void
 showImage(typename TImage::Pointer image)
 {
   using std::cout;
   using std::endl;
   using std::setw;
   image->Update();
   itk::ImageRegionIterator<TImage> it(image,
       image->GetLargestPossibleRegion());
   int i = 1;
   for (it = it.Begin(); !it.IsAtEnd(); ++it, i++)
     {
       std::cout << std::setw(4) << it.Get() << " ";
       if (i % (image->GetLargestPossibleRegion().GetSize()[0]) == 0)
         {
           cout << endl;
         }
       if (i
           % (image->GetLargestPossibleRegion().GetSize()[0]
               * image->GetLargestPossibleRegion().GetSize()[1]) == 0)
         {
           cout << endl;
         }
     }
 }
Пример #2
0
  void
  CreateImage(typename TImage::Pointer image)
  {

    typename TImage::IndexType start;
    start.Fill(0);

    typename TImage::SizeType size;
    size.Fill(s);

    typename TImage::RegionType region(start, size);

    image->SetRegions(region);
    image->Allocate();

    itk::ImageRegionIterator<TImage> it(image,
        image->GetLargestPossibleRegion());
    it.GoToBegin();

    int i = 0;
    typename TImage::PixelType vv = 1;
    for (it = it.Begin(); !it.IsAtEnd(); ++it)
      {
        vv++;
        it.Set(i++ % 3 == 0 ? 0 : vv);
      }
  }
Пример #3
0
void BDSInpainting<TImage>::Inpaint(TPatchMatchFunctor* const patchMatchFunctor,
                                    TCompositor* const compositor)
{
  assert(this->Image);
  assert(this->InpaintingMask);

  ConstructValidPatchCentersImage();

  // Initialize the output with the input
  typename TImage::Pointer currentImage = TImage::New();
  ITKHelpers::DeepCopy(this->Image.GetPointer(), currentImage.GetPointer());

  // Allocate the initial NNField
  NNFieldType::Pointer nnField =
    NNFieldType::New();
  nnField->SetRegions(currentImage->GetLargestPossibleRegion());
  nnField->Allocate();

  // Initialize the NNField in the target region
  typedef SSD<TImage> PatchDistanceFunctorType;
  PatchDistanceFunctorType patchDistanceFunctor;
  patchDistanceFunctor.SetImage(currentImage);

  patchMatchFunctor->SetValidPatchCentersImage(this->ValidPatchCentersImage);

  std::vector<itk::Index<2> > pixelsToProcess = this->InpaintingMask->GetHolePixels();
  patchMatchFunctor->SetTargetPixels(pixelsToProcess);
  patchMatchFunctor->SetPatchRadius(this->PatchRadius);

  patchMatchFunctor->GetPropagationFunctor()->SetPatchDistanceFunctor(&patchDistanceFunctor);
  patchMatchFunctor->GetPropagationFunctor()->SetPatchRadius(this->PatchRadius);

  patchMatchFunctor->GetRandomSearchFunctor()->SetImage(this->Image);
  patchMatchFunctor->GetRandomSearchFunctor()->SetPatchRadius(this->PatchRadius);
  patchMatchFunctor->GetRandomSearchFunctor()->SetPatchDistanceFunctor(&patchDistanceFunctor);

  compositor->SetPatchRadius(this->PatchRadius);
  compositor->SetTargetMask(this->InpaintingMask);
  compositor->SetImage(currentImage);
  compositor->SetNearestNeighborField(patchMatchFunctor->GetNNField());

  for(unsigned int iteration = 0; iteration < this->Iterations; ++iteration)
  {
    // Run PatchMatch to compute the NNField
    patchMatchFunctor->Compute();

    std::stringstream ssNNFieldFileName;
    ssNNFieldFileName << "BDS_" << iteration << "_NNField.mha";
    PatchMatchHelpers::WriteNNField(patchMatchFunctor->GetNNField(), ssNNFieldFileName.str());

    // Update the target pixels
    compositor->Composite();
    ITKHelpers::DeepCopy(compositor->GetOutput(), currentImage.GetPointer());
  }

  ITKHelpers::DeepCopy(currentImage.GetPointer(), this->Output.GetPointer());
}
Пример #4
0
void SmoothedClassProbabilites< TImage>
::GenerateData()
{

  typename TImage::Pointer out = this->GetOutput(0);
  out->SetRegions(this->GetInput(0)->GetLargestPossibleRegion());
  out->Allocate();

  for(unsigned int i = 0 ; i < this->GetNumberOfInputs(); i++)
  {

    auto gf = itk::DiscreteGaussianImageFilter<TImage,TImage>::New();
    gf->SetInput(this->GetInput(i));
    gf->SetVariance(this->m_Sigma);
    gf->Update();

    ImageRegionConstIterator<TImage> git(gf->GetOutput(),gf->GetOutput()->GetLargestPossibleRegion());
    ImageRegionIterator<TImage> maskiter(m_MaskImage, m_MaskImage->GetLargestPossibleRegion());
    ImageRegionIterator<TImage> outputIter(out, out->GetLargestPossibleRegion());

    while (!outputIter.IsAtEnd())
    {
      if(maskiter.Value() > 0 ){

        if(git.Value() > outputIter.Value())
          outputIter.Set(i);
      }else
      {
        outputIter.Set(0);
      }

      ++git;
      ++outputIter;
      ++maskiter;
    }
  }






}
Пример #5
0
void SampleGradHistogram(double *pt, double radius, typename TImage::Pointer image, arma::ivec& samples)
{
    typedef TImage ImageType;

    int radius_pix[3]; //radius in voxels, to be calculated


    radius_pix[0] = std::ceil(radius / image->GetSpacing()[0]);
    radius_pix[1] = std::ceil(radius / image->GetSpacing()[1]);
    radius_pix[2] = std::ceil(radius / image->GetSpacing()[2]);
    
#ifdef DEBUG_MESSAGES_HOG
    std::cout << "Requested point: " << pt[0] << ", " << pt[1] << ", " << pt[2] << std::endl;
    std::cout << "Neighborhood size in mm: " << radius << std::endl;
    std::cout << "Neighborhood size in pix: " << radius_pix[0] << ", " << radius_pix[1] << ", " << radius_pix[2] << std::endl;
#endif
    
    if (radius_pix[0] == 0 || radius_pix[1] == 0 || radius_pix[2] == 0) {
        std::cout << "One of the neighborhood dimensions is zero. Please correct the radius. Aborting." << std::endl;
        return;
    }


    //transform the point from physical s1pace
    typename ImageType::PointType point;
    point[0] = pt[0];
    point[1] = pt[1];
    point[2] = pt[2];

    typename ImageType::IndexType pt_pix;
    image->TransformPhysicalPointToIndex(point, pt_pix);


    //define the region around the point of interest
    typename ImageType::IndexType rgn_idx = {
        {pt_pix[0] - radius_pix[0], pt_pix[1] - radius_pix[1], pt_pix[2] - radius_pix[2]}};
    typename ImageType::SizeType rgn_size = {
        {2 * radius_pix[0] + 1, 2 * radius_pix[1] + 1, 2 * radius_pix[2] + 1}};

    //crop the region so that it is inside
    rgn_idx[0] = std::max(rgn_idx[0], image->GetLargestPossibleRegion().GetIndex(0));
    rgn_idx[1] = std::max(rgn_idx[1], image->GetLargestPossibleRegion().GetIndex(1));
    rgn_idx[2] = std::max(rgn_idx[2], image->GetLargestPossibleRegion().GetIndex(2));

    //set it first as a corner for comparison and then undo that operation
    rgn_size[0] = std::min(rgn_size[0]+rgn_idx[0], image->GetLargestPossibleRegion().GetSize(0))-rgn_idx[0];
    rgn_size[1] = std::min(rgn_size[1]+rgn_idx[1], image->GetLargestPossibleRegion().GetSize(1))-rgn_idx[1];
    rgn_size[2] = std::min(rgn_size[2]+rgn_idx[2], image->GetLargestPossibleRegion().GetSize(2))-rgn_idx[2];

    typename ImageType::RegionType window(rgn_idx, rgn_size);

#ifdef DEBUG_MESSAGES_HOG
    std::cout << "Region: " << window << std::endl;
#endif

    samples.set_size(rgn_size[0]*rgn_size[1]*rgn_size[2]);
    samples.zeros();
    
//    itk::ImageRegionConstIterator<typename GradientFilterType::OutputImageType> imageIterator(gradient_filter->GetOutput(), window);
    itk::ImageRegionConstIterator<ImageType> imageIterator(image, window);
    imageIterator.GoToBegin();
    
    int i=0;
    while (!imageIterator.IsAtEnd()) {
        // Get the value of the current pixel
        typename ImageType::PixelType val = imageIterator.Get();
    
        //std::cout << val << std::endl;
            
        samples[i++] = val;
        
        ++imageIterator;
    }

    
       
}
void ManualPatchSelectionDialog<TImage>::slot_UpdateResult(const itk::ImageRegion<2>& sourceRegion,
                                                           const itk::ImageRegion<2>& targetRegion)
{
  assert(sourceRegion.GetSize() == targetRegion.GetSize());

  if(!this->Image->GetLargestPossibleRegion().IsInside(sourceRegion))
  {
    std::cerr << "Source region is outside the image!" << std::endl;
    return;
  }
  
  QImage qimage(sourceRegion.GetSize()[0], sourceRegion.GetSize()[1], QImage::Format_RGB888);

  if(MaskImage->CountHolePixels(sourceRegion) > 0)
  {
    //std::cerr << "The source patch must not have any hole pixels!" << std::endl;
    //btnAccept->setVisible(false);
    qimage.fill(Qt::green);
  }
  else
  {
    typename TImage::Pointer tempImage = TImage::New();
    ITKHelpers::ConvertTo3Channel(this->Image, tempImage.GetPointer());
    if(this->Image->GetNumberOfComponentsPerPixel() != 3)
      {
      ITKHelpers::ScaleAllChannelsTo255(tempImage.GetPointer());
      }

    itk::ImageRegionIterator<TImage> sourceIterator(tempImage, sourceRegion);
    itk::ImageRegionIterator<TImage> targetIterator(tempImage, targetRegion);
    itk::ImageRegionIterator<Mask> maskIterator(MaskImage, targetRegion);

    typename TImage::Pointer resultPatch = TImage::New();
    resultPatch->SetNumberOfComponentsPerPixel(Image->GetNumberOfComponentsPerPixel());
    itk::ImageRegion<2> resultPatchRegion;
    resultPatchRegion.SetSize(sourceRegion.GetSize());
    resultPatch->SetRegions(resultPatchRegion);
    resultPatch->Allocate();

    while(!maskIterator.IsAtEnd())
      {
      ITKHelpers::FloatVectorImageType::PixelType pixel;

      if(MaskImage->IsHole(maskIterator.GetIndex()))
        {
        pixel = sourceIterator.Get();
        }
      else
        {
        pixel = targetIterator.Get();
        }

      itk::Offset<2> offset = sourceIterator.GetIndex() - sourceRegion.GetIndex();
      itk::Index<2> offsetIndex;
      offsetIndex[0] = offset[0];
      offsetIndex[1] = offset[1];
      resultPatch->SetPixel(offsetIndex, pixel);

      ++sourceIterator;
      ++targetIterator;
      ++maskIterator;
      } // end iterator loop

    qimage = ITKQtHelpers::GetQImageColor(resultPatch.GetPointer(), resultPatch->GetLargestPossibleRegion());
  } // end else

  this->ResultPatchScene->clear();
  QGraphicsPixmapItem* item = this->ResultPatchScene->addPixmap(QPixmap::fromImage(qimage));
  gfxResult->fitInView(item);

}
Пример #7
0
void SampleStatistics(double *pt, double radius, typename TImage::Pointer image, arma::vec& features)
{
    features.set_size(4); //just four features for now
    features.zeros();
    
    
    
    typedef TImage ImageType;

    int radius_pix[3]; //radius in voxels, to be calculated


    radius_pix[0] = std::ceil(radius / image->GetSpacing()[0]);
    radius_pix[1] = std::ceil(radius / image->GetSpacing()[1]);
    radius_pix[2] = std::ceil(radius / image->GetSpacing()[2]);
    
#ifdef DEBUG_MESSAGES_HOG
    std::cout << "Requested point: " << pt[0] << ", " << pt[1] << ", " << pt[2] << std::endl;
    std::cout << "Neighborhood size in mm: " << radius << std::endl;
    std::cout << "Neighborhood size in pix: " << radius_pix[0] << ", " << radius_pix[1] << ", " << radius_pix[2] << std::endl;
#endif
    
    if (radius_pix[0] == 0 || radius_pix[1] == 0 || radius_pix[2] == 0) {
        std::cout << "One of the neighborhood dimensions is zero. Please correct the radius. Aborting." << std::endl;
        return;
    }


    //transform the point from physical s1pace
    typename ImageType::PointType point;
    point[0] = pt[0];
    point[1] = pt[1];
    point[2] = pt[2];

    typename ImageType::IndexType pt_pix;
    image->TransformPhysicalPointToIndex(point, pt_pix);


    //define the region around the point of interest
    typename ImageType::IndexType rgn_idx = {
        {pt_pix[0] - radius_pix[0], pt_pix[1] - radius_pix[1], pt_pix[2] - radius_pix[2]}};
    typename ImageType::SizeType rgn_size = {
        {2 * radius_pix[0] + 1, 2 * radius_pix[1] + 1, 2 * radius_pix[2] + 1}};

    //crop the region so that it is inside
    rgn_idx[0] = std::max(rgn_idx[0], image->GetLargestPossibleRegion().GetIndex(0));
    rgn_idx[1] = std::max(rgn_idx[1], image->GetLargestPossibleRegion().GetIndex(1));
    rgn_idx[2] = std::max(rgn_idx[2], image->GetLargestPossibleRegion().GetIndex(2));

    //set it first as a corner for comparison and then undo that operation
    rgn_size[0] = std::min(rgn_size[0]+rgn_idx[0], image->GetLargestPossibleRegion().GetSize(0))-rgn_idx[0];
    rgn_size[1] = std::min(rgn_size[1]+rgn_idx[1], image->GetLargestPossibleRegion().GetSize(1))-rgn_idx[1];
    rgn_size[2] = std::min(rgn_size[2]+rgn_idx[2], image->GetLargestPossibleRegion().GetSize(2))-rgn_idx[2];

    typename ImageType::RegionType window(rgn_idx, rgn_size);

#ifdef DEBUG_MESSAGES_HOG
    std::cout << "Region: " << window << std::endl;
#endif

    
    itk::ImageRegionConstIterator<ImageType> imageIterator(image, window);
    imageIterator.GoToBegin();

    const int nvoxels = rgn_size[0]*rgn_size[1]*rgn_size[2];
    arma::vec voxels;
    voxels.set_size(nvoxels);
    voxels.zeros();
    
    

    long int i=0;
    while (!imageIterator.IsAtEnd()) {
        // Get the value of the current pixel
        typename ImageType::PixelType val = imageIterator.Get();
    
        //std::cout << val << std::endl;
            

        voxels[i++] = val;
        ++imageIterator;
    }

    //do the statistics

    const double mean = arma::mean(voxels);
    const double std = arma::stddev(voxels);
    
    double E3 = 0; //Accumulate expectation of (X-u)^3
    double E4 = 0; //Accumulate expectation of (X-u)^4
    
    for(int i=0; i<voxels.size(); i++)
    {
        const double diff = voxels[i] - mean;
        const double k = diff*diff*diff/nvoxels;
        E3 += k; //cubed
        E4 += diff*k; //4th power       
    }
       
    features[0] = mean;
    features[1] = std;
    features[2] = E3/(std*std*std); //skewness
    features[3] = E3/(std*std*std*std); //kurtosis
}