Teichlab / bin2cell

Join subcellular Visium HD bins into cells
MIT License
50 stars 1 forks source link

scaled_he_image error #18

Open lilyche26 opened 3 weeks ago

lilyche26 commented 3 weeks ago

Hi, Thank you for developing the amazing software bin2cell. I am currently using bin2cell with my Visium HD 2µm data. While I successfully ran the pipeline on two samples, I encountered an error with my one liver sample during the “b2c.scaled_he_image(adata, mpp=mpp, save_path='stardist/he.tiff')” step. (Sell below)

image001

The image appears normal (1.7 GB) and is the input used in Spaceranger (via --image). I’ve attempted to resolve the issue by adjusting the mpp (trying values of 0.3, 0.4, 0.5, 0.8, 1.0, 1.8, 2.0, etc.) and reducing the image size, but unfortunately, none of these solutions have worked. I would greatly appreciate any guidance or suggestions you might have to help resolve this issue. Thank you for your assistance!

ktpolanski commented 3 weeks ago

Alright, let's do some diagnostics.

img = b2c.load_image(source_image_path)
img.shape
np.max(adata.obsm["spatial"], axis=0)
lilyche26 commented 3 weeks ago

Thank you for the quick response. When i load the image by "b2c.load_image(source_image_path)" it raise the same error. 1728510370040

ktpolanski commented 3 weeks ago

You stumbled upon some strange cv2 error. I found this, not that it explicitly helps you.

import cv2
img = cv2.imread(source_image_path)
img.dtype

It's very strange that it loads but then explodes when attempting to reorder the channels.

lilyche26 commented 3 weeks ago

I am unable to read the image using cv2 "cv2.imread(source_image_path)"—the same error occurs. It seems this issue is related to the H&E image itself. I will discuss this with my lab scientist to check the image. Thank you for your help!

ktpolanski commented 3 weeks ago

What happens when you try to cv2.imread()?

lilyche26 commented 3 weeks ago

raise the same error 1728512345583

ktpolanski commented 3 weeks ago

Ah, I see. Sorry, it's quite late here.

You could technically try experimenting with PIL:

from PIL import Image
img = np.array(Image.open(source_image_path))

If this whines about the image being too big run Image.MAX_IMAGE_PIXELS = None, but I think it should remember that from being set up within b2c.

lilyche26 commented 3 weeks ago

This time works. 1728512881508 You can response me tomorrow. Thank you

ktpolanski commented 3 weeks ago

Interesting, why are there 4 channels in this thing? What happens if you try to load your other images that worked before, be it via cv2 or PIL?

lilyche26 commented 3 weeks ago

Hi Krzysztof, I used "cv2" to load my other images that worked previously, not use PIL.

Now, I’ve used PIL to load the two sample images that worked, and they both have 4 channels. Please see below: image image These two samples worked and I was able to run bin2cell successfully following the tutorial.

ktpolanski commented 3 weeks ago

Alright, some things are starting to come together. 21035 22993 4 = 1934631020, which is one of the numbers mentioned in the error message. Another number mentioned in the error message is 1073741824, which is 2^30, which is the default maximum pixel count cv2 works with. Thing is, I do os.environ["OPENCV_IO_MAX_IMAGE_PIXELS"] = str(2**40) to override this before loading cv2, and I've definitely loaded images far larger than yours. I was developing b2c on a BTF that's over 50,000 pixels in each dimension, and weighs almost 10GB.

I get a different, more sensible error when I try to load my image without flipping the setting (and everything loads fine once configured properly):

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.10.0) /io/opencv/modules/imgcodecs/src/loadsave.cpp:79: error: (-215:Assertion failed) pixels <= CV_IO_MAX_IMAGE_PIXELS in function 'validateInputImageSize'

The exact thing you're running into remains very arcane, I found this while trying to dig more stuff up. The fact you're seeing four channels when you PIL it in might be sending it to a different part of the cv2 library somehow where that setting has no power? What comes up when you identify the image via ImageMagick?

$ identify Visium_HD_Mouse_Brain_tissue_image.tif
Visium_HD_Mouse_Brain_tissue_image.tif TIFF 18872x23947 18872x23947+0+0 8-bit sRGB 137.865MiB 0.000u 0:00.033

As for proceeding, I think the most robust way would be for me to add an optional way to just pass the full resolution loaded image into the custom resolution functions.

lilyche26 commented 3 weeks ago

