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.
>> 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).CentroidThe 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