"Array indices must be positive integers or logical values error" for histogram equalization implementation?

36 views (last 30 days)
I'm trying to implement the histeq function, but I keep getting this error and I'm not sure what the problem is. I have 4 functions: one to compute a grayscale image's histogram (essentially the imhist function), one to plot this histogram in a graph, one to compute the image histogram's transformation function (CDF), and the last function calls these functions and performs the histogram equalization all at once. When I run my code, I get the error "Array indices must be positive integers or logical values error" in my transformation function, but I don't know what it means/how to fix it for my specific problem. Here's my code:
%%first function to compute the histogram of a grayscale image %%
function h = histogram(image)
[m,n] = size(image);
h = zeros(256);
for i = 1:m
for j = 1:n
p = double(image(i,j))+1;
h(p) = h(p)+1;
end
end
total_pixel = m*n;
h = h./total_pixel;
end
plotting function:
%%function to plot the histogram results %%
function p = plot(hist)
p = stem((0:1:255),hist);
xlabel('intensity value');
ylabel('PMF');
end
transformation function:
%%function to compute the histogram equalization transformation (CDF) %%
function T = transformation(hist)
x = size(hist,1);
y = size(hist,2);
pixels = x*y;
Y=uint8(zeros(size(hist,1),size(hist,2)));
counts=zeros(256,1);
probf=zeros(256,1);
prbc=zeros(256,1);
cum=zeros(256,1);
fin_lc=zeros(256,1);
%each pixel count
for i=1:size(hist,1)
for j=1:size(hist,2)
value=hist(i,j);
counts(value+1)=counts(value+1)+1; ****** <= This is the line that's giving me the error******
probf(value+1)=counts(value+1)/pixels;
end
end
sum = 0;
nn = 255;
%CDF
for i=1:size(probf)
sum = sum+counts(i);
cum(i)=sum;
prbc(i) = cum(i)/pixels;
fin_lc(i)=round(prbc(i)*nn);
end
end

Answers (2)

YT
YT on 22 Oct 2018
Normally when you get the error...
Array indices must be positive integers or logical values error
...this should ring a bell that you are trying to put something in a vector or matrix using an invalid index which, like the error suggests, is not a positive integer or logical value.
I don't exactly know what `hist` is your transformation function (an image?), but I would start looking at
value=hist(i,j);
If its a positive integer/logical value, you should go to the next line and inspect the variables.
If you ask me, the best way to solve these kind of issues is to just check the variables in the workspace and look at the dimensions and class of each variable that are involved in the error.
  1 Comment
Guillaume
Guillaume on 22 Oct 2018
Yes, as YT says, clearly your value index is not a positive integer or logical. Since value comes from your hist input, then hist contains some non-integer values. Something you should check yourself.
If that hist variable comes from your histogram function, then you would know already that hist is non-integer since you explictly normalise your histogram to the range [0-1].
Note that it is not a good idea to reuse the names of matlab's own functions for your functions and variables. It's going to confuse users and if you're not careful you may end calling matlab's built-in instead of your own.
And of course, why don't you use matlab built-in functions instead of rewriting them?

Sign in to comment.


Image Analyst
Image Analyst on 22 Oct 2018
Three other important points to add:
  1. Don't write your own function to do a histogram when there are at least two built in functions to do that: histogram, and histcounts, plus several old/deprecated functions.
  2. If you do write your own function, make sure it's not a built-in function, like histogram(), sum(), path, min(), max(), image(), etc. This can cause problems. You can check with the "which all histogram" command.
  3. You probably don't have to do histogram equalization. This non-linear operation generally produces ugly results - very unnatural looking and harsh. You'd be better off using mat2gray() or imadjust(). Plus for many operation, such as thresholding, the equalization or histogram adjustment doesn't help. In fact it can make it harder, for example you'd have to use an automatic thresholding routine that can adapt to changing histograms rather than using a better and simpler fixed threshold.

Community Treasure Hunt

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

Start Hunting!