Error using histeq Expected input number 1, I, to be two-dimensional. Error in histeq (line 70) validateat​tributes(a​,{'uint8',​'uint16','​double','i​nt16','sin​gle'}, ...

8 views (last 30 days)
my code is
a=imread('cameraman.jpg');
r=size(a,1);
c=size(a,2);
ah=uint8(zeros(r,c));
n=r*c;
f=zeros(256,1);
pdf=zeros(256,1);
cdf=zeros(256,1);
out=zeros(256,1);
for i=1:r
for j=1:c
value=a(i,j);
f(value+1)= f(value+1)+1;
pdf(value+1)= f(value+1)/n;
end
end
sum=0; l=255;
for i=size(pdf)
sum=sum+f(i);
cum(i)=sum;
cdf(i)=cum(i)/n;
out(i)=round(cdf(i)*l);
end
for i=1:r
for j=1:c
ah(i,j)=out(a(i,j)+1);
end
end
figure,imshow(ah);
he=histeq(a);
figure,inshow(he);
I am getting an error as,
Error using histeq
Expected input number 1, I, to be two-dimensional.
Error in histeq (line 70)
validateattributes(a,{'uint8','uint16','double','int16','single'}, ...
Error in Untitled5 (line 40)
he=histeq(a);
can somebody help me in this
Thanks in advance.

Accepted Answer

Image Analyst
Image Analyst on 25 Oct 2014
First of all, cameraman.jpg is not a standard demo image. You probably saved cameraman.tif as a jpeg image and that converted it to color. Then, since it was color, you used the size function incorrrectly. See http://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/
Below is the corrected code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
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;
grayImage = imread('cameraman.tif');
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
ah=uint8(zeros(rows,columns));
n=rows*columns;
f=zeros(256,1);
pdf=zeros(256,1);
cdf=zeros(256,1);
out=zeros(256,1);
for i=1:rows
for j=1:columns
value=grayImage(i,j);
f(value+1)= f(value+1)+1;
pdf(value+1)= f(value+1)/n;
end
end
sum=0; l=255;
for i=size(pdf)
sum=sum+f(i);
cum(i)=sum;
cdf(i)=cum(i)/n;
out(i)=round(cdf(i)*l);
end
for i=1:rows
for j=1:columns
ah(i,j)=out(grayImage(i,j)+1);
end
end
subplot(1,2,1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
drawnow;
he=histeq(grayImage);
subplot(1,2, 2);
imshow(he, []);
title('Histogram Equalized Image', 'FontSize', fontSize);
Finally, histogram equalization rarely produces a good image. It almost always looks worse - very harsh and unnatural. You'd be better off using imadjust() or just not doing it at all. Histogram equalization or scaling/normalization is usually not needed for image segmentation. Algorithms can work just fine on the original image.
  2 Comments
vetri veeran
vetri veeran on 25 Oct 2014
Edited: vetri veeran on 25 Oct 2014
Thank you sir. I got the result by using the above code
I have a question
If I give any value 1,2,3, grayImage = grayImage(:, :, 1); grayImage = grayImage(:, :, 2); grayImage = grayImage(:, :, 3);
I got the same result. Then what is the main difference between these 3 values.
Image Analyst
Image Analyst on 25 Oct 2014
Somehow, such as by saving an image as JPEG, grayImage got turned into a color, 3D image. But all color channels (red=1, green=2, and blue=3) are the same so it appears as grayscale even though it's color. The third index is what color channel you are referring to. If they're all the same image in each color channel, then you will get all the same images.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!