ITK  6.0.0
Insight Toolkit
SphinxExamples/src/Core/Common/GenerateOffsetsShapedImageNeighborhood/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"
#include <array>
#include <cassert>
#include <numeric> // For iota
#include <ios> // For hex
namespace
{
constexpr unsigned int Dimension{ 2 };
using OffsetType = itk::Offset<Dimension>;
// Print the specified offsets of a neighborhood shape. Also prints the pixel values of a small image for which such a
// shaped neigborhood located at the image center is filled with consecutive values, 1, 2, 3, ..., N.
template <typename TOffsets>
void
PrintImageNeighborhoodShape(const TOffsets & offsets)
{
std::cout << " ";
for (const OffsetType & offset : offsets)
{
std::cout << offset << ' ';
}
using ImageType = itk::Image<int>;
const auto image = ImageType::New();
constexpr unsigned int imageSize{ 7 };
image->SetRegions(ImageType::SizeType::Filled(imageSize));
image->Allocate(true);
const auto centerIndex = ImageType::IndexType::Filled(imageSize / 2);
const itk::ShapedImageNeighborhoodRange<ImageType> shapedImageNeighborhoodRange(*image, centerIndex, offsets);
// Set the values of the pixels in the "shaped neighborhood" of the image center to 1, 2, 3, ..., N, consecutively.
std::iota(shapedImageNeighborhoodRange.begin(), shapedImageNeighborhoodRange.end(), 1);
std::cout << "\n\n";
const std::ios_base::fmtflags flags(std::cout.flags());
std::cout << std::hex << std::uppercase;
const itk::ImageBufferRange<const ImageType> imageBufferRange(*image);
auto imageBufferIterator = imageBufferRange.cbegin();
for (unsigned int y{ 0 }; y < imageSize; ++y)
{
std::cout << " ";
for (unsigned int x{ 0 }; x < imageSize; ++x)
{
std::cout << *imageBufferIterator << ' ';
++imageBufferIterator;
}
std::cout << '\n';
}
std::cout.flags(flags);
std::cout << '\n';
}
} // namespace
int
main()
{
const std::array<OffsetType, 3> offsets = { { { { 0, -1 } }, { { 0, 1 } }, { { 1, 1 } } } };
std::cout << "Shape of some arbitrary offsets:\n\n";
PrintImageNeighborhoodShape(offsets);
const bool includeCenterPixel = false;
const size_t maximumCityblockDistance = 1;
std::cout << "4-connected neighborhood shape (excluding the center pixel) with maximumCityblockDistance = "
<< maximumCityblockDistance << ":\n\n";
// GenerateConnectedImageNeighborhoodShapeOffsets returns an std::array of offsets.
const auto connectedImageNeighborhoodShapeOffsets =
itk::GenerateConnectedImageNeighborhoodShapeOffsets<Dimension, maximumCityblockDistance, includeCenterPixel>();
PrintImageNeighborhoodShape(connectedImageNeighborhoodShapeOffsets);
const itk::Size<Dimension> radius = { { 1, 2 } };
std::cout << "Rectangular shape of radius " << radius << ":\n\n";
// GenerateRectangularImageNeighborhoodOffsets returns an std::vector of offsets.
const auto rectangularImageNeighborhoodOffsets = itk::GenerateRectangularImageNeighborhoodOffsets(radius);
PrintImageNeighborhoodShape(rectangularImageNeighborhoodOffsets);
}
itkImageNeighborhoodOffsets.h
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itkConnectedImageNeighborhoodShape.h
itk::ShapedImageNeighborhoodRange::begin
iterator begin() const noexcept
Definition: itkShapedImageNeighborhoodRange.h:716
itkImage.h
itkShapedImageNeighborhoodRange.h
itkRectangularImageNeighborhoodShape.h
itk::GenerateRectangularImageNeighborhoodOffsets
std::vector< Offset< VImageDimension > > GenerateRectangularImageNeighborhoodOffsets(const Size< VImageDimension > &radius)
Definition: itkImageNeighborhoodOffsets.h:43
itk::ImageBufferRange
Definition: itkImageBufferRange.h:73
itk::Offset
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image.
Definition: itkOffset.h:66
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
itk::ShapedImageNeighborhoodRange::end
iterator end() const noexcept
Definition: itkShapedImageNeighborhoodRange.h:724
itk::ImageBufferRange::cbegin
const_iterator cbegin() const noexcept
Definition: itkImageBufferRange.h:604
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::ShapedImageNeighborhoodRange
Definition: itkShapedImageNeighborhoodRange.h:92
itkImageBufferRange.h