MATLAB: Barchart not appearing as it should

barchartImage Processing Toolbox

Why does my bar chart appear black and not blue as instructed to?
[maxval,idx]=max(IM(:));
maxval= double(maxval);
[counts,x] = imhist(IM,maxval);
bar(x,counts,'b');
thanks Jason

Best Answer

Jason, It shows up as blue for me in R2014b. But if your IM is uint8 or uint16, your call to imhist is wrong - it will be aliased and you will have a false spike in the middle. Just run this to see it:
IM = uint8(randi(255, 256,256));
[maxval,idx]=max(IM(:));
maxval= double(maxval);
[counts,x] = imhist(IM,maxval);
bar(x,counts,'b');
To fix, do this:
IM = uint8(randi(255, 256,256));
[maxval,idx]=max(IM(:));
maxval= double(maxval);
[counts,x] = imhist(IM,maxval+1);
bar(x,counts,'b');
The reason is you need a bin for intensity 0. If you don't, and you have let's say values of 0-255 and you're using 255 bins, one of the bins has to be doubled up because you're using only 255 bins instead of 256 bins.
If you have a really old version of MATLAB, you can try the 'Color' option, which a lot of MATLAB functions still use:
bar(x,counts, 'Color', 'b');
Again, not necessary in R2014b, and may not even work for older releases either, but it's easy enough to try.