% Script: bwconncomp_example.m % Author: Peter Blossey, pblossey@u.washington.edu % Purpose: Demonstrate use of bwconncomp and regionprops in MATLAB % to count discrete patches or blobs and compute their % properties, e.g. their areas and centroids. % Written: April 2008. % Revised to use bwconncomp in place of bwconncomp: Oct 2019. clear all; close all % clean up work space % set up a bunch of gaussians, with differing half-widths, as the blobs. [X,Y] = meshgrid([0:100],[0:100]); % define grid N = 10; % number of gaussian humps % define x and y locations of gaussian humps and their radii. x0 = floor(100*rand(1,N)); y0 = floor(100*rand(1,N)); r0 = 1+floor(9*rand(1,N)); % define R matrix as sum of these gaussian humps. R = zeros(size(X)); for n = 1:length(x0) R = R + exp(-( (X-x0(n)).^2 + (Y-y0(n)).^2 )/2/r0(n).^2); end % subtract away mean from R. R = R - mean(mean(R)); % define zero as the threshold for identifying a blob. TOL = 0.0; % set up RL matrix which is one where R>TOL and zero elsewhere. RL = zeros(size(R)); RL(find(R>TOL)) = 1; % Call bwconncomp -- this idenitifies and counts that blobs. % Note that the second argument indicates whether blobs are % considered contiguous if they share an edge (if the value is % four) or if they share either an edge or a corner (if the value % is eight). RLL = bwconncomp(RL,4); % Use regionprops to compute the properties of the blobs. stats = regionprops(RLL,'Area','Centroid'); % write out the list of blobs and their properties for n = 1:length(stats) disp(sprintf('Blob number = %d, Area = %g, Centroid = (%g, %g)',... n,stats(n).Area,stats(n).Centroid)) end NumRegions = length(stats); % make a plot of the three matrices: R, RL and RLL. figure(10); clf colormap(parula(max(3,NumRegions))) subplot(221); pcolor(R); shading flat; colorbar; ... title('This is the matrix R'); subplot(222); pcolor(RL); shading flat; colorbar; ... title('This is the matrix RL'); RLM = nan(size(R)); for n = 1:length(RLL.PixelIdxList) RLM(RLL.PixelIdxList{n}) = n; end subplot(223); pcolor(RLM); shading flat; ... caxis([0.5 NumRegions+0.5]); hLL = colorbar; ... set(hLL,'YTick',[1:NumRegions]); ... title('This shows the blob numbers');