Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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

Asked by Muhammad Ali Qadar on 11 May 2013

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

2 Comments

Walter Roberson on 11 May 2013

What is class(Image) ? What is class(Threshold) ? Are they both the same class of integer? Is one of them an integer class and the other is a non-scalar double ?

Muhammad Ali Qadar on 12 May 2013

Image is uint8 and Threshold is Double ..............Solved

Thank you

Muhammad Ali Qadar

1 Answer

Answer by Image Analyst on 12 May 2013
Accepted 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.

2 Comments

Muhammad Ali Qadar on 16 May 2013
 thats great, but Why you have made the original image dark by doing this   
    % code
imshow(round(grayImage/4));
  end 
Image Analyst on 16 May 2013

I did that to create the demo image. Otherwise the original image is of such high contrast that doing the equalization operation would have very little noticeable effect. I wanted you to notice that it actually did something so I reduced the contrast of the image. Consider the image after that point as your "starting image".

Image Analyst

Contact us