Skip to content

fix: PNGConverterOperator uses the wrong axis dimension #154

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

Closed
wants to merge 1 commit into from

Conversation

swederik
Copy link
Contributor

Hi everyone,

After looking at the very interesting MONAI Bootcamp videos I wanted to test out the MONAI Deploy SDK and very quickly hit a minor issue. The PNG Converter is using the wrong image axis when it sets up the loop, so if you have an image where axis 0 and axis 2 have different lengths, it throws an error (along the lines of IndexError: index 261 is out of bounds for axis 2 with size 261) and fails to produce all of the PNGs.

Thanks!

Signed-off-by: swederik <erik.sweed@gmail.com>
@gigony gigony requested a review from MMelQin September 30, 2021 18:05
@gigony gigony added the bug Something isn't working label Sep 30, 2021
@gigony
Copy link
Collaborator

gigony commented Sep 30, 2021

Thanks @swederik for this PR!

@MMelQin could you please help review this change when you have time?

Copy link
Collaborator

@MMelQin MMelQin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works with loading a set of DICOM and

@MMelQin
Copy link
Collaborator

MMelQin commented Oct 9, 2021

There is a fundamental issue with the Image class as the it lacks precise semantics of the dims of its encapsulated pixel image array, i.e. for a 3D image, which DIM is meant to be the axial/slice direction in DICOM speaking, first DIM to represent the instance, or the last DIM? What happens if some code created the Image with a pixel array that has the instance as the first DIM.

Anyway, we know the issue with this first version of Image. I had to pay close attention to the what the original image type is, and how and what image array is used when instantiating Image, e.g. DICOM to Volume get a image pixel array with depth dim (slice wise) first, but I had to change that code to swap the depth to last. There is also the swapping of width and height, to as to ensure the shape out of this image.asnumpy() would the same as those from ITK or Nibabel.

This PR may resolve the symptom somewhat, but it is at the mercy of the encapsulated image pixel array. I will ensure an issue is created to address the Image class; I don't think it is usable as intended, as it now simply provides an in-memory storage to the pertinent image array.

@dbericat
Copy link
Member

@MMelQin reading your comment above, can you link the issue to this one, and either merge this PR or close it until a PR to solve the other bigger issue is ready? Thanks!

@MMelQin
Copy link
Collaborator

MMelQin commented Jan 21, 2022

PR #238 attempts to provide consistency and deterministic representation of Image object loaded by DICOMSeriesToVolumeOperator from a DICOM series, and in doing so, we chose to be consistent with (Simple) ITK as it is one of most used module when loading DICOM instances.

So, the Image.asnumpy() is expected to have an array index order of DHW (and we can only ensure that the Image object creator, e.g. DICOMSeriesToVolumeOperator to ensure this is the case), same as Simple ITK GetArrayFromImage(). The origin,
spacing , and direction cosines are also equivalent to those returned by Simple ITK API's, GetOrigin(), GetSpacing(), GetDirection().

So, after #238 is merged and this PR is rebased, the Image.asnumpy.shape[0] will indeed be the depth (corresponding to the number of slices/instances), while 2nd and 3rd are H and W respectively. PIL fromarray expects the numpy array, and since the first index is really the depth, each slice i with index order HW then would [i, :, :]

@MMelQin
Copy link
Collaborator

MMelQin commented Feb 11, 2022

@swederik thanks again for creating the PR to fix the indexing inconsistency problem. It would have worked if the inconsistency remained, though the ndarray contained in the Image object created by the DICOM to volume operator had been made consistent, with indexing order being DHW, the same as how ITK returns ndarray from its API serving out DICOM converted image.

I thought about branching out the PR but not possible, hence created a new PR #265 to adjust the indexing to fit the new convention. So this PR will not get merged.

@swederik
Copy link
Contributor Author

Sounds good! Sorry I haven't had time to make any changes to this!

@swederik swederik closed this Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants