SydneyBioX / BIDCell

Biologically-informed deep learning for cell segmentation of subcelluar spatial transcriptomics data
Other
35 stars 5 forks source link

Error in cv2.resize when using CellPos nuclei segmentation #15

Closed yuerua closed 5 months ago

yuerua commented 5 months ago

Hi team, thank you for making this great tool! I tested it on some in-house Xenium samples, and for a few images it threw the error below:

File "/BIDCell/bidcell/processing/nuclei_segmentation.py", line 26, in segment_dapi
mask, _, _, _ = model.eval(img, diameter=diameter, channels=channels)
cv2.error: OpenCV(4.9.0) :-1: error: (-5:Bad argument) in function 'resize'
> Overload resolution failed:
>  - src data type = uint32 is not supported
>  - Expected Ptr<cv::UMat> for argument 'src'

This is due to an internal error in this specific version of CellPos, which occurs to images with values over 65,535, as stated in this post

As a temporary solution, I changed line 727 in site-packages/cellpose/transforms.py to below imgs = cv2.resize(img0.astype("float32"), (Lx, Ly), interpolation=interpolation).round().astype("uint32")

This change bypassed the CV2 error. However, it also caused the nuclei segmentation to generate an empty mask, leading to an error in preannotate.py

 File "/BIDCell/bidcell/processing/preannotate.py", line 146, in preannotate
cell_type_col = cell_df["cell_type"].to_numpy()
UnboundLocalError: local variable 'cell_df' referenced before assignment

Is there a better solution to this?

yuerua commented 5 months ago

This resizing error is solved with this commit in CellPos.

xhelenfu commented 5 months ago

Hello, thanks for raising this issue. If I understand correctly, this error is generated when there are more than 65,535 nuclei detected in an input image. A potential workaround is to add a couple of parameters under nuclei, in the yaml file, for example:

nuclei:
  diameter: # estimated diameter of nuclei for Cellpose - or None to automatically compute, default: None
  max_height: 4000
  max_width: 4000

In this example the input DAPI size to Cellpose is limited to a 4000x4000 patch. BIDCell will do patching and stitching of the nuclei internally. There should be fewer nuclei in smaller patches and hopefully should overcome this problem. Hope this helps