Nuclei segmentation and assignment of spots to segmented objects

Sample image and/or code

index

Background

im trying to segment nuclei stained images from in situ sequencing experiments and then assign spots with in the same coordinate system to the segmented cells.

questions

is there any good way to do this? i attached an image above for what the output from the segmentation looks like. could i get some polygons from these segmentation results that i can use to assign spots with x and y coordinates to?

Hi @Christoffer_Langseth I’m not sure of what you want to do, do you want to attribute each pixel to one of the nuclei, and if yes, according to which criterion (distance to the nucleus?) ? If you want to perform a Voronoi tesselation, you can for example use the watershed algorithm with a uniform elevation map and one label per nuclei. For example

import numpy as np
from skimage import segmentation, measure
l = 500
rows, cols = np.random.randint(l, size=(2, 100))
nuclei_mask = np.zeros((l, l), dtype=np.bool)
nuclei_mask[rows, cols] = 1
nuclei = measure.label(nuclei_mask)
tesselation = segmentation.watershed(np.ones_like(nuclei_mask), nuclei)
import matplotlib.pyplot as plt
plt.imshow(tesselation)
plt.plot(cols, rows, 'o')
import plotly.express as px
fig = px.imshow(tesselation)
fig.add_scatter(x=cols, y=rows, mode='markers', marker_color='blue')
fig.show()

(I included visualization code both for matplotlib and plotly, matplotlib is more common but plotly is more interactive out of the box in a notebook, keep the one you prefer!)
image

hello emmanuelle! thank you so much for your answer. im sorry for not being very specific.

essentially my data comes from fluorescence microscopy in which we have images from tissues. we have labeled both the cells in the tissue as well as genes that are expressed in the tissue. this gives me one image with the cells and a coordinate system with the identity of the gene expressed, in addition to its position, in x and y coordinates. i have used starfish before which have been great, but now id like to use skimage instead. essentially, i want to segement the cells in the tissue, expand the cells 10% and then based on that assign the genes expressed to the expanded cells. so the assignment would be made if the position of the gene falls into this the area of the expanded cell.

Thanks for the information. I still don’t understand everything, not being from this domain. Is there a specific starfish tutorial which could help understanding better what you are doing?

Expanding the cells can be done by mathematical morphology (skimage.morphology.dilation, which works on arrays of labels). If you need 10% of each cell rather than a constant dilation for all cells then you’ll need first to compute the area of each cell (skimage.measure.regionprops). If you have a position (row, col) for the gene then cells[row, col] will tell you the index of the cell to which to attribute the corresponding gene.

I totally understand! Here is a better example of what my data looks like: https://drive.google.com/file/d/1ngwa1GKyoiR1a0jpWzdsO6CgczH2mGAG/view?usp=sharing
I have the nuclei as the background image and every color signifies a gene of a certain kind.

Thank you for the input on the expansion of the cells. I managed to find the region props so I have the centroid of the cell as well as the area. So, would you know how I would go from the expanded centroids and then assign the spots based on the x and y coordinates?

So what you would like to have is, for each cell, a list of the genes which are expressed in this cell?

Yes, precisely. So each gene spot has an id associated with type and coordinates and I would simply like to add to which cell it belongs to. The cell information, the centroid location and area I have.

Note that you don’t want dilation, since that might cause issues when nuclei are very close together. Instead, the new function skimage.segmentation.expand_labels, which will be present in 0.18.0 which should be out in the next few days, is exactly what you want!

Once you have the image with the expanded labels, you can do:

from scipy import ndimage as ndi

cell_labels = ndi.map_coordinates(
        expanded_labels,
        spot_coords.T,  # assuming spot coords has shape (n, 2)
        order=0,
        )
2 Likes

Thank you so much! Will give it try!

The assignment of the spots to the segmented cells worked great. Thank you so much for all of the help guys!

Now I’m eagerly waiting for 0.18.0, @jni !

Have a great day!

1 Like

Awesome! Great to hear. 0.18.0rc0 is out now and you can try it with pip install --pre -U scikit-image! 0.18 should be out… Probably wednesday, as I’m working on a workshop for tomorrow! :sweat_smile: