ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Filtering/BinaryMathematicalMorphology/ClosingBinaryImage/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
*
* http://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"
#ifdef ENABLE_QUICKVIEW
# include "QuickView.h"
#endif
namespace
{
using ImageType = itk::Image<unsigned char, 2>;
}
static void
CreateImage(ImageType * const image);
int
main(int argc, char * argv[])
{
ImageType::Pointer image;
if (argc == 1)
{
image = ImageType::New();
CreateImage(image);
}
else
{
using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
reader->Update();
image = reader->GetOutput();
}
unsigned int radius = 5;
if (argc == 3)
{
std::stringstream ss(argv[2]);
ss >> radius;
}
std::cout << "Radius: " << radius << std::endl;
StructuringElementType structuringElement;
structuringElement.SetRadius(radius);
structuringElement.CreateStructuringElement();
using BinaryMorphologicalClosingImageFilterType =
BinaryMorphologicalClosingImageFilterType::Pointer closingFilter = BinaryMorphologicalClosingImageFilterType::New();
closingFilter->SetInput(image);
closingFilter->SetKernel(structuringElement);
closingFilter->Update();
SubtractType::Pointer diff = SubtractType::New();
diff->SetInput1(closingFilter->GetOutput());
diff->SetInput2(image);
#ifdef ENABLE_QUICKVIEW
QuickView viewer;
std::stringstream desc;
desc << "Original ";
viewer.AddImage(image.GetPointer(), true, desc.str());
std::stringstream desc2;
desc2 << "BinaryClosing, radius = " << radius;
viewer.AddImage(closingFilter->GetOutput(), true, desc2.str());
std::stringstream desc3;
desc3 << "BinaryClosing - Original";
viewer.AddImage(diff->GetOutput(), true, desc3.str());
viewer.Visualize();
#endif
return EXIT_SUCCESS;
}
void
CreateImage(ImageType * const image)
{
// Create an image with 2 connected components
itk::Index<2> corner = { { 0, 0 } };
unsigned int NumRows = 200;
unsigned int NumCols = 300;
size[0] = NumRows;
size[1] = NumCols;
itk::ImageRegion<2> region(corner, size);
image->SetRegions(region);
image->Allocate();
// Make a square
for (unsigned int r = 40; r < 100; r++)
{
for (unsigned int c = 40; c < 100; c++)
{
itk::Index<2> pixelIndex;
pixelIndex[0] = r;
pixelIndex[1] = c;
image->SetPixel(pixelIndex, 50);
}
}
}
itk::BinaryBallStructuringElement
A Neighborhood that represents a ball structuring element (ellipsoid) with binary elements.
Definition: itkBinaryBallStructuringElement.h:62
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:66
itk::BinaryMorphologicalClosingImageFilter
binary morphological closing of an image.
Definition: itkBinaryMorphologicalClosingImageFilter.h:59
itk::Size< 2 >
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:69
itkImageFileReader.h
itkImage.h
itkBinaryBallStructuringElement.h
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itkBinaryMorphologicalClosingImageFilter.h
itk::Neighborhood::SetRadius
void SetRadius(const SizeType &)
QuickView.h
QuickView::AddImage
void AddImage(TImage *, bool FlipVertical=true, std::string Description="")
itkSubtractImageFilter.h
itk::SubtractImageFilter
Pixel-wise subtraction of two images.
Definition: itkSubtractImageFilter.h:68
QuickView
A convenient class to render itk images with vtk.
Definition: QuickView.h:111
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
QuickView::Visualize
void Visualize(bool interact=true)