Skip to content

Commit

Permalink
BUG: Add bufferedRegion to dict_from_image representation
Browse files Browse the repository at this point in the history
Optional specification of the BufferedRegion, which is a subset of the
LargestPossibleRegion. Addresses InsightSoftwareConsortium#4267.

The LargestPossibleRegion's Index is assumed to be trivial (zeros).

Also update the NumPy bridge to correctly use the BufferedRegion instead
of the LargestPossibleRegion for the array shape.
  • Loading branch information
thewtex committed May 1, 2024
1 parent b5b105d commit c66b046
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Modules/Bridge/NumPy/wrapping/PyBuffer.i.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
if source:
source.UpdateLargestPossibleRegion()

itksize = image.GetLargestPossibleRegion().GetSize()
itksize = image.GetBufferedRegion().GetSize()
dim = len(itksize)
shape = [int(itksize[idx]) for idx in range(dim)]

Expand Down
8 changes: 8 additions & 0 deletions Wrapping/Generators/Python/Tests/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ def custom_callback(name, progress):
diff = itk.comparison_image_filter(image.astype(itk.F), image_back.astype(itk.F))
assert np.sum(diff) == 0


largest_region = itk.ImageRegion[2]([0, 0], [1024, 1024])
image.SetLargestPossibleRegion(largest_region)
image_dict = itk.dict_from_image(image)
image_back = itk.image_from_dict(image_dict)
diff = itk.comparison_image_filter(image.astype(itk.F), image_back.astype(itk.F))
assert np.sum(diff) == 0

mesh_dict = itk.dict_from_mesh(mesh)
mesh_back = itk.mesh_from_dict(mesh_dict)

Expand Down
15 changes: 14 additions & 1 deletion Wrapping/Generators/Python/itk/support/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,12 +789,15 @@ def dict_from_image(image: "itkt.Image") -> Dict:

pixel_arr = itk.array_from_image(image)
imageType = wasm_type_from_image_type(image)
buffered_region = image.GetBufferedRegion()
bufferedRegion = { "index": tuple(buffered_region.GetIndex()), "size": tuple(buffered_region.GetSize())}
return dict(
imageType=imageType,
name=image.GetObjectName(),
origin=tuple(image.GetOrigin()),
spacing=tuple(image.GetSpacing()),
size=tuple(image.GetBufferedRegion().GetSize()),
size=tuple(image.GetLargestPossibleRegion().GetSize()),
bufferedRegion=bufferedRegion,
direction=np.asarray(image.GetDirection()),
data=pixel_arr,
)
Expand All @@ -808,10 +811,20 @@ def image_from_dict(image_dict: Dict) -> "itkt.Image":
if image_dict["data"] is None:
image = ImageType.New()
image.SetRegions(image_dict["size"])
if "bufferedRegion" in image_dict:
buffered_region = itk.ImageRegion[image.GetImageDimension()]()
buffered_region.SetIndex(image_dict["bufferedRegion"]["index"])
buffered_region.SetSize(image_dict["bufferedRegion"]["size"])
image.SetBufferedRegion(buffered_region)
image.Allocate(True)
else:
image = itk.PyBuffer[ImageType].GetImageViewFromArray(image_dict["data"])
image.SetRegions(image_dict["size"])
if "bufferedRegion" in image_dict:
buffered_region = itk.ImageRegion[image.GetImageDimension()]()
buffered_region.SetIndex(image_dict["bufferedRegion"]["index"])
buffered_region.SetSize(image_dict["bufferedRegion"]["size"])
image.SetBufferedRegion(buffered_region)
image.SetOrigin(image_dict["origin"])
image.SetSpacing(image_dict["spacing"])
image.SetDirection(image_dict["direction"])
Expand Down

0 comments on commit c66b046

Please sign in to comment.