Reading image into 16 bit signed int

4 次查看(过去 30 天)
Deep P
Deep P 2016-12-7
编辑: DGM 2024-6-12
Hello,
How do I read an image into 16 bit signed integer array?
I tried using im2int16 in-built function, but I do not have Image Processing Toolbox. Please let me know if there is any other way to do it.
Also what is the difference between signed integer and double?
Thank you.
  2 个评论
Mike
Mike 2016-12-7
Try using the imread() function. I believe it is part of the standard MatLab environment. The returned image data format will depend on the source file. Most images (JPEG,BMP) are 8 bit and return unsigned 8 bit data. TIFF files can be 8 or 16 bit. uint16() will convert to unsigned 16 bit.

请先登录,再进行评论。

回答(1 个)

DGM
DGM 2024-6-6
编辑:DGM 2024-6-12
There's no elegant and succinct way of doing this with base tools alone, considering that you'll potentially need to support multiple input classes if you want to have a tool with general utility. This is slower than optimal, but should cover most bases.
% an image
inpict = imread('peppers.png'); % uint8
% the specified output class
outclass = 'int16';
% get the scaling parameters
if isfloat(inpict)
ir = [0 1];
elseif isinteger(inpict)
inclass = class(inpict);
ir = double([intmin(inclass) intmax(inclass)]);
else
error('unsupported input class')
end
switch lower(outclass)
case {'single','double'}
or = [0 1];
case {'uint8','uint16','uint32','int8','int16','int32'}
or = double([intmin(outclass) intmax(outclass)]);
otherwise
error('unsupported output class')
end
scalefactor = diff(or)/diff(ir);
% do the rescaling
switch lower(outclass)
case {'uint8','uint16','uint32','single','double'}
outpict = cast((double(inpict) - ir(1))*scalefactor + or(1),outclass);
case {'int8','int16','int32'}
% signed outputs need to be rounded prior to offset if you want to
% be consistent with unsigned outputs and IPT tools
outpict = cast(round((double(inpict) - ir(1))*scalefactor) + or(1),outclass);
end
... but why do all that when there are complete tools that already exist? MIMT imcast() is to IPT im2double() and im2uint8() what cast() is to double() and uint8(). The fact that it's parametric is normally the appeal, but what's important here is that it does not require IPT. It also supports a broader range of numeric classes than IPT tools do.
% inpict can be 'single','double','logical',
% or any unsigned or signed 8, 16, 32, or 64-bit integer class
outpict = imcast(inpict,'int16'); % one line

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by