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
참고 항목
카테고리
Help Center 및 File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!