I have a large number of images which I\'ve broken down into segments such that their matrices look like:
img = [ 1 1 1 1 1 2 2 2 3 3 3 3
1 1 1 1 2
A simple (but probably not maximally efficient) solution is to dilate each region mask to pick neighbors:
labels = unique(img);
nLabels = length(labels);
neighbors = cell(nLabels,1);
for iLabel = 1:nLabels
msk = img == labels(iLabel);
adjacentPixelMask = imdilate(msk,true(3)) & ~msk;
neighbors{iLabel} = unique(img(adjacentPixelMask));
end
neighbors{1}
ans =
2
4
5