我有一个12位的pgm图像,我用imread读取。结果是一个16位图像,其值在0到2 ^ 16-1的整个范围内。
Matlab如何扩展?将
X = imread('filename');
X = uint16(double(X)*((2^12-1)/(2^16-1)));
恢复原始强度?
答案 0 :(得分:5)
MATLAB会正确加载PGM 12位图像。但是,在MATLAB加载图像后,图像值将从12位重新调整为16位。
MATLAB使用以下算法将值从12位缩放到16位:
% 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.
解决方法就是这样
function out_image = imreadPGM12(filename)
out_image = imread(filename);
out_image = floor(out_image./16);
return
或者向右执行4位移位:
function out_image = imreadPGM12(filename)
out_image = imread(filename);
out_image = bitshift(out_image,-4);
return