clear
% Read in image
image = imread('F:\SPRING 2020\LIVING SYSTEM ANALYSIS-BIOSTATS\CELL SEEDING\A1_TR_02122020_1200.jpg');
% Show image you're working with
imshow(image,[]);
% Convert image to a grayscale image
grayImage = rgb2gray(image);
% Show grayscale image
imshow(grayImage,[]);
% Enhance the contrast of the image using imadjust
image_imadjust = imadjust(grayImage, [0 1], [0 1], 0.4);
imshow(image_imadjust,[]);
imshowpair(grayImage,image_imadjust,'montage');
% Additional step of removing noise by adaptive filtering using a small 5×5
% window
noiseRemoval = wiener2(image_imadjust, [10 10]);
imshow(noiseRemoval,[]);
imshowpair(image_imadjust,noiseRemoval,'montage');
% Creating a binary mask that contains the edge detected cells
binary_mask = edge(noiseRemoval,'sobel');
imshow(binary_mask,[]);
imshowpair(grayImage,binary_mask,'montage');
% Fill in the gaps in the lines surrounding the object in the gradient
% mask by dilating the sobel image using linear structuring elements
% Create two perpindicular linear structuring elements using strel function
se90 = strel('line',3,90);
se0 = strel('line',3,0);
% Dialate the binary gradient mask using the vertical structuring element
% followed by the horizontal structuring element
dilated = imdilate(binary_mask,[se90 se0]);
imshow(dilated,[]);
% Filling any holes within the cells
fill = imfill(dilated,'holes');
imshow(fill,[]);
% Perform another morphological opening of the cells using a disc kernel
fillagain = imopen(fill,strel('disk',4));
imshow(fillagain,[]);
imshowpair(fill,fillagain,'montage');
% Objects on the borders can be caused by noise and other artifacts
% Can eliminate objects on the borders using imclearborders
clearBorder = imclearborder(fillagain,8);
imshow(clearBorder,[]);
imshowpair(fillagain, clearBorder,'montage');
% Remove all connected components (cells) that have fewer than 10 pixels
bware = bwareaopen(clearBorder, 100);
imshow(bware,[]);
% Find the perimeter of cells
bw4_perim = bwperim(bware);
% The [1 .3 .3] is just specifying the color
overlay1 = imoverlay(bware, bw4_perim, [1 .3 .3]);
imshow(overlay1,[]);
I = grayImage .* uint8(bware);
imshow(I,[]);
% Some cells will overlap so apply watershed algorithm on the image which
% will be able to partially divide the groups into distinct cells
% Find cell centroids
maxs = imextendedmax(I, 5);
maxs = imclose(maxs, strel('disk',2));
maxs = imfill(maxs, 'holes');
maxs = bwareaopen(maxs, 1);
overlay2 = imoverlay(I, bw4_perim | maxs, [1 .3 .3]);
imshow(overlay2,[]);
% modify the image so that the background pixels and the extended maxima pixels are forced to be the only local minima in the image.
Jc = imcomplement(I);
I_mod = imimposemin(Jc, ~bware | maxs);
% Apply the watershed algorithm
L = watershed(I_mod);
labeledImage = label2rgb(L);
% Count number of cells
[L, num] = bwlabel(L);
% Overlay the detected cells over the original grayscale image to evaluate
% the performance of the algorithm
mask = im2bw(L, 1);
overlay3 = imoverlay(I, mask, [1 .3 .3]);
imshow(overlay3,[]);
I am trying to find the centroid of the cells in this image so that I can count the number of cells despite if any of them overlap, however the code that I have bolded above is not finding the centroid of every cell. I was wondering if I have my numbers wrong or what could be the solution to my problem, because I still want to overlay the centroids onto my image to see that it has correctly found the centroids. I've included the original image below.
Best Answer