# Counting and Segmentation for Microscopic Objects

by 4hathacker  |  in Image Processing at  12:45 PM

Hello everyone...!!!

In the previous article, discussion was based on counting of objects in images. If you want to refresh the counting algorithm using morphology can go through the previous post for basic counting of objects in the image.

This article is an extension of object counting and segmentation which also involves watershed segmentation and newer concepts. The problem comes around during counting of objects when they were overlapped or connected. It makes the segmentation difficult and thus counting becomes erroneous.
To overcome the same, we have used the watershed segmentation algorithm with distance transform. From scratch, in geography, watershed is defined as the ridge that divides areas drained by different river systems and a catchment basin is used to represent the geographical area that drains into a river or reservoir. Similarly, regions of segmented image are equivalent to catchment basins and the boundaries among them are analogous to ridges. The distance transform used in conjunction will compute the distance from every pixel to the nearest non zero valued pixel.

Implementation of algorithm in MATLAB started the same as in previous article i.e., loading the image, thresholding and morphological opening or closing. Here the first test image is cell like structures taken from open research article of BMC - 'Dynamic in vivo imaging and cell tracking using a histone fluorescent protein fusion in mice'. And the second test image is taken from Cell Image Library licensed under Creative Common Attribution. The task is to perform the counting of cells and quoting the density of cells per unit area for the image. For counting and density calculations, you can use bwconncomp(), bwlabel() or regionprops() in MATLAB. One extra function imoverlay() is also used here which is not present in the MATLAB R2014a by default. So, if it is needed you can download from MATLAB File Exchange. This function allows us to easily display one image in color on top of another grayscale image. Another newer function is imimposemin() which modifies the intensity image using morphological reconstruction so it only has regional minima wherever our binary image is non zero. imextendedmax() and above functions are used to achieve the maxima corresponding to the cell nuclei and then transforming the image such that the background pixels and these maxima are present only local minima in range.

figure, imshow(A), title('Test image1')
I = rgb2gray(A);                                           %  RGB to gray conversion
I = imclearborder(I);                                    % Removing Border Noise
I = wiener2(I,[5 5]);                                     % Adaptive Wiener Filtering
bw = im2bw(I, graythresh(I));                     % Thresholding by Otsu's Method and binarization
figure, imshow(bw), title('BW image')
bw2 = imfill(bw, 'holes');                             %
bw3 = imopen(bw2, strel('disk',2));             % Morphological Processing
bw4 = bwareaopen(bw3, 100);                    %
bw4_perim = bwperim(bw4);                      %
figure, imshow(bw4_perim), title('bw4_perim')
overlay1 = imoverlay(I, bw4_perim, [1 .3 .3]);              % First Overlay function
figure, imshow(overlay1), title('overlay1')
maxs = imextendedmax(I,  5);                                        %  Extending the maxima for cell nuclei
maxs = imclose(maxs, strel('disk',3));
maxs = imfill(maxs, 'holes');
maxs = bwareaopen(maxs, 2);
overlay2 = imoverlay(I, bw4_perim | maxs, [1 .3 .3]); % Second Overlay with same parameters
figure, imshow(overlay2), title('overlay2')
Jc = imcomplement(I);
I_mod = imimposemin(Jc, ~bw4 | maxs);
figure, imshow(I_mod), title('I_mod')
L = watershed(I_mod);                                                 % Watershed Segmentation function
labeledImage = label2rgb(L);
figure, imshow(labeledImage), title('labeledImage')
cc = bwconncomp(L)
cells_per_unit_area = cc.NumObjects / (size(L,1)*size(L,2))

The resulting images obtained by following source code are show above. While the number of cells and density of cells shown at command window are given below.

% Result for test image2                                                          % Result for test image1
cc =                                                                                           cc =

Connectivity: 8                                                                            Connectivity: 8
ImageSize: [1424 2144]                                                              ImageSize: [773 815]
NumObjects: 2019                                                                       NumObjects: 234
PixelIdxList: {1x2019 cell}                                                        PixelIdxList: {1x234 cell}

cells_per_unit_area =                                                              cells_per_unit_area =

6.6130e-04                                                                                   3.7143e-04

If you find yourself comfortable with image segmentation and fluent in counting objects in images  using MATLAB or any other software, then go through this programing puzzle. Hope you like this post...!!! 