SydneyBioX / BIDCell

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

how to draw cell boundaries from the output data #11

Closed shriyasmith closed 3 months ago

shriyasmith commented 4 months ago

Hi team, congratulations to the beautiful work! I wonder how you draw the cell boundaries from the BIDcell output files? I'd appreciate a walkthrough that includes the steps for reading the files in the output directory, processing the data within, and then using that data to draw the cell boundaries. Thanks!

xhelenfu commented 4 months ago

Hi, thanks for your question. The output segmentation is a .tif file (with a name such as epoch_1_step_4000_connected.tif), where each cell has its own cell ID value. To visualise cell boundaries, you can use something like

for cell_id in all_cell_ids:
    border = np.zeros((size, size), dtype=np.uint8)
    cell_id = int(cell_id)
    mask = np.where(segmentation == cell_id, 1, 0).astype(np.uint8)
    contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_L1)
    border = cv2.drawContours(image=border, contours=contours, contourIdx=-1, color=int(cell_id), thickness=1, lineType=cv2.LINE_8)
    border[border>0] = cell_id
shriyasmith commented 4 months ago

Thanks, I will give it a try!

diala-ar commented 2 months ago

Hi @shriyasmith where you able to draw the cell boundaries using bidcell output? I am not sure how to run the code given above:

  1. Where should we get all_cell_ids from? I am getting them from the cell_id column of the expr_mat.csv file located in the cell_gene_matrices/2024_03_28_11_27_18 folder. So those are integers from 1 to the number of cells detected in your image, is this correct?
  2. I am assuming to set size variable to the height and width of the DAPI image taken from the bidcell log file
  3. What should I set segmentation to?
  4. I am not sure what to expect, I mean in which file should the boundaries be drawn?
  5. Where you able to see bidcell output in Xenium explorer?

On running the code below, I am getting an error that segmentation is not defined...

for cell_id in all_cell_ids:
    border = np.zeros((13699, 22797), dtype=np.uint8)
    cell_id = int(cell_id)
    mask = np.where(segmentation == cell_id, 1, 0).astype(np.uint8)
    contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_L1)
    border = cv2.drawContours(image=border, contours=contours, contourIdx=-1, color=int(cell_id), thickness=1, lineType=cv2.LINE_8)
    border[border>0] = cell_id

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
NameError: name 'segmentation' is not defined

I appreciate if you could share your experience.

Thanks.