-
Notifications
You must be signed in to change notification settings - Fork 256
Description
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
- MRXS files contain large scanning areas with transparent pixels (alpha channel)
- The current code truncates the alpha channel with
np.array(image)[..., :3]
, converting transparent pixels to black - 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
- 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.