Problem with ''mode'' function in matlab

4 views (last 30 days)
Arthur
Arthur on 9 Sep 2015
Commented: dpb on 10 Sep 2015
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

Accepted Answer

dpb
dpb on 9 Sep 2015
>> 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.
  2 Comments
Arthur
Arthur on 9 Sep 2015
It worked. Many many thanks to you.
dpb
dpb on 10 Sep 2015
I made a copy of the mode m-file and simply commented out the first test
if ~isfloat(x)
error(message('MATLAB:mode:InvalidInput'))
end
As one would suspect, then it functions as expected. I really don't see why TMW made it so restrictive; I suppose there's possibly some numeric class for which it might fail but seems like those ought to be dealt with as the special case instead of using such a big hammer on all other numeric types.
>> x=uint8(randi(244,10));
>> mode(x(:))
Error using mode (line 56)
X must be an array of double or single numeric values.
>> newmode(x(:))
ans =
45
>> whos ans
Name Size Bytes Class Attributes
ans 1x1 1 uint8
>>
It already preserves the class of uint8 here; didn't do an exhaustive test...

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!