Skip to content

Commit 0ba2890

Browse files
committed
ENH: Add ImageBufferAndIndexRange example
Using `ImageBufferRange` and `IndexRange`, which were introduced with ITK 5.0.
1 parent 6f0e16b commit 0ba2890

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed

src/Core/Common/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_example(AddOffsetToIndex)
2222
add_example(GetNameOfClass)
2323
add_example(CreateAnImageRegion)
2424
add_example(IsPixelInsideRegion)
25+
add_example(ImageBufferAndIndexRange)
2526
add_example(ImageRegionIntersection)
2627

2728
add_example(ImageRegionOverlap)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 3.16.3)
2+
3+
project(ImageBufferAndIndexRange)
4+
5+
find_package(ITK REQUIRED)
6+
include(${ITK_USE_FILE})
7+
8+
add_executable(${PROJECT_NAME} Code.cxx)
9+
target_link_libraries(${PROJECT_NAME} ${ITK_LIBRARIES})
10+
11+
install(TARGETS ${PROJECT_NAME}
12+
DESTINATION bin/ITKSphinxExamples/Core/Common
13+
COMPONENT Runtime
14+
)
15+
16+
install(FILES Code.cxx CMakeLists.txt
17+
DESTINATION share/ITKSphinxExamples/Code/Core/Common/${PROJECT_NAME}
18+
COMPONENT Code
19+
)
20+
21+
enable_testing()
22+
add_test(NAME ${PROJECT_NAME}Test
23+
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROJECT_NAME})
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*=========================================================================
2+
*
3+
* Copyright NumFOCUS
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0.txt
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*=========================================================================*/
18+
19+
#include "itkImage.h"
20+
#include "itkImageBufferRange.h"
21+
#include "itkIndexRange.h"
22+
#include "itkNumericTraits.h"
23+
24+
#include <cassert>
25+
#include <numeric> // For iota
26+
27+
namespace
28+
{
29+
// Creates an image with sequentially increasing pixel values (0, 1, 2, ...).
30+
template <typename TImage>
31+
typename TImage::Pointer
32+
CreateImageWithSequentiallyIncreasingPixelValues(const typename TImage::RegionType & region)
33+
{
34+
using PixelType = typename TImage::PixelType;
35+
36+
const auto image = TImage::New();
37+
image->SetRegions(region);
38+
image->Allocate();
39+
40+
const itk::ImageBufferRange<TImage> imageBufferRange(*image);
41+
std::iota(imageBufferRange.begin(), imageBufferRange.end(), PixelType{ 0 });
42+
43+
return image;
44+
}
45+
46+
47+
// Prints all pixel values with their N-dimensional indices.
48+
template <typename TImage>
49+
void
50+
PrintPixelValues(const TImage & image)
51+
{
52+
constexpr unsigned int Dimension{ TImage::ImageDimension };
53+
const itk::ImageRegion<Dimension> region = image.GetBufferedRegion();
54+
const itk::ImageRegionIndexRange<Dimension> indexRange(region);
55+
const itk::ImageBufferRange<const TImage> imageBufferRange(image);
56+
57+
using PixelType = typename TImage::PixelType;
58+
using PrintType = typename itk::NumericTraits<PixelType>::PrintType;
59+
60+
std::cout << "Region index: " << region.GetIndex() << "; Region size: " << region.GetSize() << "\n\n";
61+
62+
auto indexIterator = indexRange.cbegin();
63+
64+
for (const PixelType pixel : imageBufferRange)
65+
{
66+
const itk::Index<Dimension> index = *indexIterator;
67+
std::cout << "Pixel index: " << index << "; Pixel value: " << PrintType{ pixel } << '\n';
68+
++indexIterator;
69+
}
70+
71+
assert(indexIterator == indexRange.cend());
72+
}
73+
74+
} // namespace
75+
76+
77+
int
78+
main()
79+
{
80+
using PixelType = unsigned char;
81+
using ImageType = itk::Image<PixelType>;
82+
using RegionType = ImageType::RegionType;
83+
84+
const RegionType region(itk::MakeIndex(100, 200), itk::MakeSize(4, 5));
85+
const ImageType::ConstPointer image = CreateImageWithSequentiallyIncreasingPixelValues<ImageType>(region);
86+
PrintPixelValues(*image);
87+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
:name: ImageBufferAndIndexRange
2+
3+
Image Buffer and Index Range
4+
============================
5+
6+
.. index::
7+
single: ImageBufferRange
8+
9+
Synopsis
10+
--------
11+
12+
13+
This example demonstrates how to iterate over all pixels of the buffered region
14+
of an image, using either an iterator-based algorithm from the C++ Standard
15+
Library, or a range-based for loop.
16+
17+
18+
Results
19+
-------
20+
21+
Output::
22+
23+
Region index: [100, 200]; Region size: [4, 5]
24+
25+
Pixel index: [100, 200]; Pixel value: 0
26+
Pixel index: [101, 200]; Pixel value: 1
27+
Pixel index: [102, 200]; Pixel value: 2
28+
Pixel index: [103, 200]; Pixel value: 3
29+
Pixel index: [100, 201]; Pixel value: 4
30+
Pixel index: [101, 201]; Pixel value: 5
31+
Pixel index: [102, 201]; Pixel value: 6
32+
Pixel index: [103, 201]; Pixel value: 7
33+
Pixel index: [100, 202]; Pixel value: 8
34+
Pixel index: [101, 202]; Pixel value: 9
35+
Pixel index: [102, 202]; Pixel value: 10
36+
Pixel index: [103, 202]; Pixel value: 11
37+
Pixel index: [100, 203]; Pixel value: 12
38+
Pixel index: [101, 203]; Pixel value: 13
39+
Pixel index: [102, 203]; Pixel value: 14
40+
Pixel index: [103, 203]; Pixel value: 15
41+
Pixel index: [100, 204]; Pixel value: 16
42+
Pixel index: [101, 204]; Pixel value: 17
43+
Pixel index: [102, 204]; Pixel value: 18
44+
Pixel index: [103, 204]; Pixel value: 19
45+
46+
Code
47+
----
48+
49+
C++
50+
...
51+
52+
.. literalinclude:: Code.cxx
53+
:lines: 18-
54+
55+
56+
Classes demonstrated
57+
--------------------
58+
59+
.. breathelink:: itk::ImageBufferRange itk::IndexRange

src/Core/Common/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Common
5858
GetNameOfClass/Documentation.rst
5959
GetOrSetMemberVariableOfITKClass/Documentation.rst
6060
GetTypeBasicInformation/Documentation.rst
61+
ImageBufferAndIndexRange/Documentation.rst
6162
ImageRegionIntersection/Documentation.rst
6263
ImageRegionOverlap/Documentation.rst
6364
ImportPixelBufferIntoAnImage/Documentation.rst

0 commit comments

Comments
 (0)