检查邻居像素Matlab

时间:2013-08-25 06:20:25

标签: matlab matrix linear-algebra

我有A,即640x1单元格。其中每个单元格A(i,1)的值因行而异,例如A(1,1) =[]A(2,1)=[1]A(3,1)=[1,2,3]。 还有另一个大小为480x640的矩阵B,其中向量(i)的row_index A对应于矩阵B的col_index。虽然向量A中每行的单元格值对应于矩阵B中的row_index。例如,A(2,1)= [1]这意味着矩阵B中的col_2 row_1,而A(3,1)= [1,2,3]意味着矩阵中的col_3行1,2& 3 B
我要做的是为矩阵B中从向量A引用的每个非零值,我想检查是否至少有4个其他邻居也引用了向量A 。每个值的邻居数由值N确定  例如,这是矩阵B的一部分,其中所有的零“只是为了澄清,实际上它们可能是非零的”是X时像素N=3的邻居:

0   0   0   0   0   0   0
0   0   0   0   0   0   0
0   0   0   0   0   0   0
0   0   0   X   0   0   0
0   0   0   0   0   0   0
0   0   0   0   0   0   0
0   0   0   0   0   0   0

如图所示,因为N=3,所有这些零都是像素X的邻居。因此,如果在向量A中找到超过4个相邻像素,则执行某些操作,例如G=1,如果不是G=0; 所以如果有人可以请指教。如果需要进一步澄清,请告诉我。

2 个答案:

答案 0 :(得分:2)

我要做的第一件事就是将索引A的单元格转换为逻辑矩阵Amat。这样可以更轻松地检查A中包含的邻居数量。

以下是使用此转换的解决方案。我希望这些评论足以让它变得可以理解。

clear all
clc

nCols = 7;
nRows = 6;

N = 3; %// Number of neighbours
M = 4; %// Minimum number of wanted connections

%// Create cell of indices A
A = cell(nCols,1);
A{1} = [];
A{2} = 1;
A{3} = [1 2 3];
A{4} = [2 5];
A{5} = 3;
A{6} = [3 5];
A{7} = [1 4 6];

%// Generate radom data B
%// (There is a 50% probability for each element of B to be zero)
Bmax = 17;
B = (randi(2,nRows,nCols)-1).*(randi(Bmax,nRows,nCols));

%// Convert the cell A to a logic matrix Amat
Amat = zeros(size(B));
for ii = 1:nCols
    Amat(A{ii},ii) = 1;
end

A
B
Amat

for ii = 1:nCols
    for jj = A{ii}
        if B(jj,ii)>0

            %// Calculate neighbour indices with a lower bound of 1
            %// and an upper bound of nCols or nRows
            col_lim_low = max(1,ii-N);
            col_lim_high = min(nCols,ii+N);
            row_lim_low = max(1,jj-N);
            row_lim_high = min(nRows,jj+N);

            %// Get the corresponding neighbouring-matrix from Amat
            A_neighbours = ...
                Amat(row_lim_low:row_lim_high,col_lim_low:col_lim_high);

            %// Check the number of neighbours against the wanted number M
            if sum(A_neighbours(:)) > 1 + M
                %# do something
                fprintf('We should do something here at (%d,%d)\n',jj,ii)
            end
        end
    end
end

以下是一段代码的打印输出。

A = 

    []
    [         1]
    [1x3 double]
    [1x2 double]
    [         3]
    [1x2 double]
    [1x3 double]


B =

     1     5     0     0    11     0    16
     0    13    13     0     0     0     9
     0     0     0     5     0     0     0
     3     8    16    16     0     2    12
     0     0     5     0     9     9     0
    12    13     0     6     0    15     0


Amat =

     0     1     1     0     0     0     1
     0     0     1     1     0     0     0
     0     0     1     0     1     1     0
     0     0     0     0     0     0     1
     0     0     0     1     0     1     0
     0     0     0     0     0     0     1

We should do something here at (1,2)
We should do something here at (2,3)
We should do something here at (5,6)
We should do something here at (4,7)

答案 1 :(得分:1)

由于AB之间存在一对一的对应关系,因此无需在A上工作。 B是逻辑矩阵(如果未在A中引用则为0,如果引用则为1)。因此,您可以应用一个简单的filter2函数来计算8个最接近元素中的活动邻居数。

这是代码

B = rand(10,10);                %generate binary matrix
h = [1 1 1;1 0 1;1 1 1];        %filter to be applied
filter2(h,B,'same')>=4 & B>0    %apply filter on B, count minimum of 4 neighbors, if only B>1

修改

要将单元格数组B转换为二进制存在(0 =空,1 =非空),使用cellfun非常简单

B = ~cellfun(@isempty,B);

请参阅Armo's response上一个问题,了解如何根据A创建B.