【问题标题】:Matlab: Matrix Neighbour ExtractionMatlab:矩阵邻域提取
【发布时间】:2014-10-18 16:09:27
【问题描述】:

我有大量的图像,我将它们分解成多个片段,它们的矩阵看起来像:

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,依此类推。

我已将所有区域提取到单独的单元格中,并获得了统计数据(均值、方差等),我计划使用这些统计数据将区域与一定容差内的统计数据合并。我正在努力寻找一种有效的方法来获取每个区域的邻居以允许合并发生。

我有一个糟糕的解决方案,即使是一张图片也需要很长时间:

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

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

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

【问题讨论】:

  • 您的代码无法按原样运行。您应该定义所有变量,以便我们可以将其粘贴到 Matlab 命令窗口并获得结果。或者:对于您的示例img,准确说明所需的输出是什么

标签: matlab image-processing matrix


【解决方案1】:

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

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

【讨论】:

  • @SeanTheStudent:我相信您使用的方法与我相同,但您以某种方式检查每个像素的邻居,而查看区域的邻居则更快。跨度>
  • 太棒了!每张图像从几分钟过渡到大约 20 秒;巨大的差异非常感谢!我对 Matlab 及其向量/矩阵方法有点陌生,所以感谢您的帮助!
  • @Jonas,很好!不仅要考虑最近的邻居(比如k=1),还要考虑每个超像素的邻居的邻居(k=2)呢?
  • @Tin:1 的邻居是unique(cat(1,neighbors{neighbors{1}}))。使用邻居数组查看邻居的邻居。
  • @Jonas,谢谢!因此,如果我想考虑k=3(邻居的邻居的邻居)或更高,那么我可以使用递归函数unique(cat(1,neighbors{neighbors{ii}})),其中ii 是邻居。你认为有更快的方法吗?
猜你喜欢
  • 2015-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-12
  • 2015-07-31
相关资源
最近更新 更多