# Calculate overlap between individual objects from different images

Hello!

Background
I’ve started using scikit-image to perform segmentation and labeling of axons and synaptic puncta. I create labels for axons using edge-detection and watershed. I repeat this process for synaptic puncta with different parameters (i.e. lower_limit, min_size, etc.)

``````image = axons[0:200, 0:200]

foreground, background = 1, 2
markers = np.zeros_like(image)
markers[image < lower_limit] = background
markers[image > upper_limit] = foreground

ws = watershed(sobel(image), markers)
seg = label(ws == foreground)

objects = skimage.measure.regionprops(seg)

#size filter
filter = np.arange(np.max(seg) + 1)
for object in objects:
if not (min_size <= object.area <= max_size):
filter[object.label] = 0

labels = filter[seg]

#remove labels touching border
labels = skimage.segmentation.clear_border(labels)
``````

In the image below, I’ve overlaid the labeled puncta (green) on top of labeled axons (red):

Challenge
I’m interested in performing analysis on puncta that are within axons. Puncta that overlap at least 50% with an axon would be classified as within that axon. Does scikit-image have any built-in function for a “filter” like this? Maybe a way to iterate through each puncta and check for this condition?

Any help or advice is greatly appreciated! Thank you!

Hi @kamodulin welcome to the forum! Could you please give more details about what you mean with “overlap at least 50%”? Is the overlap about the fraction of pixels (ie 50% of the pixels of a puncta should be also part of a given axon) or about the boundary (50% of the boundary points should touch an axon)? In all cases you will have to write a for loop on the puncta I think.

Hi @emmanuelle, thank you! Yes, the former is exactly right! 50% of a puncta’s pixels have to be in an axon for it to be considered within that axon.

I agree and I think my main issue is that I don’t know how to construct that for loop.

Here’s a coarse outline of how I would do this:

• create the binary mask (and) of the overlap regions (let’s call this `overlap_mask`)
• create a label image from the punctae (let’s call this `punctae_labelled`)
• use `regionprops` or (even better) `regionprops_table`. Provide `punctae_labelled` as label image and `overlap_mask` as intensity image.
• you can the look for regions with `mean_intensity` >= 0.5. You can iterate through the results from `regionprops`. Alternatively (if you used `regionprops_table`) turn this into a pandas DataFrame (a table) and filter based on the `mean_intensity` column.

Again, this is a coarse outline but it should provide some pointers.

1 Like

@VolkerH, that is brilliant and thank you very much for the guidance!

1 Like