图像直方图未显示预期分布

Histogram of image not showing expected distribution

我有一个名为输出的元胞数组。输出包含大小为 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

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

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

谢谢:)

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

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

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

例如,我加载灰度图像并将像素缩放 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。因此,我们需要一种允许我们缩放 bin 的方法。

解决方案:使用histogram

histogram 专为任何数字类型和范围而设计。您可能需要 fiddle 使用 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;