Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix issue #1950, "ImageMaskSpatialObject tries to access pixels outside the image buffer (segfault)" #1951

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions Modules/Core/SpatialObjects/include/itkImageMaskSpatialObject.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,12 @@ template <unsigned int TDimension, typename TPixel>
bool
ImageMaskSpatialObject<TDimension, TPixel>::IsInsideInObjectSpace(const PointType & point) const
{
typename Superclass::InterpolatorType::ContinuousIndexType index;
if (this->GetImage()->TransformPhysicalPointToContinuousIndex(point, index))
{
using InterpolatorOutputType = typename InterpolatorType::OutputType;
bool insideMask = (Math::NotExactlyEquals(DefaultConvertPixelTraits<InterpolatorOutputType>::GetScalarValue(
this->GetInterpolator()->EvaluateAtContinuousIndex(index)),
NumericTraits<PixelType>::ZeroValue()));
if (insideMask)
{
return true;
}
}
const ImageType * const image = this->GetImage();

const IndexType index = image->TransformPhysicalPointToIndex(point);

return false;
return image->GetBufferedRegion().IsInside(index) &&
Math::NotExactlyEquals(image->GetPixel(index), NumericTraits<PixelType>::ZeroValue());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,20 @@ TEST(ImageMaskSpatialObject, IsInsideIndependentOfDistantPixels)


#endif


// Tests that IsInsideInObjectSpace returns false for a corner point, when the
// mask image is filled with zero values. This test would sometimes fail on
// ITK v5.0.1 and v5.1.0
TEST(ImageMaskSpatialObject, CornerPointIsNotInsideMaskOfZeroValues)
{
// Create a mask image, and fill the image with zero vales.
const auto image = itk::Image<unsigned char>::New();
image->SetRegions(itk::Size<>{ { 2, 2 } });
image->Allocate(true);

const auto imageMaskSpatialObject = itk::ImageMaskSpatialObject<2>::New();
imageMaskSpatialObject->SetImage(image);
const double cornerPoint[] = { 1.5, 1.5 };
ASSERT_FALSE(imageMaskSpatialObject->IsInsideInObjectSpace(cornerPoint));
}