ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Core/Common/IterateOverARegionWithAShapedNeighborhoodIteratorManual/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"
// Notice that char type pixel values will not appear
// properly on the command prompt therefore for the
// demonstration purposes it is best to use the int
// type, however in real applications iterators have
// no problems with char type images.
// using ImageType = itk::Image<unsigned char, 2>;
using ImageType = itk::Image<unsigned int, 2>;
void
CreateImage(ImageType::Pointer image);
int
main(int, char *[])
{
ImageType::Pointer image = ImageType::New();
CreateImage(image);
itk::Size<2> radius;
radius.Fill(1);
IteratorType iterator(radius, image, image->GetLargestPossibleRegion());
std::cout << "By default there are " << iterator.GetActiveIndexListSize() << " active indices." << std::endl;
IteratorType::OffsetType top = { { 0, -1 } };
iterator.ActivateOffset(top);
IteratorType::OffsetType bottom = { { 0, 1 } };
iterator.ActivateOffset(bottom);
std::cout << "Now there are " << iterator.GetActiveIndexListSize() << " active indices." << std::endl;
IteratorType::IndexListType indexList = iterator.GetActiveIndexList();
IteratorType::IndexListType::const_iterator listIterator = indexList.begin();
while (listIterator != indexList.end())
{
std::cout << *listIterator << " ";
++listIterator;
}
std::cout << std::endl;
// Note that ZeroFluxNeumannBoundaryCondition is used by default so even
// pixels outside of the image will have valid values (equivalent to their neighbors just inside the image)
for (iterator.GoToBegin(); !iterator.IsAtEnd(); ++iterator)
{
std::cout << "New position: " << std::endl;
IteratorType::ConstIterator ci = iterator.Begin();
while (!ci.IsAtEnd())
{
std::cout << "Centered at " << iterator.GetIndex() << std::endl;
std::cout << "Neighborhood index " << ci.GetNeighborhoodIndex() << " is offset " << ci.GetNeighborhoodOffset()
<< " and has value " << ci.Get() << " The real index is "
<< iterator.GetIndex() + ci.GetNeighborhoodOffset() << std::endl;
ci++;
}
}
std::cout << std::endl;
return EXIT_SUCCESS;
}
void
CreateImage(ImageType::Pointer image)
{
start.Fill(0);
size.Fill(10);
ImageType::RegionType region(start, size);
image->SetRegions(region);
image->Allocate();
image->FillBuffer(0);
}
itkNeighborhoodAlgorithm.h
itk::Size< 2 >
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itkImage.h
itkImageRegionIterator.h
itk::ShapedNeighborhoodIterator< ImageType >
itk::Index::Fill
void Fill(IndexValueType value)
Definition: itkIndex.h:270
itk::Size::Fill
void Fill(SizeValueType value)
Definition: itkSize.h:211
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itkShapedNeighborhoodIterator.h
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86