1

I'm using Matlab. I have a 2-D Binary image/array. like this

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

I want to find out center of very first white block/Circle with respect to y-axis Answer of the above image will be.

0 1 0
1 1 1
0 1 0

Anyone who have have a simplest solution for this.

Muaaz Khalid
  • 2,199
  • 2
  • 28
  • 51
  • 1
    This is a specific case of [general method to find submatrix in matrix](http://stackoverflow.com/questions/18831011/general-method-to-find-submatrix-in-matlab-matrix) Hence the answers there should be able to help you as well though the ones here seem to be simpler. You may also be interested in the [findsubmat - File Exchange Submission](http://www.mathworks.nl/matlabcentral/fileexchange/23998-findsubmat) – Dennis Jaheruddin Feb 04 '14 at 15:30
  • This pattern may vary.. it could be 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 – Muaaz Khalid Feb 04 '14 at 15:35
  • 1
    Note that if you want to add information about your question, the best way is to edit it instead of writing a comment. – Cape Code Feb 04 '14 at 15:37
  • no, it's not required. its for a specific case.. I want a general solution that finds first circle. its current shape is like a diamond, there might be more than two diamonds... and shape can be vary time to time.. – Muaaz Khalid Feb 04 '14 at 15:38
  • Please indicate if you are interested in *circles* or just blobs of connected pixels, in the later case check the `bwconncomp` function. – Cape Code Feb 04 '14 at 16:09
  • connected component will be ok.. – Muaaz Khalid Feb 04 '14 at 19:03
  • I have this image http://s24.postimg.org/n1r2kg0bl/image.png I want to get first block of 1's (White color) with respect to y-axis..(exclude x-axis) This may be a circle or a block – Muaaz Khalid Feb 04 '14 at 19:52

2 Answers2

1

If you are looking for exact matches of the template, you can use a moving filter, one example is:

H=[0 0 0 0 0 0 0 0 0 0 0 0 0;
   0 0 0 0 0 0 1 0 0 0 0 0 0;
   0 0 0 0 0 1 1 1 0 0 1 0 0;
   0 0 1 1 0 0 1 0 0 0 0 0 0;
   0 0 0 1 0 0 0 0 0 0 0 0 0;
   0 0 0 0 0 0 1 0 0 0 0 0 0;
   0 0 0 0 0 0 1 1 0 0 0 0 0;
   0 0 0 0 0 0 1 0 0 0 0 0 0];

b=[0 1 0;
   1 1 1;
   0 1 0];

C=filter2(b,H, 'same');
[x,y]=find(C==max(max(C)));

x and y are the locations of your template in the order that it appears from the top left corner of your array.

Edit: if you have the Image Processing Toolbox and are looking for a less strict way of finding objects that have a roughly circular shape you can use regionprops with the 'Centroid' and 'Eccentricity' arguments with the bwconncomp function.

ObjectStats=regionprops(bwconncomp(H,4), 'Centroid', 'Eccentricity');

Objects with an 'Eccentricity' of 0 (or close to 0) will be the circles.

idx=find(cell2mat({ObjectStats.Eccentricity})==0); % Change ==0 to <0.2 or something to make it less strict.
ctrs={ObjectStats.Centroid};

>> ctrs{1,idx(1)}

ans =

     7     3

Note that in your case, a lone pixel is an object with an eccentricity of 0, it is the smallest 'circle' that you can find. If you need to define a minimum size, use the 'Area' property of regionprops

Cape Code
  • 3,584
  • 3
  • 24
  • 45
1

You can do this with a simple 2 dimensional convolution. It will "overlay" the filter along a larger matrix and multiply the filter by the values it is overlaying. If the product is equal to the sum of the filter, then you know you found a match.

Here is some simple code.

mat = [0 0 0 0 0 0 0 0 0 0 0 0 0
       0 0 0 0 0 0 1 0 0 0 0 0 0
       0 0 0 0 0 1 1 1 0 0 1 0 0
       0 0 1 1 0 0 1 0 0 0 0 0 0
       0 0 0 1 0 0 0 0 0 0 0 0 0
       0 0 0 0 0 0 1 0 0 0 0 0 0
       0 0 0 0 0 0 1 1 0 0 0 0 0
       0 0 0 0 0 0 1 0 0 0 0 0 0];

filt = [0 1 0
        1 1 1
        0 1 0];

[row,col] = find(conv2(mat,filt,'same') == sum(filt(:)))
MZimmerman6
  • 8,445
  • 10
  • 40
  • 70
  • I have this image s24.postimg.org/n1r2kg0bl/image.png I want to get first block of 1's (White color) with respect to y-axis..(exclude x-axis) This may be a circle or a block – Muaaz Khalid Feb 05 '14 at 14:42
  • @muaaz First, I recommend not posting the same comment multiple times. Second, you would just need to adjust the filter a little bit to make it match the shape you are looking for. That is on you. The process would be the same, and it would not require any additional toolboxes. You also need to define what you mean by first, first when scanning left to right, or first scanning top to bottom? MATLAB will give you the index columnwise – MZimmerman6 Feb 05 '14 at 16:03