简体   繁体   English

Matlab:保持非零矩阵元素彼此相邻并忽略单独的元素

[英]Matlab: keeping non-zero matrix elements adjacent to each other and ignoring lone elements

Here is an example matrix (but the result shouldn't be constrained to only working on this): 这是一个示例矩阵(但结果不应仅限于此处):

a=zeros(7,7);
a(5,3:6)=1;
a(2,2)=1;
a(2,4)=1;
a(7,1:2)=1

a=
0 0 0 0 0 0 0
0 1 0 1 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 1 1 1 1 0
0 0 0 0 0 0 0
1 1 0 0 0 0 0

I want to get rid of all the 1's that are alone (the noise), such that I only have the line of 1's on the fifth row. 我想摆脱所有单独的1(噪音),这样我在第五行只有1行。

rules: -the 1's are in 'connected lines' if there are adjacent 1's (including diagonally) eg: 规则: - 如果有相邻的1(包括对角线),则1在'连接线'中,例如:

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

(The connected lines are what I want to keep. I want to get rid of all the 1's that are not in connected lines, the connected lines can intersect each other) (连接线是我想要保留的。我想摆脱所有不在连接线上的1,连接线可以相互交叉)

  • the 'connected lines need to be at least 3 elements long. '连接线需要至少3个元素长。 So in the 7x7 example, there would only be one line that matches this criteria. 因此,在7x7示例中,只有一行符合此条件。 If a(7,3) was set to 1, then there would be a connected line at the bottom left also 如果a(7,3)设置为1,那么左下角也会有一条连线

I am currently looking at this through a column by column approach, and here is the first draft of my code so far: 我目前正在通过逐列方法来看这个,这是我的代码到目前为止的第一个草稿:

 for nnn=2:6
        rowPoss=find(a(:,nnn)==1);
        rowPoss2=find(a(:,nnn+1)==1);


        for nn=1:length(rowPoss)
            if myResult(rowPoss(nn)-1:rowPoss(nn)+1,n-1)==0 %
                %then?
            end
        end
    end

My difficulty is, during this column by column process, I'd have to enable a way to recognise the beginning of the connected line, the middle of the connected line, and when a connected line ends. 我的困难在于,在此列的逐列过程中,我必须启用一种方法来识别连接线的开始,连接线的中间以及连接线的结束时间。 The same rules for this, when applied to noise (the lone 1's), would just ignore the lone 1's. 当应用于噪声(单独的1)时,相同的规则将忽略单独的1。

The output I want is basically: 我想要的输出基本上是:

 b=
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 1 1 1 1 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0

If you have image processing toolbox, try bwareaopen 如果您有图像处理工具箱,请尝试bwareaopen

b = bwareaopen(a, 3);

Sample Run #1: 示例运行#1:

>> a

a =

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

>> b

b =

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

Sample Run #2: 示例运行#2:

>> a

a = 

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

>> b

b =

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM