This is a common problem when processing images. There are many options, such as a flood filling a region in an image, or finding which pixels belong to the same region. One general approach is to use the depth of the first search . The idea is that you move the image from left to right and from top to bottom, and for any encountered pixels equal to 1, you add them to the stack. For each pixel in your stack, you delete the stack, and then look at the neighboring pixels surrounding that pixel. Any pixels you add to the stack. You need to save an additional variable, where all the pixels that you have already visited , you do not add them to the stack. When the stack is empty, we found those pixels that are the whole region, so you mark them with a unique identifier. Then repeat this procedure until regions are highlighted in your image.
Thus, given that your matrix is stored in A , this is the main algorithm:
Initialize an array whose size is A logical . This will record which pixels we examined or visited. Also initialize the output array B to all zeros, which gives you all the connected components you are looking for. Any locations that are ultimately zero do not belong to any related components. Also, initialize an ID counter that keeps track of which related component label each of them has.
For each location that is in our matrix:
a. If location 0 , mark this location as visited and continue.
b. If we have already visited this place, continue.
with. If we have not visited this place ... go to step 3.
Add this invisible space to the stack.
a. Although this stack is not empty ...
b. Put this location from the stack.
with. If we visited this place, we will continue.
d. Else, mark this location as visited and mark this location with the identifier of the connected components.
e. Given this location, look at 8 adjacent pixels.
f. Delete those pixels in this list that were visited, but not equal to 1 or outside the borders of the matrix
d. Regardless of location, add them to the stack.
Once the stack is empty, increase the counter, and then return to step # 2.
Continue driving until we visit all locations in our array.
Without further ado, here is the code.
%// Step
With your sample matrix, this is what I get for B :
B = 1 1 0 0 0 0 0 1 1 0 0 2 2 0 1 1 0 0 0 2 0 1 1 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0
To search in a 4-connected neighborhood
To change the code for searching in a four-connected region, that is, only North, East, West and South, the section where you see %// Look at the 8 neighbouring locations , that is:
%// Look at the 8 neighbouring locations [locs_y, locs_x] = meshgrid(loc(2)-1:loc(2)+1, loc(1)-1:loc(1)+1); locs_y = locs_y(:); locs_x = locs_x(:);
To search for 4-related mods, you just need to change this code to give only those main directions:
%
The rest of the code remains untouched.
To match MATLAB bwlabel function
If you want to combine the output of the MATLAB function bwlabel , bwlabel searches for connected components in the main or FORTRAN column. The above code searches in line by line or in order. So you just need to search the columns first, not the rows, as what the above code does, and you do this by replacing the order of the two for loops.
In particular, instead of executing:
for row = 1 : rows for col = 1 : cols .... ....
You would do:
for col = 1 : cols for row = 1 : rows .... ....
Now this should repeat the output of bwlabel .