ITK  6.0.0
Insight Toolkit
SphinxExamples/src/Core/Common/ImageBufferAndIndexRange/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 "itkIndexRange.h"
#include <cassert>
#include <numeric> // For iota
namespace
{
// Creates an image with sequentially increasing pixel values (0, 1, 2, ...).
template <typename TImage>
CreateImageWithSequentiallyIncreasingPixelValues(const typename TImage::RegionType & region)
{
using PixelType = typename TImage::PixelType;
const auto image = TImage::New();
image->SetRegions(region);
image->Allocate();
const itk::ImageBufferRange<TImage> imageBufferRange(*image);
std::iota(imageBufferRange.begin(), imageBufferRange.end(), PixelType{ 0 });
return image;
}
// Prints all pixel values with their N-dimensional indices.
template <typename TImage>
void
PrintPixelValues(const TImage & image)
{
constexpr unsigned int Dimension{ TImage::ImageDimension };
const itk::ImageRegion<Dimension> region = image.GetBufferedRegion();
const itk::ImageRegionIndexRange<Dimension> indexRange(region);
const itk::ImageBufferRange<const TImage> imageBufferRange(image);
using PixelType = typename TImage::PixelType;
using PrintType = typename itk::NumericTraits<PixelType>::PrintType;
std::cout << "Region index: " << region.GetIndex() << "; Region size: " << region.GetSize() << "\n\n";
auto indexIterator = indexRange.cbegin();
for (const PixelType pixel : imageBufferRange)
{
const itk::Index<Dimension> index = *indexIterator;
std::cout << "Pixel index: " << index << "; Pixel value: " << PrintType{ pixel } << '\n';
++indexIterator;
}
assert(indexIterator == indexRange.cend());
}
} // namespace
int
main()
{
using PixelType = unsigned char;
using ImageType = itk::Image<PixelType>;
const RegionType region(itk::MakeIndex(100, 200), itk::MakeSize(4, 5));
const ImageType::ConstPointer image = CreateImageWithSequentiallyIncreasingPixelValues<ImageType>(region);
PrintPixelValues(*image);
}
itkIndexRange.h
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
ConstPointer
SmartPointer< const Self > ConstPointer
Definition: itkAddImageFilter.h:94
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:68
itk::MakeSize
auto MakeSize(const T... values)
Definition: itkSize.h:515
itk::ImageRegion::GetIndex
const IndexType & GetIndex() const
Definition: itkImageRegion.h:188
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:80
itkImage.h
itk::ImageRegion::GetSize
const SizeType & GetSize() const
Definition: itkImageRegion.h:209
itk::ImageBufferRange::end
iterator end() const noexcept
Definition: itkImageBufferRange.h:593
itk::IndexRange::cend
constexpr const_iterator cend() const noexcept
Definition: itkIndexRange.h:379
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::NumericTraits::PrintType
T PrintType
Definition: itkNumericTraits.h:70
itk::ImageBufferRange
Definition: itkImageBufferRange.h:73
itk::ImageBufferRange::begin
iterator begin() const noexcept
Definition: itkImageBufferRange.h:586
itk::IndexRange::cbegin
constexpr const_iterator cbegin() const noexcept
Definition: itkIndexRange.h:372
itk::MakeIndex
auto MakeIndex(const T... values)
Definition: itkIndex.h:604
itk::IndexRange
Definition: itkIndexRange.h:78
itkNumericTraits.h
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itkImageBufferRange.h