5

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
CaptainObv
  • 360
  • 1
  • 4
  • 15

1 Answers1

3

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
Santhan Salai
  • 3,888
  • 19
  • 29
  • I will reply back if I find any scenarios where this doesn't work, but it seems right now this works with all the cases I have at the moment, so thank you! – CaptainObv Apr 27 '15 at 06:41
  • @CaptainObv Ok sure. Once you feel like the problem is solved, consider accepting the answer. – Santhan Salai Apr 27 '15 at 06:43
  • My initial concern was that there seems to be a dependence on the length of the connected string and that a connected string has 1's adjacent to each other. In my example above, everything happened to conveniently be resolved using image processing with a 3x3 kernel as you've shown. I was hoping there would be some flexibility between the two constraints e.g. change some parameters so you can look two rows up and down (instead of 1) in the previous column a=zeros(7,7); a(2,2)=1; a(2,4)=1; a(7,1:2)=1; a(7,7)=1; a(5,6)=1; a(3,5)=1 gives: a=zeros(7,7) a(2,4)=1; a(7,7)=1; a(5,6)=1 a(3,5)=1 – CaptainObv Apr 27 '15 at 07:21
  • 1
    @CaptainObv - `bwareaopen` performs a connected components analysis by identifying connected regions and giving them an ID. Any connected regions that are less than 3 (in this example) get removed. It's independent of any image processing kernels. – rayryeng Apr 27 '15 at 07:23
  • @rayryeng, Thanks for answering the comment. I'm not so good with Image Processing. Also I got this function from one of your answers only. [Here](http://stackoverflow.com/a/29325707/2111163) is that answer. :) – Santhan Salai Apr 27 '15 at 07:29
  • @CaptainObv If you wanna look two rows up or down, this might not be a good function. Edit the example with expected results separately so that some one here, might give you the generic solution. – Santhan Salai Apr 27 '15 at 07:33
  • 1
    @SanthanSalai - Ah! I remember that question. It was a very good question :) No worries. Never too late to learn! – rayryeng Apr 27 '15 at 07:47