|
| 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 "itkConnectedImageNeighborhoodShape.h" |
| 20 | +#include "itkImage.h" |
| 21 | +#include "itkImageBufferRange.h" |
| 22 | +#include "itkImageNeighborhoodOffsets.h" |
| 23 | +#include "itkRectangularImageNeighborhoodShape.h" |
| 24 | +#include "itkShapedImageNeighborhoodRange.h" |
| 25 | + |
| 26 | +#include <array> |
| 27 | +#include <cassert> |
| 28 | +#include <numeric> // For iota |
| 29 | +#include <ios> // For hex |
| 30 | + |
| 31 | +namespace |
| 32 | +{ |
| 33 | +constexpr unsigned int Dimension{ 2 }; |
| 34 | +using OffsetType = itk::Offset<Dimension>; |
| 35 | + |
| 36 | +// Print the specified offsets of a neighborhood shape. Also prints the pixel values of a small image for which such a |
| 37 | +// shaped neigborhood located at the image center is filled with consecutive values, 1, 2, 3, ..., N. |
| 38 | +template <typename TOffsets> |
| 39 | +void |
| 40 | +PrintImageNeighborhoodShape(const TOffsets & offsets) |
| 41 | +{ |
| 42 | + std::cout << " "; |
| 43 | + |
| 44 | + for (const OffsetType & offset : offsets) |
| 45 | + { |
| 46 | + std::cout << offset << ' '; |
| 47 | + } |
| 48 | + |
| 49 | + using ImageType = itk::Image<int>; |
| 50 | + const auto image = ImageType::New(); |
| 51 | + constexpr unsigned int imageSize{ 7 }; |
| 52 | + image->SetRegions(ImageType::SizeType::Filled(imageSize)); |
| 53 | + image->Allocate(true); |
| 54 | + |
| 55 | + const auto centerIndex = ImageType::IndexType::Filled(imageSize / 2); |
| 56 | + const itk::ShapedImageNeighborhoodRange<ImageType> shapedImageNeighborhoodRange(*image, centerIndex, offsets); |
| 57 | + |
| 58 | + // Set the values of the pixels in the "shaped neighborhood" of the image center to 1, 2, 3, ..., N, consecutively. |
| 59 | + std::iota(shapedImageNeighborhoodRange.begin(), shapedImageNeighborhoodRange.end(), 1); |
| 60 | + |
| 61 | + std::cout << "\n\n"; |
| 62 | + const std::ios_base::fmtflags flags(std::cout.flags()); |
| 63 | + std::cout << std::hex << std::uppercase; |
| 64 | + |
| 65 | + const itk::ImageBufferRange<const ImageType> imageBufferRange(*image); |
| 66 | + auto imageBufferIterator = imageBufferRange.cbegin(); |
| 67 | + |
| 68 | + for (int y{ 0 }; y < imageSize; ++y) |
| 69 | + { |
| 70 | + std::cout << " "; |
| 71 | + |
| 72 | + for (int x{ 0 }; x < imageSize; ++x) |
| 73 | + { |
| 74 | + std::cout << *imageBufferIterator << ' '; |
| 75 | + ++imageBufferIterator; |
| 76 | + } |
| 77 | + std::cout << '\n'; |
| 78 | + } |
| 79 | + std::cout.flags(flags); |
| 80 | + std::cout << '\n'; |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace |
| 84 | + |
| 85 | + |
| 86 | +int |
| 87 | +main() |
| 88 | +{ |
| 89 | + const std::array<OffsetType, 3> offsets = { { { { 0, -1 } }, { { 0, 1 } }, { { 1, 1 } } } }; |
| 90 | + std::cout << "Shape of some arbitrary offsets:\n\n"; |
| 91 | + PrintImageNeighborhoodShape(offsets); |
| 92 | + |
| 93 | + const bool includeCenterPixel = false; |
| 94 | + const size_t maximumCityblockDistance = 1; |
| 95 | + std::cout << "4-connected neighborhood shape (excluding the center pixel) with maximumCityblockDistance = " |
| 96 | + << maximumCityblockDistance << ":\n\n"; |
| 97 | + |
| 98 | + // GenerateConnectedImageNeighborhoodShapeOffsets returns an std::array of offsets. |
| 99 | + const auto connectedImageNeighborhoodShapeOffsets = |
| 100 | + itk::GenerateConnectedImageNeighborhoodShapeOffsets<Dimension, maximumCityblockDistance, includeCenterPixel>(); |
| 101 | + PrintImageNeighborhoodShape(connectedImageNeighborhoodShapeOffsets); |
| 102 | + |
| 103 | + const itk::Size<Dimension> radius = { { 1, 2 } }; |
| 104 | + std::cout << "Rectangular shape of radius " << radius << ":\n\n"; |
| 105 | + |
| 106 | + // GenerateRectangularImageNeighborhoodOffsets returns an std::vector of offsets. |
| 107 | + const auto rectangularImageNeighborhoodOffsets = itk::GenerateRectangularImageNeighborhoodOffsets(radius); |
| 108 | + PrintImageNeighborhoodShape(rectangularImageNeighborhoodOffsets); |
| 109 | +} |
0 commit comments