Skip to content

BUG: make_points_image truncated point indices and ignored 4D images - #966

Open
Srizdebnath wants to merge 1 commit into
ANTsX:mainfrom
Srizdebnath:fix/make-points-image-rounding
Open

Srizdebnath wants to merge 1 commit into
ANTsX:mainfrom
Srizdebnath:fix/make-points-image-rounding

Conversation

@Srizdebnath

Copy link
Copy Markdown

make_points_image places points in the wrong voxel, and does nothing at all for 4-D targets:

import ants, numpy as np
img = ants.make_image((10, 10), 0)
pts = np.array([[3.6, 3.6],    # nearest voxel (4, 4)
                [9.6, 2.0],    # outside: voxel 9 covers [8.5, 9.5)
                [-0.7, 5.0]])  # outside: voxel 0 covers [-0.5, 0.5)
r = ants.make_points_image(pts, img, radius=0)
print(np.argwhere(r.numpy() > 0).tolist())
# [[0, 5], [3, 3], [9, 2]]   expected [[4, 4]]

img4 = ants.make_image((5, 5, 5, 5), 0)
print(ants.make_points_image(np.array([[1., 2., 3., 4.]]), img4, radius=0).sum())
# 0.0

transform_physical_point_to_index returns a continuous index, and .astype(int) truncates it toward zero:

  • Inside the image: a point more than half a voxel past a voxel centre lands in the previous voxel. With anisotropic spacing that can be up to one full voxel spacing.
  • Outside the image: points with a continuous index in [n - 0.5, n) or (-1, -0.5) are written into the edge voxels instead of being dropped.

This rounds to the nearest voxel the way ITK's TransformPhysicalPointToIndex does, rounding halves up (floor(x + 0.5)), so an integer index from ITK and the label written here agree. Points that already sit on voxel centres, like the ones in the existing test, are unaffected.

The write was also guarded by if dim == 3 / elif dim == 2, so a 4-D target image silently returned all zeros. It now indexes with tuple(idx), which works for any dimension.

Tests:

  • test_make_points_image_nearest_voxel uses an image with non-unit spacing and an origin, and checks a rounded-up point, a point inside the first voxel and two points just outside the image.
  • test_make_points_image_4d covers the 4-D case.

Both fail on main and pass here, and the existing make_points_image test still passes.

transform_physical_point_to_index returns a continuous index, and
.astype(int) truncated it toward zero. Points more than half a voxel
past a voxel center were placed in the previous voxel, and points
outside the image, with a continuous index in [n - 0.5, n) or in
(-1, -0.5), were written into the edge voxels instead of being dropped.
Round to the nearest voxel the way ITK's TransformPhysicalPointToIndex
does (halves up).

Only 2D and 3D indices were written, so 4D target images silently got
an empty label image. Index with the tuple for any dimension.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant