How does imread scale 12bit images?

泪湿孤枕 提交于 2019-12-06 04:07:09

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!