A problem in using imhist to display histogram of indexed image

2 views (last 30 days)
First i convert the image to an indexed image, and use only 5 colors to show what is my problem.
clear
img = imread('peppers.png');
[x,map] = rgb2ind(img,5);
figure;
imhist(x,map)
the colorbar doesn't match with the histogarm bars, this is my colormap:
map =
0.2784 0.1373 0.2353
0.7608 0.1686 0.1373
0.8902 0.7255 0.6353
0.4275 0.3765 0.2235
0.8471 0.5569 0.1020
the first one isn't white, It seems that the colorbar is shifted by one.

Answers (1)

DGM
DGM on 16 Jun 2024
This is a bug that's been part of imhist() for a very long time. When fed an integer-class indexed image, the colorbar will be off by one and the last color will be missing due to the use of inappropriate XData in the second axes and the two axes being subsequently linked. Converting the indexed image to float-format using the appropriate convention should suffice as a workaround.
This particular bug and others are discussed here:
Therein are examples showing the difference in behavior between float and integer indexed images. Also included is a third-party replacement for imhist() which addresses the given problems. The last example given demonstrates multiple ways to histogram indexed images in both float and uint conventions.

Categories

Find more on Images 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!