Problem while counting cells

4 views (last 30 days)
Arthur
Arthur on 4 Sep 2015
Commented: Arthur on 8 Sep 2015
Hello chaps.
My problem is simple, I can't find a way to use matlab to count my cells. I'v tried many approach, the more reliable would be the one named Steve on Image Processing.
I tried his code, and what happened is that my image would become black. Without further do, here are the images step by step. I'll include the code lines as well as the images.
We're beginning with this first image, the raw product take on a Zweiss microscope.
clear all; close all; clc
I = imread('test.jpg');
Applying the grey threshold.
bw = im2bw(I, graythresh(I));
As you can see, I'm already loosing some cells in the dark area.
Now for the "filling of holes"
bw2 = imfill(bw,'holes');
So yeah, not much to work from. Here's the small portion of code related to this, it's not much, but it doesn't work anyway so.
I = imread('test.jpg');
bw = im2bw(I, graythresh(I));
bw2 = imfill(bw,'holes');
bw3 = imopen(bw2, ones(5,5));
bw4 = bwareaopen(bw3, 40);
bw4_perim = bwperim(bw4);
imshow(bw4_perim)
If anyone has a solution, or a better method, thank you, thank you so much. You'll save me countless hours of counting cells.
Regards, Arthur

Answers (1)

Image Analyst
Image Analyst on 4 Sep 2015
This is the kind of image a bottom hat filter is meant for:
% Demo to link nearby endpoints together.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = pwd % Determine where demo folder is (works with all versions).
baseFileName = 'test.jpg';
%===============================================================================
% Read in a color reference image.
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
% Take the blue channel and get rid of the grid lines
grayImage = rgbImage(:,:,3);
gridLines = grayImage > 207;
grayImage(gridLines) = mode(grayImage(:));
subplot(2, 2, 2);
imshow(grayImage);
axis on;
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Do a bottom hat filter
se = strel('disk', 11);
botHatImage = imbothat(grayImage, se);
subplot(2, 2, 3);
imshow(botHatImage, []);
title('Bottom Hat Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Threshold and binarize the image.
% [lowThreshold, highThreshold, lastThresholdedBand] = threshold(83, 255, botHatImage);
lowThreshold = 12;
binaryImage = botHatImage >= 12;
% Get rid of single pixels
binaryImage = bwareaopen(binaryImage, 2);
% Count the number of blobs:
[labeledImage, numBlobs] = bwlabel(binaryImage, 8);
subplot(2, 2, 4);
imshow(binaryImage, []);
caption = sprintf('Final Binary Image with %d cells', numBlobs);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
  1 Comment
Arthur
Arthur on 8 Sep 2015
I have this error code when I try to run it
Error using mode (line 56)
X must be an array of double or single numeric values
Any idea what's that about?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!