ITK  6.0.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
*
* 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"
#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[])
{
unsigned int radius = 5;
std::string outputFilename = "Output.png";
if (argc >= 4)
{
image = itk::ReadImage<ImageType>(argv[1]);
std::stringstream ss(argv[2]);
ss >> radius;
outputFilename = argv[3];
}
else
{
image = ImageType::New();
CreateImage(image);
}
std::cout << "Radius: " << radius << std::endl;
StructuringElementType structuringElement;
structuringElement.SetRadius(radius);
structuringElement.CreateStructuringElement();
using BinaryMorphologicalClosingImageFilterType =
closingFilter->SetInput(image);
closingFilter->SetKernel(structuringElement);
closingFilter->Update();
auto 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
itk::WriteImage(closingFilter->GetOutput(), outputFilename);
return EXIT_SUCCESS;
}
void
CreateImage(ImageType * const image)
{
// Create an image with a gray square
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(true);
// 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);
}
}
}
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
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:68
itk::BinaryMorphologicalClosingImageFilter
binary morphological closing of an image.
Definition: itkBinaryMorphologicalClosingImageFilter.h:58
itk::Size< 2 >
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:80
itkImageFileReader.h
itkImage.h
itkBinaryBallStructuringElement.h
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
itkImageFileWriter.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::WriteImage
ITK_TEMPLATE_EXPORT void WriteImage(TImagePointer &&image, const std::string &filename, bool compress=false)
Definition: itkImageFileWriter.h:256