clear all; a = [1 1 0 0 0 0 1 1 1 0 0; 1 1 0 0 0 0 1 1 0 0 0; 0 0 1 1 0 0 0 1 1 0 0; 0 0 1 1 0 0 0 1 1 0 0; 0 0 0 0 0 0 1 0 0 0 0; 0 0 0 0 0 0 1 0 0 0 0; 0 0 0 0 0 1 1 1 0 0 1]; cc = bwlabel(a, 4); % 4-neighbors % Examine pixels to see the labels imtool(cc); % View with color and the default colormap ccImg = label2rgb(cc); imtool(ccImg); % View with a specific colormap and black background ccImg = label2rgb(cc, @jet, 'k'); imtool(ccImg); % REPEAT WITH 8-neighbors cc8 = bwlabel(a, 8); cc8Img = label2rgb(cc8, @jet, 'k'); imtool(cc8Img); % Place pixels from region 2 into a mask mask = zeros(size(cc)); mask ( find (cc == 2)) = 1 % Count pixels from a single region size(find(cc == 2)) % get coords of pixels from a single region [x,y] = find(cc == 3) % Find centroid (you do)