Hi Thank you for clarify, and set "os.environ["OPENCV_IO_MAX_IMAGE_PIXELS"] = str(2**40)" in bin2cell. I’m unable to open all my images with ImageMagick, even the ones that previously worked. I receive an error stating that the width and height exceed the limit. image

Actually, I successfully ran the entire bin2cell pipeline using two 10x Visium HD demo datasets (samples P1 and P3, available at https://www.10xgenomics.com/products/visium-hd-spatial-gene-expression/dataset-human-crc). The BTF images for those samples are much larger, at 12.6 GB (sample P1) and 10.4 GB (sample P3). The problematic sample image is only 1.7 GB, which is puzzling.

$ identify Visium_HD_Human_Colon_Normal_P3_tissue_image.btf Visium_HD_Human_Colon_Normal_P3_tissue_image.btf[0] TIFF64 80562x42537 80562x42537+0+0 8-bit sRGB 9.72097GiB 0.000u 0:00.036 Visium_HD_Human_Colon_Normal_P3_tissue_image.btf[1] TIFF64 36x24 36x24+0+0 16-bit Grayscale Gray 0.000u 0:00.000 $ identify Visium_HD_Human_Colon_Cancer_P1_tissue_image.btf Visium_HD_Human_Colon_Cancer_P1_tissue_image.btf[0] TIFF64 71106x58791 71106x58791+0+0 8-bit sRGB 11.7081GiB 0.000u 0:00.030 Visium_HD_Human_Colon_Cancer_P1_tissue_image.btf[1] TIFF64 32x32 32x32+0+0 16-bit Grayscale Gray 0.000u 0:00.000

Do you think I should try saving the image in BTF format to see if that resolves the issue?

nadavyayon commented 3 weeks ago

Hi, it does seem something is off with the images... can you try to open these in fiji https://imagej.net/software/fiji/downloads and have a look if it's an RGB image as expected? If not you can convert to rgb and save as a tiff. It's a long shot but in case you didn't try something similar already.

lilyche26 commented 3 weeks ago

Hi, thank you for your great suggestion, yes, i just notice that the image is not RGB. It can not open in fiji, i use GIMP ([https://www.gimp.org]) to open it and export to a new tiff, now the pipeline works! Thank you so much for you and Krzysztof help!

liss-parad commented 3 days ago

Alright, some things are starting to come together. 21035 22993 4 = 1934631020, which is one of the numbers mentioned in the error message. Another number mentioned in the error message is 1073741824, which is 2^30, which is the default maximum pixel count cv2 works with. Thing is, I do os.environ["OPENCV_IO_MAX_IMAGE_PIXELS"] = str(2**40) to override this before loading cv2, and I've definitely loaded images far larger than yours. I was developing b2c on a BTF that's over 50,000 pixels in each dimension, and weighs almost 10GB.

I get a different, more sensible error when I try to load my image without flipping the setting (and everything loads fine once configured properly):

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.10.0) /io/opencv/modules/imgcodecs/src/loadsave.cpp:79: error: (-215:Assertion failed) pixels <= CV_IO_MAX_IMAGE_PIXELS in function 'validateInputImageSize'

The exact thing you're running into remains very arcane, I found this while trying to dig more stuff up. The fact you're seeing four channels when you PIL it in might be sending it to a different part of the cv2 library somehow where that setting has no power? What comes up when you identify the image via ImageMagick?

$ identify Visium_HD_Mouse_Brain_tissue_image.tif
Visium_HD_Mouse_Brain_tissue_image.tif TIFF 18872x23947 18872x23947+0+0 8-bit sRGB 137.865MiB 0.000u 0:00.033

As for proceeding, I think the most robust way would be for me to add an optional way to just pass the full resolution loaded image into the custom resolution functions.

Hello, I've run into this issue too. Could you please tell me how to configure the parameters?

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.10.0) /io/opencv/modules/imgcodecs/src/loadsave.cpp:79: error: (-215:Assertion failed) pixels <= CV_IO_MAX_IMAGE_PIXELS in function 'validateInputImageSize'

Thank you for your response.

ktpolanski commented 3 days ago

You've ran into a different issue, not the same one as OP.

I was illustrating a more common cv2 problem, and how to fix it. This fix is already worked into bin2cell. Are you loading cv2 before you load b2c?

liss-parad commented 3 days ago

Hi,I adjusted the loading order of the modules, placing bin2cell at the front, and now it runs normally. Thank you so much for you help.