Why are 12-bit PGM images scaled up to 16-bit value representation in Image Processing Toolbox 7.10 (R2010a)?

18 views (last 30 days)
I am loading 12-bit PGM images into MATLAB. However, the image values when loaded are between 0 and 65535 suggesting that this is actually a 16-bit PGM image. I would expect to see values between 0 and 4095. Additionally, the values do not appear to be an exact multiply of original values by 16.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 Jul 2010
This is not a bug in MATLAB 7.10 (R2010a). 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.
To work around this issue you need to manually rescale the result of IMREAD from 16-bit to 12-bit. To do that divide color values for the image by 16. Below is a sample function that acomplishes 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

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2010a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!