4

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

Matlab 是如何缩放的?将要

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

恢复原来的强度?

4

1 回答 1

5

MATLAB does load PGM 12-bit images correctly. However, after MATLAB loads the images, the image values are rescaled from 12-bit to 16-bit.

MATLAB uses the following algorithm to scale the values from 12-bit to 16-bit:

% 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.

Workaround for this is like that

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

Alternatively perform a 4 bit shift to the right:

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

Further information can be found here: 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

于 2014-11-28T10:56:54.617 回答