View this
page in Romanian courtesy of ewstranslate

# HOWTO: Counting Blobs (and finding their properties) in MATLAB

## Update (2019-10-21)

MATLAB supplies a
function bwconncomp
that replaces the function bwlabel and generalizes it to more than two
dimensions. I have revised the code below to use bwconncomp instead
of bwlabel.

## History

I wrote a script to count blobs or patches of precipitation using
gridded data of radar reflectivity. The script also computed the
properties of these blobs, including area and orientation. I was
motivated to do this because such things have sometimes been done by
hand, as in Cetrone and Houze 2006 Characteristics of Tropical Convection
over the Ocean near Kwajalein, and I figured that automating this
process would be useful. The script was used by Chris Holder and
Sandra Yuter of NCSU
to study The mesoscale characteristics of tropical oceanic
precipitation during Kelvin and mixed Rossby-gravity wave events.
Chris also contributed improvements and found a couple of bugs.
However, while cleaning up the script to post it on this website, I
discovered that it reproduces functionality that already exists in
the MATLAB's image processing toolbox. To avoid a duplication of
effort, I do not supply the script here. Rather, I include below a
HOWTO that describes how to use MATLAB's built-in functions to count
blobs and compute their properties.

## Blob Counting HOWTO for MATLAB

If you have a matrix R with radar
reflectivities and want to look for objects with reflectivities
greater than TOL, define a new matrix RL with ones where R>TOL and
zeros elsewhere. Note that the matrix could hold anything, not just
radar reflectivities. It is only necessary that discrete patches can
be identified using a threshold value applied to the values within the
matrix R.
>> RL = zeros(size(R));
>> RL(find(R>TOL)) = 1;

Next call the MATLAB
function bwconncomp,
which returns a structure that identifies and assigns an index to each contiguous object with RL==1. Note that the second argument to bwconncomp is four if regions are connected through edges, eight if they are connected through both edges and corners.
>> RLL = bwconncomp(RL,4);

Note that the largest element in RLL is the object count. The properties of each of these regions can be computed using regionprops. For example, to get the centroids and areas of the various objects:
>> stats = regionprops(RLL,'Centroid','Area');

stats is a vector, whose length is the number of objects. Each
element in the vector is a structure that holds the properties of the
objects. MATLAB
structures are useful way of collecting data that pertains to a
single object. Here, for example, the properties of the fourth object
can be accessed as follows:
>> stats(4).Area
>> stats(4).Centroid

The MATLAB
function regionprops
can compute many different blob properties. The help page at the
above link gives a full list. A list of the blobs with the area and centroid of each blob can be printed as follows:
>> for n = 1:length(stats)
>> disp(sprintf('Blob number = %d, Area = %g, Centroid = (%g, %g)',...
>> n,stats(n).Area,stats(n).Centroid))
>> end

## Example Script

bwconncomp_example.m is a sample script
that counts the number of patches where R>0 when R is computed as the sum of a
few gaussian humps and the mean value of R is subtracted away.

Peter Blossey <bloss@uw.edu>