MATLAB: Integers can only be combined with integers of the same class, or scalar doubles

image adaptive thresholdingImage Processing Toolbox

I have following code and above stated error I am getting,
if true
% Global Thresholding of an unsuitable image. In comparison, the same
% image is thresholded in an Adaptive manner.
clear all;
% load ball;
Image= imread('cameraman.tif');
Image=Image(:,:,1);
clear ball;
% Display the original image (normalized to the range 0-64)
figure;
colormap(gray);
image(round(Image/4));
title('(a) The Original image');
% Display the Gray-level Distribution Histogram
Min= min(min(Image));
Max= max(max(Image));
Min=double(Min);
Max=double(Max);
figure;
hist(Image, (Min):(Max));
title('(b) Gray-level Distribution Histogram');
% Perform Global thresholding with a threshold of 130
Tg= 130;
GlobalThresholded= (255 * (Image > Tg));
figure;
colormap(gray);
image(GlobalThresholded);
title('(c) Globally Thresholded image, Tg=130');
% Perform an Adaptive Thresholding: average over a neighborhood of 61x61
[M, N]= size(Image);
AverageMask= ones(61, 61) / (61^2);
Threshold= conv2(Image, AverageMask);
ThresholdMask= (Image - (Threshold(31:M+30, 31:N+30)) + 6);<--------- here is the error
AdaptiveThresholded= 255 * (ThresholdMask > 0);
figure;
colormap(gray);
image(AdaptiveThresholded);
title('(d) Adaptive Thresholded Image');
% code
end
Somebody please Suggest me Whats wrong here, please give me reasons because I got it many times,
Thanks

Best Answer

You have to case "Image" as double in
Threshold= conv2(Image, AverageMask);
By the way, don't use "Image" as a variable name since it's so close to the built-in function called "image" and you risk using the wrong one with a simple typo. There were a number of other errors in there, which I fixed for you:
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
% Global Thresholding of an unsuitable image. In comparison, the same
% image is thresholded in an Adaptive manner.
grayImage= imread('cameraman.tif');
clear ball;
% Display the original image (normalized to the range 0-64)
colormap(gray);
subplot(2, 2, 1);
% Reduce the contrast.
imshow(round(grayImage/4));
title('(a) The Original image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
grid on;
title('(b) Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Perform Global thresholding with a threshold of 130
Tg= 130;
GlobalThresholded = grayImage > Tg;
colormap(gray);
subplot(2, 2, 3);
imshow(GlobalThresholded);
title('(c) Globally Thresholded image, Tg=130', 'FontSize', fontSize);
% Perform an Adaptive Thresholding: average over a neighborhood of 61x61
AverageMask= ones(61, 61) / (61^2);
Threshold = conv2(double(grayImage), AverageMask, 'same');
% Get the image that is 6 above the deviation from the local mean.
ThresholdMask = double(grayImage) - Threshold + 6; % Why 6????
AdaptiveThresholded = ThresholdMask > 0;
subplot(2, 2, 4);
imshow(AdaptiveThresholded);
title('(d) Adaptive Thresholded Image', 'FontSize', fontSize);
By the way, do you know that CLAHE is implemented by MATLAB's Image Processing Toolbox. You can use the function adapthisteq() to do essentially what you tried to, and do a better job of it.
Related Question