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

How do I make a tissue mask (with photoshop?) #73

Open
NicolaasVanRenne opened this issue Aug 7, 2023 · 4 comments
Open

How do I make a tissue mask (with photoshop?) #73

NicolaasVanRenne opened this issue Aug 7, 2023 · 4 comments

Comments

@NicolaasVanRenne
Copy link

I want to run a new analysis, this time with a mask. I can create a mask .png file with photoshop (a black and white image, in grayscale).

When I run the code:
mydir/XFuse/bin/xfuse convert image --image /mydir/XFuse/sections/23O2788/23O2788cirroseHE.jpg --scale 1.0 --mask --mask-file /mydir/XFuse/sections/23O2788/23O2788_mask_grayscale.png --save-path /mydir/XFuse/sections/23O2788_scale1

I get the following error:

[2023-08-07 16:06:31,485] ℹ : Running xfuse version 0.2.1
[2023-08-07 16:06:34,931] ℹ : Computing tissue mask:
[2023-08-07 16:06:34,932] ⚠ WARNING : UserWarning (/mydir/XFuse/xfuse/utility/mask.py:74): Failed to mask tissue
OpenCV(4.8.0) /io/opencv/modules/imgproc/src/grabcut.cpp:343: error: (-5:Bad argument) mask element value must be equal GC_BGD or GC_FGD or GC_PR_BGD or GC_PR_FGD in function 'checkMask'
[2023-08-07 16:06:35,006] ⚠ WARNING : UserWarning (/mydir/XFuse/xfuse/convert/utility.py:207): The image resolution is very large! 😱 XFuse typically works best on medium resolution images (approximately 1000x1000 px). If you experience performance issues, please consider reducing the resolution.
[2023-08-07 16:06:38,991] ℹ : Writing data to data.h5

So the .h5 file is made; but the assignments GC_BGD or GC_FGD or GC_PR_BGD or GC_PR_FGD need to be made - I understand I need to tell him GC_BGD is background (black) and GC_FGD is foreground (white). But how do I do this?

This is my mask made in photoshop:
23O2788_mask_grayscale

If I run python visualize_tissue_masks.py /mydir/XFuse/sections/23O2788_scale1/data.h5 to visualize the mask, I don't get a mask, but I do get the big border (which I did not ask for).

image

So, (i) How can I make a mask
and (ii) How can I get rid of the border?

@ludvb
Copy link
Owner

ludvb commented Aug 9, 2023

The mask should be a single-channel image and encoded like this: 0 = background, 1 = foreground, 2 = probable background, 3 = probable foreground. So if you have a png image with white = foreground and black = background, you can convert it to the expected format by running something like this (untested so may need to be tweaked a bit):

import imageio as ii

mask = ii.imread("/path/to/mask.png")
mask = mask > 0

# reduce color channels if mask is not single channel:
if mask.ndim == 3:
  mask = mask.any(-1)

# optionally, we can use the probable fg/bgd classes to let xfuse convert refine the mask:
mask = np.where(mask, 3, 2)

mask = mask.astype(np.uint8)
ii.imsave("/path/to/encoded_mask.png", mask)

Regarding the border, xfuse convert adds some margin to the bounding box of the tissue in order to make sure that the tissue boundaries are sampled during training. The added border has the average color of the edge of the image. In you case, since the tissue extends to the edge of the image, this gives a very weird result. Unfortunately, there is no CLI flag to turn off this behavior, so you would need to modify the code. This can be done by changing the line

margin: float = 0.12,

to margin: float = 0.0,.

@NicolaasVanRenne
Copy link
Author

I think I cracked it. I managed to make a mask using the following code in python:
The input file is a file I saved in grayscale (8 bit). the line where you call cv is crucial to get the right format, although I dont know what its for. I think it makes foreground into 'probable foreground' which seems weird

import imageio as ii
import numpy as np
import cv2 as cv

mask = ii.imread("HE_mask_greyscale.png")
mask = mask > 0
mask = np.where(mask, cv.GC_PR_FGD, cv.GC_PR_BGD).astype(np.uint8)

ii.imsave("HE_mask_output.png", mask)

Oh yeah before I forget; setting margin: float = 0.0, did not remove the border... Maybe I can solve it by giving the image a white border, and then masking it

@NicolaasVanRenne
Copy link
Author

update; I understand now what setting the margin to 0 does. It does work for removing the added border after the initial cropping.

I guess the key message here is to always use a tissue mask. (my original question was to remove the colorized border that was added like it was a gene expression area, if you dont use tissue mask). So please ignore all of this.

@ludvb
Copy link
Owner

ludvb commented Nov 6, 2023

Awesome, glad you got it to work!

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

No branches or pull requests

2 participants