ITK/Examples/ImageProcessing/RelabelComponentImageFilter: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Added Quickview)
(Deprecated content that is moved to sphinx)
 
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
==Description==
{{warning|1=The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/*  pages likely require ITK version 4.13 or earlier releasesIn many cases, the examples on this page no longer conform to the best practices for modern ITK versions.}}
Relabel the components (blobs) in an image such that consecutive labels are used. This example creates a black image with one rectangle consisting of pixels with value 100 and a second rectangle consisting of pixels with value 200. There are three resulting components (the background and the two rectangles). The relabel filter labels these three components 0, 1, and 2.  
 
==Displayed Output==
The original image and an intensity scaled version of the relabeled image.
 
==RelabelComponentImageFilter.cxx==
<source lang="cpp">
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkRelabelComponentImageFilter.h"
 
#include "QuickView.h"
 
typedef itk::Image<unsigned char, 2>  ImageType;
 
static void CreateImage(ImageType::Pointer image);
 
int main(int, char *[])
{
  ImageType::Pointer image = ImageType::New();
  CreateImage(image);
 
  itk::RelabelComponentImageFilter<ImageType, ImageType>::Pointer relabelFilter =
    itk::RelabelComponentImageFilter<ImageType, ImageType>::New();
  relabelFilter->SetInput(image);
 
  QuickView viewer;
  viewer.AddImage(
    image.GetPointer(),
    true,
    "Original");
 
  viewer.AddImage(
    relabelFilter->GetOutput(),
    true,
    "Relabeled");
 
  viewer.Visualize();
 
  return EXIT_SUCCESS;
}
 
void CreateImage(ImageType::Pointer image)
{
  // Create an image with 2 connected components
  ImageType::RegionType region;
  ImageType::IndexType start;
  start[0] = 0;
  start[1] = 0;
 
  ImageType::SizeType size;
  size[0] = 200;
  size[1] = 300;
 
  region.SetSize(size);
  region.SetIndex(start);
 
   image->SetRegions(region);
  image->Allocate();
 
  itk::ImageRegionIterator<ImageType> imageIterator(image,region);
 
  while(!imageIterator.IsAtEnd())
    {
    if(imageIterator.GetIndex()[0] > 100 &&
      imageIterator.GetIndex()[0] < 150 &&
      imageIterator.GetIndex()[1] > 100 &&
      imageIterator.GetIndex()[1] < 150)
      {
      imageIterator.Set(100);
      }
    else if(imageIterator.GetIndex()[0] > 50 &&
      imageIterator.GetIndex()[0] < 70 &&
      imageIterator.GetIndex()[1] > 50 &&
      imageIterator.GetIndex()[1] < 70)
      {
      imageIterator.Set(200);
      }
    else
      {
      imageIterator.Set(0);
      }
 
    ++imageIterator;
  }
}
</source>
 
{{ITKVTKCMakeLists|RelabelComponentImageFilter|}}

Latest revision as of 15:50, 7 June 2019

Warning: The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/* pages likely require ITK version 4.13 or earlier releases. In many cases, the examples on this page no longer conform to the best practices for modern ITK versions.