Calculate shortest distance between neighboring child object within a parent object along the parent object

I segmented (i) several protein attachment regions contained within (ii) bacterial cell membranes as visible on the attached image.

I now wish to to calculate the shortest distance between the center of the next-neighboring attachments regions (green outlines) contained within a bacterial membrane (purple outlines.)

Furthermore, I want the distance to be measured only within a membrane, not across membrane boundaries.

Does anyone know a way how to get this result?

Thanks and cheers,

Hi Kai,
CellProfiler will calculate neighbor measurements, but it won’t restrict it to only objects within the same parent, and it will draw the shortest straight line between any two objects, not calculate the distance along a predefined path.

I’ll kick this to my team to see if they have any ideas, but I can’t think of any good way to do this in high throughput off the top of my head; outside of doing a freehand drawing plus a measure step in something like ImageJ, I’m stumped.

Can you please clarify, @schlda00 - you are wanting to measure for each green object the closest other green object, but only within a given parent (purple)? This seems relatively feasible by adjusting the code in MeasureObjectNeighbors if you or someone can big into it!

And you don’t want a straight line but instead the line to follow the closest possible curvature of the in-between space in the membrane? That’s a little more complex but I bet also feasible within that module.

It’s such a unique sort of measurement I would be surprised if any standard toolboxes calculate this out of the box. My labmate @karhohs says “I think R has a good package for calculating distances.” so exporting objects from CP might make sense.

I wonder if something like this approach might work. If you were going to try it, you’d probably want to shrink all your objects by 1 pixel using ExpandOrShrinkObjects; it’ll ensure all the membrane objects don’t touch each other so that there’s NO path from a green object in one membrane to a green object in another membrane.

hi all, many thanks for your quick feedback and input!


yes indeed!

thats correct as well.

How would I do this within the module? Could it be done with the module as it is at the moment or would the modules code require changes?

Unfortunately I am kind of a rookie when it comes to coding, so I actually have no experience using R at all and on top of that wouldn’t really know how to make changes to the module.

However, if I would get something done in python with the help of a colleague, could that be integrated into the module more easily from your side?


That is a good idea, thanks for the tip!

How about this method?

  • Morph -> skeleton: Skeletonize the purple membrane so that the membrane is only a single pixel thick
  • MaskObjects: Mask the purple membrane with the green objects, thus breaking up the purple objects into multiple segments
  • MeasureObjectAreaShape on the purple segments
  • Use the Area feature to sort the lengths. Here you can choose the shortest length per bacterial cell. This step would be done outside CP, but could conceivably be done with Excel
1 Like

This works really well, thanks for the good idea!


I have seen this post and this is exactly what I want. I want to measure the distance between an object and the closest object but only within a given parent (cell). Is that possible? When you say “…by adjusting the code…”, you mean the code of the CellProfiler module?

Thanks a lot.

Hi @juanje,
I think you can first use the module “MaskObjects” to limit the measurements to within the cells and then use " MeasureObjectNeighbors" to measure the spatial relation of objects within the cells.

Hello CellKai,

Thank you very much for your help. Yes, that’s exactly what I was doing, but I still have the same problem. The “MeasureObjectNeighnors” module gives you the distance between two objects independently of the parent object. So some times it gives me the distance between two close objects in different cells. I haven’t found a way to solve it.

Thanks a lot.



thats indeed true.
Maybe there is a way to solve the problem not in CellProfiler but further downstream.

For example, you can look in you results.txt per Object and crosscheck the column listing the distances between neighboring objects with the ones that list the corresponding parent objects. If you sort them by distance and only count objects that have the same parent, you should arrive at your desired quantity.

So I believe the measurements are there, its just a bit more complicated to sort them accordingly.

Yes, you are totally right. The measurements are there and I just need to sort them properly. That’s what I’m doing. It’s a little painful, but that’s ok.

Thank you very much for your help.