How does imread scale 12bit images?

2019-05-10 09:24发布

I have a 12-bit pgm-image, which I read with imread. The result is a 16-bit image having values in the full range 0 to 2^16 - 1.

How does Matlab scale? Will

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

recover the original intensities?

标签: matlab imread
1条回答
放荡不羁爱自由
2楼-- · 2019-05-10 10:01

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

查看更多
登录 后发表回答