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: Add bufferedRegion to dict_from_image representation #4633

Merged
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
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