MATLAB: Obtaining Histogram of Image Stored in Cell Array

grayhistogramImage Processing Toolboximhist

Hello,
I have a cell array called output. Output contains matrices of size 1024 x 1024, type = double, grayscale. I would like to plot each matrix and its corresponding histogram on a single plot. Here is what I have so far:
for i = 1:size(output,2)
figure
subplot(2,1,1)
imagesc(output{1,i});
colormap('gray')
colorbar;
title(num2str(dinfo(i).name))
subplot(2,1,2)
[pixelCount, grayLevels] = imhist(output{1,i});
bar(pixelCount);
title('Histogram of original image');
xlim([0 grayLevels(end)]); % Scale x axis manually.
grid on;
end
The plot I get, however, seems to be faulty… I was expecting a distribution of bars.
I am somewhat lost at how to proceed, any help or suggestions would be appreciated!
Thanks 🙂

Best Answer

Instead of this
[pixelCount, grayLevels] = imhist(output{1,i});
bar(pixelCount);
try this:
histogram(output{1,i}, 256);
grid on;