Finding neighbors of an object from labeled image

10 views (last 30 days)
Philip on 1 Apr 2011
Commented: Alaa on 1 Mar 2016
I have used segmentation and labelling techniques to assign each object within an image a unigue number. For example, the sky will be given label 1, a tree may be given label 2, a person label 3, the ground label 4, and so on...
What I would like to do now is grab all the labels directly neighboring a specific object. For example, if I select the object of interest as label 3 (the person), I want to end up with a list of all the objects that the person directly neighbors. This would be the sky, and the ground (labels 1 and 4), but not the tree (label 2) as this is separated from the person.
Does anyone know of any good efficient methods for achieving this?
Thanks in advance!

Answers (1)

David Young
David Young on 1 Apr 2011
One possibility:
% assume labelled array is called "labelled"
label = 8; % label of object to find neighbours of
object = labelled == label;
se = ones(3); % 8-connectivity for neighbours - could be changed
neighbours = imdilate(object, se) & ~object;
neighbourLabels = unique(labelled(neighbours));
  1 Comment
Alaa on 1 Mar 2016
please,How can improve this to find each cell the number of neighbours cells of corneal cells to calculate hexagonality coefficient (pleomorphism) of cells. thanks in advance.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by