Skip to content

ConstShapedNeighborhoodIterator doesn't loop over all possibilities #2387

@Leengit

Description

@Leengit

In a region of 12×12 pixels, an iterator is expected to hit all 144 possibilities. However it gets only 142 of them.

Steps to Reproduce

#include "itkImage.h"
#include "itkConstShapedNeighborhoodIterator.h"
#include "itkConstantBoundaryCondition.h"

int man(int argc, char * argv[])
{
    using PixelType = unsigned char;
    constexpr unsigned int Dimension = 2;
    using ImageType = itk::Image<PixelType, Dimension>;
    using ImagePointer = typename ImageType::Pointer;
    using RegionType = typename ImageType::RegionType;
    using IndexType = typename RegionType::IndexType;
    using SizeType = typename RegionType::SizeType;
    using OffsetType = typename RegionType::OffsetType;
    ImagePointer imageTest = ImageType::New();
    imageTest->SetRegions(RegionType{ IndexType{ 0, 0 }, SizeType{ 10, 10 } });
    imageTest->Allocate();
    using BoundaryConditionType = itk::ConstantBoundaryCondition<ImageType>;
    using SquareIterator = itk::ConstShapedNeighborhoodIterator<ImageType, BoundaryConditionType>;
    using RadiusType = typename SquareIterator::RadiusType;
    SquareIterator sqIt(RadiusType{ 1, 1 }, imageTest, RegionType{ IndexType{ -1, -1 }, SizeType{ 12, 12 } });
    sqIt.ActivateOffset(OffsetType{ 0, 0 });
    sqIt.ActivateOffset(OffsetType{ 0, 1 });
    sqIt.ActivateOffset(OffsetType{ 1, 0 });
    sqIt.ActivateOffset(OffsetType{ 1, 1 });
    int numberOfIterations{ 0 };
    for (sqIt.GoToBegin(); !sqIt.IsAtEnd(); ++sqIt)
    {
      ++numberOfIterations;
    }
    std::cout << "numberOfIterations = " << numberOfIterations << " but should be 144." << std::endl;
}

Expected behavior

Prints numberOfIterations = 144 but should be 144.

Actual behavior

Prints numberOfIterations = 142 but should be 144.

Reproducibility

100%

Versions

Master branch (currently 25dea2f).

Environment

Ubuntu 20.04. gcc 10.2.0.

Additional information.

I am not completely sure by any means, but maybe the problem is that the region for the square iterator includes values with x < 0. Because of this, the .end() value that is calculated as the left-most position of the row after the region can be misinterpreted as being near the right end of the previous row. If that's true, maybe it is also true that a computed WrapOffset is negative when it is implicitly assumed to always be positive.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:Use_Milestone_BacklogUse "Backlog" milestone instead of label for issues without a fixed deadlinetype:BugInconsistencies or issues which will cause an incorrect result under some or all circumstances

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions