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

Fix inverse masking in MRXS files due to transparency handling #291

Open
YoniSchirris opened this issue Mar 6, 2025 · 2 comments · May be fixed by #292
Open

Fix inverse masking in MRXS files due to transparency handling #291

YoniSchirris opened this issue Mar 6, 2025 · 2 comments · May be fixed by #292

Comments

@YoniSchirris
Copy link

Fix inverse masking in MRXS files due to transparency handling

Issue Description

When processing MRXS files, the tissue masking algorithm produces inverted masks where background is incorrectly identified as tissue. This occurs because MRXS files contain transparent pixels in the scanning area, and this is represented as black with current RGBA->RGB handling, which OTSU thresholding then recognized to be foreground.

Can't post images because they are not public.

Root Cause

  1. MRXS files contain large scanning areas with transparent pixels (alpha channel)
  2. The current code truncates the alpha channel with np.array(image)[..., :3], converting transparent pixels to black
  3. During masking, these black areas are incorrectly identified as tissue, resulting in an inverse mask

Technical Details

In wsi_handler.py, both read_region and get_full_img methods discard the alpha channel:

# Current implementation
region = self.file_ptr.read_region(new_coord, self.read_lv, size)
return np.array(region)[..., :3]  # Discards alpha, transparent pixels become black

Solution

Add a static helper method to FileHandler that properly handles transparency by compositing RGBA images with a white background:

@staticmethod
def pil_to_rgb_array(pil_img):
    """Convert PIL image to RGB numpy array, properly handling transparency."""
    if pil_img.mode == 'RGBA':
        background = Image.new('RGBA', pil_img.size, (255, 255, 255, 255))
        return np.array(Image.alpha_composite(background, pil_img).convert('RGB'))
    else:
        return np.array(pil_img.convert('RGB'))

Then update the relevant methods to use this helper:

# In read_region and get_full_img
region_pil = self.file_ptr.read_region(new_coord, self.read_lv, size)
return self.pil_to_rgb_array(region_pil)

Considerations

  1. We explored
    • Simple alpha truncation (original approach) - caused the issue
    • PIL's convert('RGB') - didn't properly handle transparency in MRXS files

I can't think of negative side effects, since performance effect is negligible, and I don't know of any usage of transparency in WSI formats that is not to represent unscanned tissue.

@YoniSchirris
Copy link
Author

@vqdang I have the fix and PR ready but i can't push a new branch.

@simongraham
Copy link
Collaborator

Hi @YoniSchirris - thanks for this!

You should be able to make a PR - did you fork the repo?

Simon

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 a pull request may close this issue.

2 participants