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


This works really well, thanks for the good idea!