Skip to content

Conversation

@maneeshrj
Copy link

Casting to uint8 will cause integer overflow if the pixel value is beyond the display range, e.g. a value of 257 will overflow to 1. The convention (e.g. in MATLAB, matplotlib) seems to be that values above the display range will be displayed as 255 and values below the display range will be displayed as 0, so to match this behavior in stackview, the casting should be done after clipping the pixel value to the range [0,255].

If the way it's implemented now is intentional, feel free to ignore this, but I figured it's probably a bug.

Casting to uint8 will cause overflow if the pixel value is beyond the display range. Common behavior (e.g. in MATLAB, matplotlib) is that any values above the display range will be displayed as 255 so to match this in stackview the casting should be done after the clipping to [0,255].
@haesleinhuepf
Copy link
Owner

Hi @maneeshrj

interesting, thanka for this suggestion! Could you please provide a scenario / example code / example image where I could see the impact of this code modification?

Thanks!

Best,
Robert

@maneeshrj
Copy link
Author

Hi Robert,

For my own work I'm trying to view brain MRI data, specifically quantitative susceptibility maps (QSM). For the display range we tend to use something like [-0.2, 0.2], but due to pathology (e.g. hemorrhages in the brain), we can get very high values beyond the display max. Automatically scaling the display range will result in poor contrast in the rest of the brain. However, setting the display range to [-0.2, 0.2] causes those high values to appear incorrect due to the overflow bug.

Here's an example of what a hemorrhage looks like with the current code vs with the bug fix.

Screenshot 2025-11-15 140106 Screenshot 2025-11-15 135531

As it is now, the values in the hemorrhage are misleading. They look very low but are actually very high. I noticed this behavior because until now I've been using matplotlib and MATLAB imshow, and with the same display range, they both display the hemorrhage as white. Hope that helps, and thanks for your time!

Trying to implement a tool for python notebooks that matches the functionality of Yi Wang Lab's in-house MATLAB "vis" tool
@maneeshrj
Copy link
Author

Sorry I didn't realize that any further commits I made to my fork would show up in this pull request, I've reverted that commit.

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.

2 participants