Skip to content

Commit b54c78d

Browse files
committed
Enhance image conversion logic in MonaiBundleInferenceOperator
- Improved handling of different dimensional outputs during image conversion to maintain DICOM pixel data arrangement. - Added specific cases for 2D and 3D images to ensure correct axis handling and data type conversion. Signed-off-by: Victor Chang <[email protected]>
1 parent 94eafcd commit b54c78d

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

monai/deploy/operators/monai_bundle_inference_operator.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,18 @@ def _send_output(self, value: Any, name: str, metadata: Dict, op_output, context
966966

967967
logging.debug(f"Output {name} numpy image shape: {value.shape}")
968968

969-
result: Any = Image(np.swapaxes(np.squeeze(value, 0), 0, 2).astype(np.uint8), metadata=metadata)
969+
# Handle different dimensional outputs while maintaining DICOM pixel data arrangement
970+
squeezed_value = np.squeeze(value, 0)
971+
if squeezed_value.ndim == 2:
972+
# 2D case: Keep as (H, W) - no axis swapping needed for 2D images
973+
out_img = squeezed_value.astype(np.uint8)
974+
elif squeezed_value.ndim == 3:
975+
# 3D case: (W, H, D) -> (D, H, W) for DICOM volumetric data
976+
out_img = np.swapaxes(squeezed_value, 0, 2).astype(np.uint8)
977+
else:
978+
# Higher dimensions: apply standard DICOM conversion
979+
out_img = np.swapaxes(squeezed_value, 0, 2).astype(np.uint8)
980+
result: Any = Image(out_img, metadata=metadata)
970981
logging.debug(f"Converted Image shape: {result.asnumpy().shape}")
971982
elif otype == np.ndarray:
972983
result = np.asarray(value)

0 commit comments

Comments
 (0)