A. The first problem with your code is the initial definition of bins
. It seems that you come from C or somthing like that, but the definition should be- bins=zeros(1,256);
B. The second point is that you don't need the nested loop, you have a matlab function especially for that:
bins=hist(grayImg(:),1:256); % now, you don't need the pre-definition for 'bins'.
plot(bins);
C. Try to use functions like bar
or imhist
or hist(grayImg(:))
, it may save you all this, and give a nice plot.