【问题标题】:Histogram of image not showing expected distribution图像直方图未显示预期分布
【发布时间】:2018-02-08 15:27:51
【问题描述】:

我有一个称为输出的元胞数组。输出包含大小为 1024 x 1024、类型 = 双、灰度的矩阵。我想在一个图上绘制每个矩阵及其相应的直方图。这是我目前所拥有的:

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

然而,我得到的情节似乎有问题......我期待的是条形分布。

我对如何继续有些迷茫,任何帮助或建议将不胜感激!

谢谢:)

【问题讨论】:

  • 尝试hist 而不是imhist。我认为imhist 可能需要 [0 255] 或 [0 1] 范围内的值。
  • 似乎没有帮助:(
  • 嗯,这很令人惊讶。我在下面的答案中为我的评论添加了一些细节。如果这仍然对您不起作用,我们将需要您的数据样本以提供进一步帮助。尝试构建一个具有相同行为但足够小以包含在帖子中的玩具样本(例如 10x10 矩阵)

标签: arrays matlab histogram threshold


【解决方案1】:

根据图像上的颜色条绘制图像像素的值范围为 [0, 5*10^6]。

对于许多图像处理函数,MATLAB 假定两种颜色模型之一,从 [0, 1] 范围内的双精度值或从 [0 255] 范围内的整数值。虽然imhist 文档中没有明确提到支持的范围,但在"Tips" section of the imhist documentation 中,有一个不同数字类型的比例因子表暗示了这些假设。

我认为您的图像范围与这些模型之间的差异是问题的根源。

例如,我加载一个灰度图像并将像素缩放 1000 以近似您的数据。

% Toy data to approximate your image
I = im2double(imread('cameraman.tif'));
output = {I, I .* 1000};

for i = 1:size(output,2)
    figure 
    subplot(2,1,1)
    imagesc(output{1,i});
    colormap('gray')
    colorbar;

    subplot(2,1,2)
    [pixelCount, grayLevels] = imhist(output{1,i});
    bar(pixelCount);
    title('Histogram of original image');
    grid on;
end

第一张图片使用了标准 [0,1] 双值范围的矩阵。 imhist 按预期计算直方图。第二个图像使用了一个具有缩放 [0, 1000] 双值范围的矩阵。 imhist 将所有像素分配给 255 bin,因为这是最大 bin。因此,我们需要一种允许我们缩放箱的方法。

解决方案:使用histogram

histogram 专为任何数字类型和范围而设计。您可能需要摆弄 bin 边缘以显示您感兴趣的结构,因为它不会像 imhist 那样初始化 bin。

figure 
subplot(2,1,1)
imagesc(output{1,2});
colormap('gray')
colorbar;

subplot(2,1,2)
histogram(output{1,2});
title('Histogram of original image');
grid on;

【讨论】:

猜你喜欢
  • 2023-01-29
  • 2019-07-11
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-15
相关资源
最近更新 更多