![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192352/image.jpeg)
How to make a histogram plot show as many lines (bars) instead of one long line showing the high points?
4 次查看(过去 30 天)
显示 更早的评论
I'm working on a function that takes in a grayscale image, turns it into a matrix, then takes that info and turns it into a histogram plot. I finally got it working but my histogram plot looks like the first image (blue), and it's supposed to look like the second (black). Anyone know what I should change to fix it? My code is below
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192241/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192242/image.jpeg)
% plotting the image data into a histogram %
function p = histogram(image)
histogram = histogram_matlab(image);
plot((0:1:255),histogram);
xlabel('intensity value');
xlim([0,255]);
ylim([0 max(histogram)]);
ylabel('PMF');
end
%computing the grayscale image %
function h = histogram_matlab(imageSource)
openImage = imread(imageSource);
[m,n] = size(openImage);
h = zeros(256);
for i = 1:m
for j = 1:n
p = double(openImage(i,j))+1;
h(p) = h(p)+1;
end
end
total_pixel = m*n;
h = h./total_pixel;
end
0 个评论
采纳的回答
YT
2018-10-22
编辑:YT
2018-10-22
Without looking at the code, I've already seen that you used `plot` instead of `histogram` or `bar` function. You see, when you use `plot` this way, it gives you a line through all the points. I think you're looking for something like
bar((0:1:255),histogram,'k'); %'k' is black
It will look something like this (tested with a random image)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192352/image.jpeg)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Histograms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!