Array Indices Error In Histogram Equalisation

%Practical 6 Histogram Equalisation
close all;
clear all;
clc;
a=imread('HG.jpg');
a1=double(a);
a2=rgb2gray(uint8(a1));
[row, col]=size(a2);
c=row*col;
h=zeros(1,300);
z=zeros(1,300);
for m = 1:1:row
for n = 1:1:col
t=a2(m,n);
h(t+1)=h(t+1)+1;
end
end
pdf=h/c;
cdf(1)=pdf(1);
for x=2:1:256
cdf(x)=pdf(x)+cdf(x-1);
end
new=round(cdf*256);
new=new+1;
for p=1:1:row
for q=1:1:col
temp=a2(p,q);
b(p,q)=new(temp);
t=b(p,q);
z(t+1)=z(t+1)+1;
end
end
b=b-1;
subplot(2,2,1)
imshow(uint8(a2))
title('Original Image');
subplot(2,2,2)
bar(h)
title('Histogram of Original Image');
subplot(2,2,3)
imshow(uint8(b))
title('Histogrammed Image');
subplot(2,2,4)
bar(z)
title('Eq. Histogram');
Error:
Array indices must be positive integers or logical values.
Error in Equal (line 28)
b(p,q)=new(temp);

回答(1 个)

a=imread('HG.jpg');
a1=double(a);
a2=rgb2gray(uint8(a1));
the result of the above should be uint8 because the input is uint8 -- but the output values can include 0.
for p=1:1:row
for q=1:1:col
temp=a2(p,q);
b(p,q)=new(temp);
the a2 values can include 0, so temp can be 0, so new(temp) could be an attempt to index new at index 0.
Caution: you cannot just use new(temp+1) as that risks adding 1 to uint8(255) which does not give 256 and instead gives 255.

类别

Community Treasure Hunt

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

Start Hunting!

Translated by