Obtaining Histogram of Image Stored in Cell Array
3 views (last 30 days)
Show older comments
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 :)
0 Comments
Accepted Answer
Image Analyst
on 5 Sep 2017
Instead of this
[pixelCount, grayLevels] = imhist(output{1,i});
bar(pixelCount);
try this:
histogram(output{1,i}, 256);
grid on;
More Answers (1)
Aylin
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
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!