Matlab: Matrix Neighbour Extraction

前端 未结 1 1275
滥情空心
滥情空心 2021-01-20 15:06

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         


        
相关标签:
1条回答
  • 2021-01-20 15:21

    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
    
    0 讨论(0)
提交回复
热议问题