Matlab:矩阵邻域提取 [英] Matlab: Matrix Neighbour Extraction

查看:1362
本文介绍了Matlab:矩阵邻域提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量的图片,我将其划分为多个段,使得它们的矩阵看起来像:

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 2 2 2 2 3 3 3  
        1 1 1 4 4 4 2 2 2 3 3 3  
        5 5 5 5 5 5 5 2 2 3 3 3 ];

其中每个数字代表不同的区域,每个区域都是任意形状。所以在这种情况下,区域1有邻居2,4和5,区域2有邻居1,3和4,依此类推。

where each number represents a different region and each region is arbitrarily shaped. So in this case, region 1 has neighbours 2, 4 and 5, region 2 has neighbours 1, 3 and 4 and so on.

我已经提取了所有的将区域划分为单独的单元格并获得统计数据(均值,方差等),我计划将其用于在一定容差范围内合并具有统计数据的区域。我正在努力想出一种有效的方法来获得每个地区的邻居以允许合并发生。

I've extracted all of the regions into separate cells and obtained statistics (mean, variance, etc) which I plan to use to merge regions with statistics within a certain tolerance. I'm struggling to think of an efficient way to obtain the neighbours of each region to allow that merging to occur.

我有一个可怕的解决方案需要很长时间即使是一张图片:

I have a horrible solution which takes a very long time for even one image:

referenceImage = [ 1 1 1 1 1 2 2 2 3 3 3 3;
                    1 1 1 1 2 2 2 2 2 3 3 3;
                    1 1 1 4 4 4 2 2 2 3 3 3;
                    5 5 5 5 5 5 5 2 2 3 3 3];

% Wish to extract each region into a separate cell
lastSP = 5;
sps = 1:lastSP;
% Could be a way to vectorise the below loop but it escapes me
superPixels(lastSP) = struct('Indices', 0, 'Neighbours', 0);
% Split data into separate cells
parfor a = 1 : lastSP
    inds = find(referenceImage == sps(a));
    superPixels(a).Indices = inds;
end

szs = size(referenceImage); % Sizes of RGB Image
for a = 1 : lastSP + 1
    mask = zeros(szs(1), szs(2)); % Just bin mask wanted
    mask(superPixels(a).Indices) = 1; % Mark the region pixels as one
    mask = xor(bwmorph(mask, 'thicken'), mask); % Obtain the outlying regions

    inds = find(mask ==1); % Fetch the external region indices

    neighbours = []; % Have to dynamically grow neighbours matrix
    neigh = 1;  

    for b = 1 : length(inds)
        found = false;
        if ~isempty(neighbours) % Check neighbours first
            for c = 1 : length(neighbours)
                if any(superPixels(neighbours(c)).Indices == inds(b))
                    found = true;
                    break;
                end
            end
        end

        if ~found
           for c = 1 : lastSP + 1 % Check every other region
               if any(superPixels(c).Indices == inds(b))
                    neighbours(neigh) = c;
                    neigh = neigh + 1;
                    break;
                end
           end
        end            
    end  
    superPixels(a).Neighbours = neighbours;
end

我想知道这是否真的是解决此问题的最佳方法。我知道最后一个循环是主要的问题,但我想不出另一种方法来合理地写这个,除非我递归并检查已知邻居的邻居。

I'm wondering if this is actually the best way to approach this problem. I know the very last loop is the main problem but I can't think of another way to reasonably write this, unless I recurse and check the neighbours of known neighbours.

任何帮助或推动正确的方向将不胜感激;谢谢!

Any help or nudges in the right direction would be greatly appreciated; thanks!

推荐答案

一个简单的(但可能不是最有效的)解决方案是扩展每个区域掩码以挑选邻居:

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

这篇关于Matlab:矩阵邻域提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