ITK  5.4.0
Insight Toolkit
SphinxExamples/src/Segmentation/ConnectedComponents/AssignContiguousLabelsToConnectedRegions/Code.cxx
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkImage.h"
#include "itkRGBPixel.h"
#ifdef ENABLE_QUICKVIEW
# include "QuickView.h"
#endif
using RGBPixelType = itk::RGBPixel<unsigned char>;
using RGBImageType = itk::Image<RGBPixelType, 2>;
using ImageType = itk::Image<unsigned char, 2>;
static void
CreateImage(ImageType::Pointer image);
static void
CreateRandomColormap(unsigned int size, ColormapType::Pointer colormap);
int
main()
{
auto image = ImageType::New();
CreateImage(image);
auto relabelFilter = FilterType::New();
relabelFilter->SetInput(image);
auto colormapFilter1 = ColormapFilterType::New();
auto largeColormap = ColormapType::New();
CreateRandomColormap(255, largeColormap);
colormapFilter1->SetInput(image);
colormapFilter1->SetColormap(largeColormap);
auto colormapFilter2 = ColormapFilterType::New();
colormapFilter2->SetInput(relabelFilter->GetOutput());
colormapFilter2->SetColormap(largeColormap);
#ifdef ENABLE_QUICKVIEW
QuickView viewer;
viewer.AddRGBImage(colormapFilter1->GetOutput(), true, "Original");
viewer.AddRGBImage(colormapFilter2->GetOutput(), true, "Relabeled");
viewer.Visualize();
#endif
return EXIT_SUCCESS;
}
void
CreateImage(ImageType::Pointer image)
{
// Create an image with 2 connected components
start[0] = 0;
start[1] = 0;
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(200);
}
else if (imageIterator.GetIndex()[0] > 50 && imageIterator.GetIndex()[0] < 70 && imageIterator.GetIndex()[1] > 50 &&
imageIterator.GetIndex()[1] < 70)
{
imageIterator.Set(100);
}
else
{
imageIterator.Set(0);
}
++imageIterator;
}
}
void
CreateRandomColormap(unsigned int size, ColormapType::Pointer colormap)
{
ColormapType::ChannelType redChannel;
ColormapType::ChannelType greenChannel;
ColormapType::ChannelType blueChannel;
random->SetSeed(8775070);
for (unsigned int i = 0; i < size; ++i)
{
redChannel.push_back(static_cast<ColormapType::RealType>(random->GetUniformVariate(.3, 1.0)));
greenChannel.push_back(static_cast<ColormapType::RealType>(random->GetUniformVariate(.3, 1.0)));
blueChannel.push_back(static_cast<ColormapType::RealType>(random->GetUniformVariate(.3, 1.0)));
}
colormap->SetRedChannel(redChannel);
colormap->SetGreenChannel(greenChannel);
colormap->SetBlueChannel(blueChannel);
}
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
itk::RGBPixel
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
itkRGBPixel.h
itk::ScalarToRGBColormapImageFilter
Implements pixel-wise intensity->rgb mapping operation on one image.
Definition: itkScalarToRGBColormapImageFilter.h:130
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itkImage.h
itk::SmartPointer< Self >
itkRelabelComponentImageFilter.h
itk::ImageRegionIterator
A multi-dimensional iterator templated over image type that walks a region of pixels.
Definition: itkImageRegionIterator.h:80
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
QuickView::AddRGBImage
void AddRGBImage(TImage *, bool FlipVertical=true, std::string Description="")
QuickView.h
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::Statistics::MersenneTwisterRandomVariateGenerator::New
static Pointer New()
Method for creation through the object factory.
itkMersenneTwisterRandomVariateGenerator.h
itk::Size::SetSize
void SetSize(const SizeValueType val[VDimension])
Definition: itkSize.h:181
itkScalarToRGBColormapImageFilter.h
itk::RelabelComponentImageFilter
Relabel the components in an image such that consecutive labels are used.
Definition: itkRelabelComponentImageFilter.h:82
itkCustomColormapFunction.h
QuickView
A convenient class to render itk images with vtk.
Definition: QuickView.h:111
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
QuickView::Visualize
void Visualize(bool interact=true)
itk::Function::CustomColormapFunction
Function object which maps a scalar value into an RGB colormap value.
Definition: itkCustomColormapFunction.h:49