How can imread scale 12-bit images?
I have a 12 bit pgm image that I am reading using imread. The result is a 16-bit image that has values ββin the entire range from 0 to 2 ^ 16 - 1.
How does Matlab scale? Will be
X = imread('filename');
X = uint16(double(X)*((2^12-1)/(2^16-1)));
restore the original intensities?
+3
source to share
1 answer
MATLAB loads 12-bit PGM images correctly. However, after MATLAB loads the images, the image values ββare scaled from 12-bit to 16-bit.
MATLAB uses the following algorithm to scale values ββfrom 12 to 16 bits:
% 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.
The workaround for this is like
function out_image = imreadPGM12(filename)
out_image = imread(filename);
out_image = floor(out_image./16);
return
Alternatively, do a 4-bit right shift:
function out_image = imreadPGM12(filename)
out_image = imread(filename);
out_image = bitshift(out_image,-4);
return
More 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
+5
source to share