MATLAB: Separating and Identifying different Black Clusters which are Connected. (All clusters are of same intensity i.e. 0, We are only interested in identifying connected/not connected clusters)

digital image processingdigital signal processinggraphicsimage analysisimage processingImage Processing Toolboximage segmentation

@ ImageAnalyst
Please Guide and Assist!
Lets say I have a binarized image as shown in figure below.
Figure1 (Input Image)
I want to identify black clusters which are connected. (I want a flexible threshold). For example, any black pixel if surrounded by atleast 3 black pixels (threshold) will be said to contain in the cluster, if not then we would say that that pixel is a discountinuty between two adjacent clusters. Please see the figure attached below, which shows how the photoshop bucket fill option has automatically segregrated two clusters, ie blue and red. It is important to note that my figure (attached above) is binarized and contains uniform intensities either 1s or 0s, and I am interested in different clusters of black ie. 0s , hence segregating clusters based on intensities might not work well here.
Figure 2
In the nustshell, I simply want a method of segregrating all the black clusters (0s) and identifying individiually such as B1,B2,B3…… and so on. Please find the attached Image (Figure 3) which represents the final output clearly identifying different clusters with different colours (using the same bucket fill option as mentioned in Figure 2)
Figure 3
Hence I want something like this, with different colors representing dfifferent clusters , all stored indivdually in different 2D Matrices
Example: Blue Cluster values stored in Matrix1, Red Cluster values strored in Matrix 2 and so on….
I have already tried MagicWand.m and grayconnected(), but they all simply cant help in here.
I have already spent a month on this problem. Please help me!

Best Answer

With the latest version there is a mode filter. Maybe that will do what you want. Or maybe you just simply want bwlabel() with 4 connectivity.