MATLAB Answers


Obtaining Histogram of Image Stored in Cell Array

Asked by itend
on 30 Aug 2017
Latest activity Commented on by itend
on 6 Sep 2017
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)
[pixelCount, grayLevels] = imhist(output{1,i});
title('Histogram of original image');
xlim([0 grayLevels(end)]); % Scale x axis manually.
grid on;
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 :)


Sign in to comment.

2 Answers

Answer by Image Analyst
on 5 Sep 2017
 Accepted Answer

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

  1 Comment

Thank you for your response.
This works!

Sign in to comment.

Answer by Rylan Dmello on 5 Sep 2017

This issue might be caused due to the difference between the binLocations output of the imhist command and the x-axis scale for the bar graph. For example:
A = rand(1024); % generate some example data
[counts, binLocations] = imhist(A); % generate histogram bins
bar(counts); % plot histogram data into bar graph
After executing the above code, examine the value of the binLocations variable. It would have a range between 0 to 1, while the histogram would have an x-axis between 0 to 255. Therefore, the following line of code:
xlim([0 binLocations(end)]);
would limit the bar graph to a range of 0 to 1, while the actual data is plotted on a range from 0 to 255. If you would like to show the full range of the bar graph, the following may work better:
xlim([0 numel(binLocations)])
However, if you would like to scale the x-axis of the bar graph correctly between 0 and 1, the following might work better:
bar(binLocations, counts); % provide both x and y inputs to the 'bar' function
xlim([0 binLocations(end)]); % apply x-axis limits
For more information, please refer to the MATLAB documentation pages for bar , numel , and imhist .

  1 Comment

Thank you for your response.

Sign in to comment.