Beispiel #1
0
void Mask::KeepLargestHole()
{
  throw std::runtime_error("Mask::KeepLargestHole() needs to be tested!");

  typedef itk::Image<unsigned int> LabelImageType;
  // Only keep the largest segment
  typedef itk::ConnectedComponentImageFilter<Mask, LabelImageType>
      ConnectedComponentImageFilterType;
  ConnectedComponentImageFilterType::Pointer connectedComponentFilter =
      ConnectedComponentImageFilterType::New();
  connectedComponentFilter->SetInput(this);
  connectedComponentFilter->Update();

  WriteImage(connectedComponentFilter->GetOutput(), "ConnectedComponents.mha");

  typedef itk::LabelShapeKeepNObjectsImageFilter<LabelImageType>
      LabelShapeKeepNObjectsImageFilterType;
  LabelShapeKeepNObjectsImageFilterType::Pointer labelShapeKeepNObjectsImageFilter =
           LabelShapeKeepNObjectsImageFilterType::New();
  labelShapeKeepNObjectsImageFilter->SetInput(connectedComponentFilter->GetOutput());
  labelShapeKeepNObjectsImageFilter->SetBackgroundValue(0); // TODO: why zero?
  labelShapeKeepNObjectsImageFilter->SetNumberOfObjects(1);
  labelShapeKeepNObjectsImageFilter
            ->SetAttribute(LabelShapeKeepNObjectsImageFilterType::LabelObjectType::NUMBER_OF_PIXELS);
  labelShapeKeepNObjectsImageFilter->Update();

  WriteImage(labelShapeKeepNObjectsImageFilter->GetOutput(),
                         "LargestComponent.mha");

  // TODO: replace whatever value is the foreground (by looking at the output above)
  // with HOLE
//  DeepCopy(rescaleFilter->GetOutput(), this);
}
Beispiel #2
0
Matrix<int> getCCLabeling(Matrix<int> p, bool recip){

    Matrix<int> m = Matrix<int>(p.getXdim(), p.getYdim(), 1);
    if(recip){
        for(int i=0; i<p.getXdim(); i++)
            for(int j=0; j<p.getYdim(); j++)
                if(p(i,j) == 1)
                    m(i,j) = 0;
    }else{
        for(int i=0; i<p.getXdim(); i++)
            for(int j=0; j<p.getYdim(); j++)
                m(i,j) = p(i,j);
    }


    const unsigned int Dimension = 2;
    typedef unsigned char                       PixelType;
    typedef itk::RGBPixel<unsigned char>         RGBPixelType;
    typedef itk::Image<PixelType, 2>     ImageType;
    typedef itk::Image<RGBPixelType, 2>  RGBImageType;

    //fill ITK image type
    ImageType::Pointer image = ImageType::New();
    typename ImageType::IndexType start;// = {{0,0}};//TImage::IndexType start = {{0,0}};
    start[0] = 0;
    start[1] = 0;

    typename ImageType::SizeType size;
    unsigned int NumRows = m.getXdim();
    unsigned int NumCols = m.getYdim();
    size[0] = NumRows;
    size[1] = NumCols;

    ImageType::RegionType region;// region(start, size);
    region.SetSize(size);
    region.SetIndex(start);
    //region.SetSize(size);

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

    //cout << "here" << endl;

    for(typename ImageType::IndexValueType r = 0; r < m.getXdim(); r++)
      {
        for(typename ImageType::IndexValueType c = 0; c < m.getYdim(); c++)
        {
        typename ImageType::IndexType pixelIndex = {{r,c}};
          if(m(r,c) == 1)
              image->SetPixel(pixelIndex, 255);
          else
              image->SetPixel(pixelIndex, 0);
        //image->SetPixel(pixelIndex, 255);

        }
      }

    //cout << "got here" << endl;
    typedef itk::Image< unsigned short, Dimension > OutputImageType;

      typedef itk::ConnectedComponentImageFilter <ImageType, OutputImageType >
        ConnectedComponentImageFilterType;

      ConnectedComponentImageFilterType::Pointer connected =
        ConnectedComponentImageFilterType::New ();
      connected->SetInput(image);
      connected->Update();

      //cout << "got CC" << endl;

      //std::cout << "Number of objects: " << connected->GetObjectCount() << std::endl;

      Matrix<int> out = Matrix<int>(m.getXdim(), m.getYdim(), 0);

      //connected->SetMaskImage(image);
      //connected->GetOutput();

      for(typename ImageType::IndexValueType r = 0; r < m.getXdim(); r++)
        {
          for(typename ImageType::IndexValueType c = 0; c < m.getYdim(); c++)
          {
          typename ImageType::IndexType pixelIndex = {{r,c}};
              out(r,c) =  connected->GetOutput()->GetPixel(pixelIndex);
                //out(r,c) = image->GetPixel(pixelIndex);
          }
        }
    return out;
}