【发布时间】:2015-09-16 18:12:07
【问题描述】:
我很想在Matlab中实现(我知道有一个自定义函数可以实现它)灰度图像直方图,到目前为止我已经尝试过:
function h = histogram_matlab(imageSource)
openImage = rgb2gray(imread(imageSource));
[rows,cols] = size(openImage);
histogram_values = [0:255];
for i = 1:rows
for j = 1:cols
p = openImage(i,j);
histogram_values(p) = histogram_values(p) + 1;
end
end
histogram(histogram_values)
但是当我调用函数时,例如:histogram_matlab('Harris.png')
我得到一些图表,例如:
这显然不是我所期望的,x 轴应该从 0 到 255,y 轴应该从 0 到存储在 histogram_values 中的任何最大值。
我需要获得类似imhist 提供的东西:
我应该如何设置它?我是不是执行不好?
编辑
我已将代码更改为 @rayryeng 建议的改进和更正:
function h = histogram_matlab(imageSource)
openImage = rgb2gray(imread(imageSource));
[rows,cols] = size(openImage);
histogram_values = zeros(256,1)
for i = 1:rows
for j = 1:cols
p = double(openImage(i,j)) +1;
histogram_values(p) = histogram_values(p) + 1;
end
end
histogram(histogram_values, 0:255)
但是直方图不是预期的:
这里很明显,y 轴上存在一些问题或错误,因为它肯定会超过 2。
【问题讨论】:
标签: matlab image-processing histogram