MATLAB: Problem with ”mode” function in matlab

cell countingimageMATLABmodeprocessing

Hi,
I've posted last week on the forum regarding a problem with counting cells. Image Analyst very kindly showed my his code that worked, but when I try to run it on my machine, I get this error message :
Error using mode (line 56) X must be an array of double or single numeric values.
Here's the complete code for you guys to see where the error could be
% Demo to link nearby endpoints together.
%All credits for this code go to Image Analyst
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 = 'test2.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');
This is the part that stops the entire code : grayImage(gridLines) = mode(grayImage(:));
If anyone can help me out here, I've tried to change the variables in my code, modify the image so that the mode would work, etc. It'll my life much much easier if I can count thos damned cells with matlab, instead of doing it the old fashioned way.
I run Matlab R2012B.
Thank you

Best Answer

>> x=uint8(randi(244,10));
>> mode(x(:))
Error using mode (line 56)
X must be an array of double or single numeric values.
>> mode(double(x(:)))
ans =
22
>>
For some reason TMW didn't overload the mode function to handle anything other than floating point values.