如何读取缩放的12位图像? [英] How does imread scale 12bit images?
问题描述
我有一个12位的pgm图像,我用imread读取了它.结果是一个16位图像,其值在0到2 ^ 16-1的整个范围内.
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.
Matlab如何缩放?会
How does Matlab scale? Will
X = imread('filename');
X = uint16(double(X)*((2^12-1)/(2^16-1)));
恢复原始强度吗?
推荐答案
MATLAB确实正确加载了PGM 12位图像.但是,在MATLAB加载图像后,图像值将从12位重新缩放为16位.
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使用以下算法将值从12位缩放到16位:
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.
解决方法就是这样
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
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
这篇关于如何读取缩放的12位图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!