typename MaskedHistogramGenerator<TBinValue, TQuadrantProperties>::HistogramType MaskedHistogramGenerator<TBinValue, TQuadrantProperties>::ComputeMaskedScalarImageHistogram
    (const TImage* const image, const itk::ImageRegion<2>& imageRegion,
     const Mask* const mask, const itk::ImageRegion<2>& maskRegion, const unsigned int numberOfBins,
     const TRangeValue& rangeMin, const TRangeValue& rangeMax, const bool allowOutside, const HoleMaskPixelTypeEnum& maskValue)
{
  std::vector<itk::Index<2> > maskIndices = ITKHelpers::GetPixelsWithValueInRegion(mask, maskRegion, maskValue);

  // Compute the corresponding locations in the imageRegion
  std::vector<itk::Index<2> > imageIndices(maskIndices.size());
  itk::Offset<2> maskRegionToImageRegionOffset = imageRegion.GetIndex() - maskRegion.GetIndex(); // The offset from the maskRegion to the imageRegion
  for(size_t i = 0; i < maskIndices.size(); ++i)
  {
    imageIndices[i] = maskIndices[i] + maskRegionToImageRegionOffset;
  }

  HistogramType histogram;

  // Get this channels masked scalar values
  std::vector<typename TImage::PixelType> validPixels =
      ITKHelpers::GetPixelValues(image, imageIndices);

  // Compute the histogram of the scalar values
  if(allowOutside)
  {
    histogram = HistogramGeneratorType::ScalarHistogramAllowOutside(validPixels, numberOfBins, rangeMin, rangeMax);
  }
  else
  {
    histogram = HistogramGeneratorType::ScalarHistogram(validPixels, numberOfBins, rangeMin, rangeMax);
  }

  return histogram;
}
Ejemplo n.º 2
0
void Mask::ApplyRegionToImageRegion(const itk::ImageRegion<2>& maskRegion, TImage* const image,
                                    const itk::ImageRegion<2>& imageRegion, const typename TImage::PixelType& color) const
{
  if(maskRegion.GetSize() != imageRegion.GetSize())
    {
    std::cerr << "imageRegion and maskRegion must be the same size!" << std::endl
              << "Image region: " << imageRegion << std::endl
              << "Mask region: " << maskRegion << std::endl;
    return;
    }

  itk::ImageRegionConstIterator<Mask> maskIterator(this, maskRegion);
  itk::ImageRegionIterator<TImage> imageIterator(image, imageRegion);

  while(!maskIterator.IsAtEnd())
  {
    if(this->IsHole(maskIterator.GetIndex()))
    {
      imageIterator.Set(color);
    }

    ++maskIterator;
    ++imageIterator;
  }
}
Ejemplo n.º 3
0
itk::Index<2> GetRandomPixelInRegion(const itk::ImageRegion<2>& region)
{
    itk::Index<2> pixel;
    pixel[0] = region.GetIndex()[0] + Helpers::RandomInt(0, region.GetSize()[0] - 1);
    pixel[1] = region.GetIndex()[1] + Helpers::RandomInt(0, region.GetSize()[1] - 1);

    return pixel;
}
Ejemplo n.º 4
0
itk::Index<2> GetRegionCenter(const itk::ImageRegion<2>& region)
{
  // This assumes that the region is an odd size in both dimensions
  itk::Index<2> center;
  center[0] = region.GetIndex()[0] + region.GetSize()[0] / 2;
  center[1] = region.GetIndex()[1] + region.GetSize()[1] / 2;

  return center;
}
typename MaskedHistogramGenerator<TBinValue, TQuadrantProperties>::HistogramType MaskedHistogramGenerator<TBinValue, TQuadrantProperties>::ComputeMaskedImage1DHistogram(
                const itk::VectorImage<TComponent, 2>* image,
                const itk::ImageRegion<2>& imageRegion,
                const Mask* const mask,
                const itk::ImageRegion<2>& maskRegion,
                const unsigned int numberOfBinsPerDimension,
                const TRangeContainer& rangeMins,
                const TRangeContainer& rangeMaxs, const bool allowOutside, const HoleMaskPixelTypeEnum& maskValue)
{
  // For VectorImage, we must use VectorImageToImageAdaptor

  typedef itk::VectorImage<TComponent, 2> ImageType;
  typedef itk::Image<TComponent, 2> ScalarImageType;

  std::vector<itk::Index<2> > maskIndices = ITKHelpers::GetPixelsWithValueInRegion(mask, maskRegion, maskValue);

  // Compute the corresponding locations in the imageRegion
  std::vector<itk::Index<2> > imageIndices(maskIndices.size());
  itk::Offset<2> maskRegionToImageRegionOffset = imageRegion.GetIndex() - maskRegion.GetIndex(); // The offset from the maskRegion to the imageRegion
  for(size_t i = 0; i < maskIndices.size(); ++i)
  {
    imageIndices[i] = maskIndices[i] + maskRegionToImageRegionOffset;
  }

  HistogramType concatenatedHistograms;

  for(unsigned int channel = 0; channel < image->GetNumberOfComponentsPerPixel(); ++channel)
  {
    // Extract the channel

    typedef itk::VectorImageToImageAdaptor<TComponent, 2> ImageAdaptorType;
    typename ImageAdaptorType::Pointer adaptor = ImageAdaptorType::New();
    adaptor->SetExtractComponentIndex(channel);
    adaptor->SetImage(const_cast<ImageType*>(image));

    // Get this channels masked scalar values
    std::vector<typename ScalarImageType::PixelType> validPixels =
        ITKHelpers::GetPixelValues(adaptor.GetPointer(), imageIndices);

    // Compute the histogram of the scalar values
    HistogramType histogram;
    if(allowOutside)
    {
      histogram = HistogramGeneratorType::ScalarHistogramAllowOutside(validPixels, numberOfBinsPerDimension,
                                                                      rangeMins[channel], rangeMaxs[channel]);
    }
    else
    {
      histogram = HistogramGeneratorType::ScalarHistogram(validPixels, numberOfBinsPerDimension,
                                                          rangeMins[channel], rangeMaxs[channel]);
    }
    concatenatedHistograms.insert(concatenatedHistograms.end(), histogram.begin(), histogram.end());
  }

  return concatenatedHistograms;
}
Ejemplo n.º 6
0
itk::ImageRegion<2> GetRandomRegionInRegion(const itk::ImageRegion<2>& region, const unsigned int patchRadius)
{
    itk::Index<2> randomPixel;
    randomPixel[0] = Helpers::RandomInt(region.GetIndex()[0], region.GetIndex()[0] + region.GetSize()[0]);
    randomPixel[1] = Helpers::RandomInt(region.GetIndex()[1], region.GetIndex()[1] + region.GetSize()[1]);

    itk::ImageRegion<2> randomRegion = ITKHelpers::GetRegionInRadiusAroundPixel(randomPixel, patchRadius);

    return randomRegion;
}
Ejemplo n.º 7
0
std::vector<itk::Offset<2> > Mask::GetHoleOffsetsInRegion(itk::ImageRegion<2> region) const
{
  // Ensure the region is inside the image
  region.Crop(this->GetLargestPossibleRegion());

  std::vector<itk::Index<2> > indices =
      GetPixelsWithValueInRegion(this, region, HoleMaskPixelTypeEnum::HOLE);

  std::vector<itk::Offset<2> > holeOffsets =
      IndicesToOffsets(indices, region.GetIndex());

  return holeOffsets;
}
Ejemplo n.º 8
0
void InitializeVTKImage(const itk::ImageRegion<2>& region, const unsigned int channels, vtkImageData* outputImage)
{
  // Setup and allocate the VTK image
  outputImage->SetDimensions(region.GetSize()[0],
                             region.GetSize()[1],
                             1);
  outputImage->AllocateScalars(VTK_UNSIGNED_CHAR, channels);
}
float TruncatedQuadraticDifference<TImage>::Difference
(const TImage* const image, const float truncationPoint, const itk::ImageRegion<2>& region1, const itk::ImageRegion<2>& region2)
{
  assert(region1.GetSize() == region2.GetSize());
  assert(image->GetLargestPossibleRegion().IsInside(region1));
  assert(image->GetLargestPossibleRegion().IsInside(region2));

  itk::ImageRegionConstIterator<TImage> patch1Iterator(image, region1);
  itk::ImageRegionConstIterator<TImage> patch2Iterator(image, region2);

  float sumSquaredDifferences = 0;

  typename TImage::PixelType pixel1;
  typename TImage::PixelType pixel2;

  while(!patch1Iterator.IsAtEnd())
    {
    pixel1 = patch1Iterator.Get();
    pixel2 = patch2Iterator.Get();

    float squaredDifference = PixelDifferences::SumOfSquaredDifferences(pixel1, pixel2);

//       std::cout << "Source pixel: " << static_cast<unsigned int>(sourcePixel)
//                 << " target pixel: " << static_cast<unsigned int>(targetPixel)
//                 << "Difference: " << difference << " squaredDifference: " << squaredDifference << std::endl;

    // Perform the truncation
    if(sqrt(squaredDifference) > truncationPoint)
    {
      squaredDifference = truncationPoint*truncationPoint;
    }

    sumSquaredDifferences += squaredDifference;

    ++patch1Iterator;
    ++patch2Iterator;
    } // end while iterate over sourcePatch


  unsigned int numberOfPixels = region1.GetNumberOfPixels();

  float averageSSD = sumSquaredDifferences / static_cast<float>(numberOfPixels);

  return averageSSD;
}
void PatchHighlighter::SetRegion(const itk::ImageRegion<2>& region)
{
  // Snap user patch to integer pixels
  double position[3];
  position[0] = region.GetIndex()[0];
  position[1] = region.GetIndex()[1];
  position[2] = 0;

  this->PatchLayer.ImageSlice->SetPosition(position);
}
Ejemplo n.º 11
0
void SetRegionCenterPixel(vtkImageData* const image, const itk::ImageRegion<2>& region, const unsigned char color[3])
{
  int dims[3];
  image->GetDimensions(dims);

  unsigned char* pixel = static_cast<unsigned char*>(image->GetScalarPointer(region.GetIndex()[0] + region.GetSize()[0]/2,
                                                                             region.GetIndex()[1] + region.GetSize()[1]/2, 0));
  pixel[0] = color[0];
  pixel[1] = color[1];
  pixel[2] = color[2];
  pixel[3] = 255; // visible
}
Ejemplo n.º 12
0
void LocalPCADistance<TImage>::ComputeProjectionMatrix(const itk::ImageRegion<2>& region)
{
  assert(this->Image);

  // Dilate the query region
  itk::Index<2> dilatedRegionCorner = {{region.GetIndex()[0] - 1, region.GetIndex()[1] - 1}};
  itk::Size<2> dilatedRegionSize = {{region.GetSize()[0] + 2, region.GetSize()[1] + 2}};
  itk::ImageRegion<2> dilatedRegion(dilatedRegionCorner, dilatedRegionSize);
  dilatedRegion.Crop(this->Image->GetLargestPossibleRegion());

  // Compute a local feature matrix (the query patch and all of it's neighbors (if they are inside the image)
  MatrixType featureMatrix = PatchProjection<Eigen::MatrixXf, Eigen::VectorXf>::
                               VectorizeImage(this->Image, region.GetSize()[0]/2, dilatedRegion);

  unsigned int inputPoints = featureMatrix.cols();

  this->ProjectionMatrix = PatchProjection<MatrixType, VectorType>::ProjectionMatrixFromFeatureMatrix(featureMatrix, this->MeanVector);

  // Only keep the number of columns corresponding to the number of input points used
  this->ProjectionMatrix = EigenHelpers::TruncateColumns<MatrixType>(this->ProjectionMatrix, inputPoints);
}
Ejemplo n.º 13
0
std::vector<itk::Index<2> > Mask::GetValidPixelsInRegion(itk::ImageRegion<2> region,
                                                         const bool forward) const
{
  // Ensure the region is inside the image
  region.Crop(this->GetLargestPossibleRegion());

  std::vector<itk::Index<2> > validPixels =
      GetPixelsWithValueInRegion(this, region, HoleMaskPixelTypeEnum::VALID);

  if(!forward)
  {
    std::reverse(validPixels.begin( ), validPixels.end( ) );
  }

  return validPixels;
}
void BasicViewerWidget<TImage>::slot_UpdateSource(const itk::ImageRegion<2>& sourceRegion)
{
//  std::cout << "BasicViewerWidget::slot_UpdateSource " << sourceRegion << std::endl;

  if(!this->SourceHighlighter)
  {
    this->SourceHighlighter = new PatchHighlighter(sourceRegion.GetSize()[0]/2, this->Renderer, Qt::green);
  }
  this->SourceHighlighter->SetRegion(sourceRegion);

  QImage sourceImage = ITKQtHelpers::GetQImageColor(this->Image.GetPointer(), sourceRegion);
  QGraphicsPixmapItem* item = this->SourcePatchScene->addPixmap(QPixmap::fromImage(sourceImage));
  gfxSource->fitInView(item);

  // Make the renderer show the Highlighter in the new position
  this->qvtkWidget->GetRenderWindow()->Render();
}
Ejemplo n.º 15
0
void BlankRegion(vtkImageData* image, const itk::ImageRegion<2>& region)
{
  // Blank the image
  for(unsigned int i = region.GetIndex()[0]; i < region.GetIndex()[0] + region.GetSize()[0]; ++i)
    {
    for(unsigned int j = region.GetIndex()[1]; j < region.GetIndex()[1] + region.GetSize()[1]; ++j)
      {
      unsigned char* pixel = static_cast<unsigned char*>(image->GetScalarPointer(i, region.GetIndex()[1], 0));
      pixel[0] = 0;
      pixel[1] = 0;
      pixel[2] = 0;
      pixel[3] = 0; // transparent
      }
    }
  image->Modified();
}
void BasicViewerWidget<TImage>::slot_UpdateTarget(const itk::ImageRegion<2>& targetRegion)
{
//  std::cout << "BasicViewerWidget:slot_UpdateTarget " << targetRegion << std::endl;

  if(!this->TargetHighlighter)
  {
    this->TargetHighlighter = new PatchHighlighter(targetRegion.GetSize()[0]/2, this->Renderer, Qt::red);
  }

  this->TargetHighlighter->SetRegion(targetRegion);

  // Target patch
  QImage targetImage = ITKQtHelpers::GetQImageColor(this->Image.GetPointer(), targetRegion);
  QGraphicsPixmapItem* item = this->TargetPatchScene->addPixmap(QPixmap::fromImage(targetImage));
  gfxTarget->fitInView(item);

  // Make the renderer show the Highlighter in the new position
  this->qvtkWidget->GetRenderWindow()->Render();
}
 bool operator()(const itk::ImageRegion<2> region1, const itk::ImageRegion<2> region2) const
 {
   return IndexCompareFunctor(region1.GetIndex(), region2.GetIndex());
 }
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);

}
Ejemplo n.º 19
0
void OutlineRegion(vtkImageData* const image, const itk::ImageRegion<2>& region, const unsigned char color[3])
{
//   std::cout << "Outlining region: " << region << std::endl;
//    std::cout << "Outline color: " << static_cast<int>(color[0])
//              << " " << static_cast<int>(color[1]) << " "
//              << static_cast<int>(color[2]) << std::endl;
//   std::cout << "Image components: " << image->GetNumberOfScalarComponents() << std::endl;

  unsigned int leftEdge = region.GetIndex()[0];
  //std::cout << "leftEdge : " << leftEdge << std::endl;

  unsigned int rightEdge = region.GetIndex()[0] + region.GetSize()[0] - 1;
  //std::cout << "rightEdge : " << rightEdge << std::endl;

  unsigned int topEdge = region.GetIndex()[1];
  //std::cout << "topEdge : " << topEdge << std::endl;

  unsigned int bottomEdge = region.GetIndex()[1] + region.GetSize()[1] - 1;
  //std::cout << "bottomEdge : " << bottomEdge << std::endl;

  // Move along the top and bottom of the region, setting the border pixels.
  unsigned int counter = 0;
  for(unsigned int xpos = leftEdge; xpos <= rightEdge; ++xpos)
    {
    //std::cout << "xpos : " << xpos << std::endl;
    unsigned char* topPixel = static_cast<unsigned char*>(image->GetScalarPointer(xpos, topEdge, 0));
    topPixel[0] = color[0];
    topPixel[1] = color[1];
    topPixel[2] = color[2];
    topPixel[3] = 255; // visible

    unsigned char* bottomPixel = static_cast<unsigned char*>(image->GetScalarPointer(xpos, bottomEdge, 0));
    bottomPixel[0] = color[0];
    bottomPixel[1] = color[1];
    bottomPixel[2] = color[2];
    bottomPixel[3] = 255; // visible

    counter++;
    }

  //std::cout << "Set " << counter << " pixels." << std::endl;
  // Move along the left and right of the region, setting the border pixels.
  for(unsigned int j = region.GetIndex()[1]; j < region.GetIndex()[1] + region.GetSize()[1]; ++j)
    {
    unsigned char* pixel = static_cast<unsigned char*>(image->GetScalarPointer(region.GetIndex()[0], j, 0));
    pixel[0] = color[0];
    pixel[1] = color[1];
    pixel[2] = color[2];
    pixel[3] = 255; // visible

    pixel = static_cast<unsigned char*>(image->GetScalarPointer(region.GetIndex()[0] + region.GetSize()[0] - 1, j, 0));
    pixel[0] = color[0];
    pixel[1] = color[1];
    pixel[2] = color[2];
    pixel[3] = 255; // visible
    }

  image->Modified();
}