【问题标题】:How does imread scale 12bit images?imread 如何缩放 12 位图像?
【发布时间】:2015-01-27 00:11:05
【问题描述】:

我有一个 12 位的 pgm-image,我用 imread 读取它。结果是一个 16 位图像,其值在 0 到 2^16 - 1 的整个范围内。

Matlab 如何扩展?会

 X = imread('filename');
 X = uint16(double(X)*((2^12-1)/(2^16-1)));

恢复原来的强度?

【问题讨论】:

    标签: matlab imread


    【解决方案1】:

    MATLAB 确实可以正确加载 PGM 12 位图像。但是,在 MATLAB 加载图像后,图像值会从 12 位重新缩放为 16 位。

    MATLAB 使用以下算法将值从 12 位缩放到 16 位:

    % W contains the the 12-bit data loaded from file. Data is stored in 16-bit unsigned integer
    % First 4 bits are 0. Consider 12-bit pixel color value of ABC
    % Then W = 0ABC
    X = bitshift(W,4); % X = ABC0
    Y = bitshift(W,-8); %Y = 000A
    Z = bitor(X,Y); %Z = ABCA 
    % Z is the variable that is returned by IMREAD.
    

    解决方法是这样的

    function out_image = imreadPGM12(filename)
    out_image = imread(filename);
    out_image = floor(out_image./16);
    return
    

    或者向右执行 4 位移位:

    function out_image = imreadPGM12(filename)
    out_image = imread(filename);
    out_image = bitshift(out_image,-4);
    return
    

    更多信息可以在这里找到: http://www.mathworks.com/matlabcentral/answers/93578-why-are-12-bit-pgm-images-scaled-up-to-16-bit-value-representation-in-image-processing-toolbox-7-10

    【讨论】:

    • 仅链接答案应至少包含相关信息,以便如果链接断开,则答案仍然有效。你能总结一下链接的内容吗?谢谢
    • @sof_dff 看起来不错,但我试过这个A = uint16(1:(2^12-1)); imwrite(A,'test.pgm','MaxValue',max(A(:))); B = imread('test.pgm');,你的解决方案都不能恢复A。我误会了什么?
    • @jolo 我对你的例子有点困惑。在您的示例中,如果您从 imwrite 中删除 max(A(:)) ,您将阅读您的智慧。我仍然不明白你的例子与你最初的问题有什么关系。你能解释一下吗?
    • @sof_dff 我试图在我的问题中创建一个示例,即 12 位 .pgm 文件。正如您指出正确删除 max(A(:)) 将使 imread 读取正确的值 - 正如您在答案中指出的那样,它不应该。原因是imwrite 也使用了一些缩放(可以通过在保存的图像上使用imfinfo 看到),我试图通过传递maxValue 参数来解决这个问题。
    • @sof_dff 我现在明白了imwrite 的工作原理以及我如何滥用maxValue 参数。
    猜你喜欢
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多