I need correction for following simple code of histogram equalization for the image.

1 view (last 30 days)
A = imread('C:\Users\pc_2\Desktop\bs\Im_one.jpg');
r=size(A,1);
c=size(A,2);
A=double(A);
ah= uint8(zeros(r,c));
n=r*c;
f=zeros(256,1);
pdf=zeros(256,1);
cdf=zeros(256,1);
cum=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=1:size(pdf)
sum =sum +freq(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,imshow(he);
when i run it responds as "Index exceeds the number of array elements (1)."

Answers (1)

DGM
DGM on 27 Nov 2023
Edited: DGM on 27 Nov 2023
It's sunday, so it's time to exercise boredom on dead questions. This is one of those questions where it's clear that the given code doesn't match the description of the problem, but it's not at all clear how the described problem could occur.
The given code certainly won't run. It will fail with an error on this line:
% sum =sum +freq(i);
... because there is no such variable called freq. It's named f in the rest of the code, but for some reason it's named differently here. So there's a completely different error than the one described, and this error is quite easy to find.
So what happens if we fix that one line?
% a single-channel uint8 image
A = imread('tire.tif');
r = size(A,1);
c = size(A,2);
Ad = double(A); % create a copy instead of dealing with an improperly-scaled image later on
ah = zeros(r,c,'uint8');
n = r*c;
f = zeros(256,1);
pdf = zeros(256,1);
cdf = zeros(256,1);
cum = zeros(256,1);
out = zeros(256,1);
for i = 1:r
for j = 1:c
value = Ad(i,j);
f(value+1) = f(value+1) + 1;
pdf(value+1) = f(value + 1)/n;
end
end
sum = 0;
L = 255;
for i = 1:size(pdf) % use numel() (or length() i guess)
sum = sum + f(i); % f instead of freq
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(Ad(i,j) + 1);
end
end
% create comparison image
he = histeq(A); % A must be correctly-scaled for its class!
imshow([A ah he],'border','tight');
So the code works without any other errors. Where did the indexing error come from? We don't know, since the complete error message was never included.
Could it have been caused by feeding it an RGB image? Certainly, most JPGs are RGB, so maybe that's the issue? Nope. The depth of A is completely ignored. All operations on A are done with respect to the first channel. The output image ah is strictly single-channel, regardless of the input image size.
What if the image weren't uint8? Would that cause an error? Yes, that would cause an error, since you would be trying to index beyond the extents of the fixed-length vectors f and pdf. Depending on the class and range of the image, the errors you would get would either look like
Index exceeds the number of array elements (256).
Error in AT_sandbox (line 28)
f(value+1) = f(value+1) + 1;
or
Array indices must be positive integers or logical values.
Error in AT_sandbox (line 28)
f(value+1) = f(value+1) + 1;
Those vectors are explicitly preallocated with a fixed length of 256, so that particular error message shouldn't be happening here either.
Are there any places where a scalar variable is being addressed with a single index? I don't see any, but there is one other thing ...
% for i = 1:size(pdf) % don't do that
This is just a great way to break things. The output of size() is a variable-length vector. When used like this with the colon operator, only the first element is used. It's just a matter of chance that pdf happens to be a column vector, so the range of i is appropriate for the uses within the loop. For this application where everything being indexed are vectors, this line should be using numel(), not size(). Again, while this is a great way to create problems, it can't cause a problem here, so it's not the mystery issue either.
So what was the problem? My guess is that OP had shadowed size() with a variable of the same name. That would throw an error prior to reaching the error regarding freq. Getting rid of forgotten and unintended junk variables is why I always start sandbox scripts with clearvars.

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!