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;
}
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;
}
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;
}
Esempio n. 4
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();
}
Esempio n. 5
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();
}
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;
}
Esempio n. 7
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;
}
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);
}
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);
}
Esempio n. 10
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
}
Esempio n. 11
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;
}
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);

}
 bool operator()(const itk::ImageRegion<2> region1, const itk::ImageRegion<2> region2) const
 {
   return IndexCompareFunctor(region1.GetIndex(), region2.GetIndex());
 }