检查邻居像素Matlab [英] Check neighbour pixels Matlab

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

问题描述

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

I have a A which is 640x1 cell. where the value of each cell A(i,1) varies from row to row, for example A(1,1) =[], while A(2,1)=[1] and A(3,1)=[1,2,3].
There is another matrix B of size 480x640, where the row_index (i) of vector A corresponds to the col_index of matrix B. While the cell value of each row in vector A corresponds to the row_index in matrix B. For example, A(2,1)=[1] this means col_2 row_1 in matrix B, while A(3,1)=[1,2,3] means col_3 rows 1,2&3 in matrix B.
What I'm trying to do is to for each non-zero value in matrix B that are referenced from vector A, I want to check whether there are at least 4 other neighbors that are also referenced from vector A. The number neighbors of each value are determined by a value N.
For example, this is a part of matrix B where all the zeros"just to clarify, as in fact they may be non-zeros" are the neighbors of pixel X when 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的操作;否则执行G=0操作. 所以,如果有人可以请指教.并请让我知道是否需要进一步澄清.

As shown, because N=3, all these zeros are pixel X's neighbors. So if more than 4 neighbor pixels are found in vector A then do something e.g G=1 if not then G=0; So if anyone could please advise. And please let me know if any more clarification is needed.

推荐答案

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

The first thing I would do is to convert your cell of indices A to a logic matrix Amat. This makes it easier to check how many neighbours are included in A.

这是使用此转换的解决方案.我希望这些评论足以使它易于理解.

Here is a solution that uses this conversion. I hope the comments are enough to make it understandable.

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

以下是一次运行代码的打印输出.

The following is a printout from one run of the code.

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)

这篇关于检查邻居像素Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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