void SuperPixelSegmentationGUI::slot_IterationComplete(unsigned int numberOfSegments)
{
  std::stringstream ss;
  ss << "Computed " << numberOfSegments << " segments." << std::endl;
  this->statusBar()->showMessage(ss.str().c_str());
  
  typedef itk::Image<itk::RGBPixel<unsigned char>, 2> RGBImageType;
  RGBImageType::Pointer colorImage = RGBImageType::New();
  colorImage->SetRegions(this->LabelImage->GetLargestPossibleRegion());
  colorImage->Allocate();

  unsigned int maxLabel = Helpers::MaxValue<LabelImageType>(this->LabelImage);
  std::vector<RGBImageType::PixelType> segmentColors;
  for(unsigned int labelId = 0; labelId <= maxLabel; ++labelId)
    {
    //std::cout << "Coloring label " << labelId << std::endl;
    float rgb[3] = {0,0,0};
    itk::ImageRegionIterator<LabelImageType> labelIterator(this->LabelImage, this->LabelImage->GetLargestPossibleRegion());
    unsigned int counter = 0;
    while(!labelIterator.IsAtEnd())
      {
      if(labelIterator.Get() == labelId)
        {
        rgb[0] += this->Image->GetPixel(labelIterator.GetIndex())[0];
        rgb[1] += this->Image->GetPixel(labelIterator.GetIndex())[1];
        rgb[2] += this->Image->GetPixel(labelIterator.GetIndex())[2];
        counter++;
        }// end if
      ++labelIterator;
      } // end while
    RGBImageType::PixelType colorPixel;
    colorPixel[0] = rgb[0]/static_cast<float>(counter);
    colorPixel[1] = rgb[1]/static_cast<float>(counter);
    colorPixel[2] = rgb[2]/static_cast<float>(counter);
    segmentColors.push_back(colorPixel);
    } // end for
    
        
  itk::ImageRegionIterator<LabelImageType> colorIterator(this->LabelImage, this->LabelImage->GetLargestPossibleRegion());
  
  while(!colorIterator.IsAtEnd())
    {
    colorImage->SetPixel(colorIterator.GetIndex(), segmentColors[colorIterator.Get()]);

    ++colorIterator;
    } // end while

  QImage qimage = HelpersQt::GetQImageRGB<RGBImageType>(colorImage);
  if(this->LabelImagePixmapItem)
    {
    this->Scene->removeItem(this->LabelImagePixmapItem);
    }
  this->LabelImagePixmapItem = this->Scene->addPixmap(QPixmap::fromImage(qimage));

  Refresh();
}
void LidarSegmentationWidget::on_action_Selections_SaveAsImage_triggered()
{
  QString filename = QFileDialog::getSaveFileName(this,
     "Save Image", ".", "PNG Files (*.png)");

  if(filename.isEmpty())
    {
    return;
    }

  RGBImageType::Pointer selectionsImage = RGBImageType::New();
  
  selectionsImage->SetRegions(this->ImageRegion);
  selectionsImage->Allocate();
  
  RGBPixelType whitePixel;
  whitePixel.SetRed(255);
  whitePixel.SetGreen(255);
  whitePixel.SetBlue(255);
  
  selectionsImage->FillBuffer(whitePixel);
  
  RGBPixelType greenPixel;
  greenPixel.SetRed(0);
  greenPixel.SetGreen(255);
  greenPixel.SetBlue(0);
  ITKHelpers::SetPixels(selectionsImage.GetPointer(), this->Sources, greenPixel);
  
  RGBPixelType redPixel;
  redPixel.SetRed(255);
  redPixel.SetGreen(0);
  redPixel.SetBlue(0);
  ITKHelpers::SetPixels(selectionsImage.GetPointer(), this->Sinks, redPixel);

  typedef  itk::ImageFileWriter< RGBImageType  > WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(filename.toStdString());
  writer->SetInput(selectionsImage);
  writer->Update();
}
Ejemplo n.º 3
0
bool TestCreateLuminanceImage()
{
  // From RGB image
  {
  itk::Index<2> imageCorner = {{0,0}};
  itk::Size<2> imageSize = {{100,100}};
  itk::ImageRegion<2> imageRegion(imageCorner, imageSize);

  typedef itk::Image<itk::RGBPixel<unsigned char>, 2> RGBImageType;
  RGBImageType::Pointer rgbImage = RGBImageType::New();
  rgbImage->SetRegions(imageRegion);
  rgbImage->Allocate();

  typedef itk::Image<float, 2> LuminanceImageType;
  LuminanceImageType::Pointer luminanceImage = LuminanceImageType::New();

  ITKHelpers::CreateLuminanceImage(rgbImage.GetPointer(), luminanceImage.GetPointer());
  }

  // From Vector image
  {
  itk::Index<2> imageCorner = {{0,0}};
  itk::Size<2> imageSize = {{100,100}};
  itk::ImageRegion<2> imageRegion(imageCorner, imageSize);

  typedef itk::Image<itk::CovariantVector<unsigned char, 3>, 2> VectorImageType;
  VectorImageType::Pointer vectorImage = VectorImageType::New();
  vectorImage->SetRegions(imageRegion);
  vectorImage->Allocate();

  typedef itk::Image<float, 2> LuminanceImageType;
  LuminanceImageType::Pointer luminanceImage = LuminanceImageType::New();

  ITKHelpers::CreateLuminanceImage(vectorImage.GetPointer(), luminanceImage.GetPointer());
  }

  return true;
}
void LidarSegmentationWidget::on_action_Selections_SaveBackgroundAsImage_triggered()
{
  QString filename = QFileDialog::getSaveFileName(this,
     "Save Image", "background.png", "PNG Files (*.png)");

  if(filename.isEmpty())
    {
    return;
    }

  RGBImageType::Pointer selectionsImage = RGBImageType::New();

  selectionsImage->SetRegions(this->ImageRegion);
  selectionsImage->Allocate();

  RGBPixelType blackPixel;
  blackPixel.SetRed(0);
  blackPixel.SetGreen(0);
  blackPixel.SetBlue(0);

  RGBPixelType whitePixel;
  whitePixel.SetRed(255);
  whitePixel.SetGreen(255);
  whitePixel.SetBlue(255);

  ITKHelpers::SetPixelsInRegionToValue(selectionsImage.GetPointer(), selectionsImage->GetLargestPossibleRegion(),
                                    blackPixel);

  ITKHelpers::SetPixels(selectionsImage.GetPointer(), this->Sinks, whitePixel);

  typedef  itk::ImageFileWriter< RGBImageType  > WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(filename.toStdString());
  writer->SetInput(selectionsImage);
  writer->Update();
}
int main(int argc, char *argv[])
{
  if(argc != 3)
    {
    std::cerr << "Required arguments: image mask" << std::endl;
    return EXIT_FAILURE;
    }
  std::string imageFilename = argv[1];
  std::string maskFilename = argv[2];
  std::cout << "Reading image: " << imageFilename << std::endl;
  std::cout << "Reading mask: " << maskFilename << std::endl;

  typedef itk::ImageFileReader<FloatVectorImageType> ImageReaderType;
  ImageReaderType::Pointer imageReader = ImageReaderType::New();
  imageReader->SetFileName(imageFilename.c_str());
  imageReader->Update();

  std::cout << "Read image " << imageReader->GetOutput()->GetLargestPossibleRegion() << std::endl;

  typedef itk::ImageFileReader<Mask> MaskReaderType;
  MaskReaderType::Pointer maskReader = MaskReaderType::New();
  maskReader->SetFileName(maskFilename.c_str());
  maskReader->Update();

  std::cout << "Read mask " << maskReader->GetOutput()->GetLargestPossibleRegion() << std::endl;

  // Prepare image
  RGBImageType::Pointer rgbImage = RGBImageType::New();
  // TODO: update this to the new API
  //Helpers::VectorImageToRGBImage(imageReader->GetOutput(), rgbImage);

  OutputHelpers::WriteImage(rgbImage.GetPointer(), "Test/TestIsophotes.rgb.mha");

  typedef itk::RGBToLuminanceImageFilter< RGBImageType, FloatScalarImageType > LuminanceFilterType;
  LuminanceFilterType::Pointer luminanceFilter = LuminanceFilterType::New();
  luminanceFilter->SetInput(rgbImage);
  luminanceFilter->Update();

  FloatScalarImageType::Pointer blurredLuminance = FloatScalarImageType::New();
  // Blur with a Gaussian kernel
  unsigned int kernelRadius = 5;
  MaskOperations::MaskedBlur<FloatScalarImageType>(luminanceFilter->GetOutput(), maskReader->GetOutput(), kernelRadius,
                                            blurredLuminance);

  OutputHelpers::WriteImage<FloatScalarImageType>(blurredLuminance, "Test/TestIsophotes.blurred.mha");

  //inpainting.ComputeMaskedIsophotes(blurredLuminance, maskReader->GetOutput());

  //Helpers::WriteImage<FloatVector2ImageType>(inpainting.GetIsophoteImage(), );
  //HelpersOutput::Write2DVectorImage(inpainting.GetIsophoteImage(), "Test/TestIsophotes.isophotes.mha");

  itk::Size<2> size;
  size.Fill(21);

  // Target
  itk::Index<2> targetIndex;
  targetIndex[0] = 187;
  targetIndex[1] = 118;
  itk::ImageRegion<2> targetRegion(targetIndex, size);

  // Source
  itk::Index<2> sourceIndex;
  sourceIndex[0] = 176;
  sourceIndex[1] = 118;
  itk::ImageRegion<2> sourceRegion(sourceIndex, size);

  //PatchPair patchPair(Patch(sourceRegion), Patch(targetRegion));
  //PatchPair patchPair;
//   Patch sourcePatch(sourceRegion);
//   Patch targetPatch(targetRegion);
//   PatchPair patchPair(sourcePatch, targetPatch);

  //inpainting.FindBoundary();

//   std::vector<itk::Index<2> > borderPixels =
//     ITKHelpers::GetNonZeroPixels(inpainting.GetBoundaryImage(), targetRegion);

  itk::RGBPixel<unsigned char> black;
  black.SetRed(0);
  black.SetGreen(0);
  black.SetBlue(0);

  itk::RGBPixel<unsigned char> red;
  red.SetRed(255);
  red.SetGreen(0);
  red.SetBlue(0);

  itk::RGBPixel<unsigned char> darkRed;
  darkRed.SetRed(100);
  darkRed.SetGreen(0);
  darkRed.SetBlue(0);

  itk::RGBPixel<unsigned char> yellow;
  yellow.SetRed(255);
  yellow.SetGreen(255);
  yellow.SetBlue(0);

  itk::RGBPixel<unsigned char> green;
  green.SetRed(0);
  green.SetGreen(255);
  green.SetBlue(0);

  itk::RGBPixel<unsigned char> darkGreen;
  darkGreen.SetRed(0);
  darkGreen.SetGreen(100);
  darkGreen.SetBlue(0);

  itk::RGBPixel<unsigned char> blue;
  blue.SetRed(0);
  blue.SetGreen(0);
  blue.SetBlue(255);

  RGBImageType::Pointer output = RGBImageType::New();
  output->SetRegions(imageReader->GetOutput()->GetLargestPossibleRegion());
  output->Allocate();
  output->FillBuffer(black);

  ITKHelpers::BlankAndOutlineRegion(output.GetPointer(), targetRegion, black, red);
  ITKHelpers::BlankAndOutlineRegion(output.GetPointer(), sourceRegion, black, green);

  RGBImageType::Pointer target = RGBImageType::New();
  target->SetRegions(imageReader->GetOutput()->GetLargestPossibleRegion());
  target->Allocate();
  ITKHelpers::BlankAndOutlineRegion(target.GetPointer(), targetRegion, black, red);

  RGBImageType::Pointer source = RGBImageType::New();
  source->SetRegions(imageReader->GetOutput()->GetLargestPossibleRegion());
  source->Allocate();
  ITKHelpers::BlankAndOutlineRegion(source.GetPointer(), sourceRegion, black, green);

  // itk::Offset<2> offset = targetIndex - sourceIndex;
  /*
  for(unsigned int pixelId = 0; pixelId < borderPixels.size(); ++pixelId)
    {
    itk::Index<2> targetPatchSourceSideBoundaryPixel = borderPixels[pixelId];
    itk::Index<2> sourcePatchTargetSideBoundaryPixel;
    //bool valid = GetAdjacentBoundaryPixel(currentPixel, candidatePairs[sourcePatchId], adjacentBoundaryPixel);
    bool valid = inpainting.GetAdjacentBoundaryPixel(targetPatchSourceSideBoundaryPixel, patchPair, sourcePatchTargetSideBoundaryPixel);

    target->SetPixel(targetPatchSourceSideBoundaryPixel, darkRed);
    source->SetPixel(sourcePatchTargetSideBoundaryPixel, darkGreen);

    if(!valid)
      {
      continue;
      }

    // Bring the adjacent pixel back to the target region.
    itk::Index<2> targetPatchTargetSideBoundaryPixel = sourcePatchTargetSideBoundaryPixel + offset;

    output->SetPixel(targetPatchSourceSideBoundaryPixel, darkRed);

    output->SetPixel(targetPatchTargetSideBoundaryPixel, blue);
    output->SetPixel(sourcePatchTargetSideBoundaryPixel, darkGreen);
    }
  */

//   OutputHelpers::WriteImage(output.GetPointer(), "Test/FollowIsophotes.Output.mha");
//   OutputHelpers::WriteImage(target.GetPointer(), "Test/FollowIsophotes.Target.mha");
//   OutputHelpers::WriteImage(source.GetPointer(), "Test/FollowIsophotes.Source.mha");

  return EXIT_SUCCESS;
}