Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

rules: -the 1's are in 'connected lines' if there are adjacent 1's (including diagonally) e.g.:

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)

  • the 'connected lines need to be at least 3 elements long. So in the 7x7 example, there would only be one line that matches this criteria. If a(7,3) was set to 1, then there would be a connected line at the bottom left also

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.

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
like image 422
CaptainObv Avatar asked Oct 19 '22 13:10

CaptainObv


1 Answers

If you have image processing toolbox, try bwareaopen

b = bwareaopen(a, 3);

Sample Run #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:

>> 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
like image 162
Santhan Salai Avatar answered Oct 27 '22 20:10

Santhan Salai