Obtaining Histogram of Image Stored in Cell Array
1 次查看(过去 30 天)
显示 更早的评论
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/166961/image.jpeg)
I am somewhat lost at how to proceed, any help or suggestions would be appreciated!
Thanks :)
0 个评论
采纳的回答
Image Analyst
2017-9-5
Instead of this
[pixelCount, grayLevels] = imhist(output{1,i});
bar(pixelCount);
try this:
histogram(output{1,i}, 256);
grid on;
更多回答(1 个)
Aylin
2017-9-5
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!