MATLAB: Imhist() is not generating histogram image of image

histogramImage Processing Toolboximhist

i have an RGB image and i am trying to generate histogram from the intensity image of the RGB. but my code is not generating histogram. please let me know how to solve
Pic = 'violet.bmp';
s1 = imread(Pic);
I1 = rgb2gray(s1);
imhist(I1);
imshow(I1);
drawnow();
this code only generatig till rgb2gray(), it is not generating imhist() part.

Best Answer

imhist(), as you called it, will display the histogram only. It will not return the histogram bin values. However, you immediately cover it up with the image so that the histogram can no longer be shown. Try this:
baseFileName = 'violet.bmp';
fullFileName = fullfile(pwd, baseFileName);
if exist(fullFileName)
rgbImage = imread(fullFileName);
grayImage= rgb2gray(rgbImage);
[pixelCounts grayLevels] = imhist(grayImage, 256);
bar(grayLevels, pixelCounts);
xlim([0 255]);
uiwait(msgbox('Click OK to see the image'));
imshow(grayImage);
else
warningMessage = sprintf('Error: file not found\n%s', fullFileName);
uiwait(warndlg(warningMessage));
end
Related Question