Azzera filtri
Azzera filtri

Reading image into 16 bit signed int

15 visualizzazioni (ultimi 30 giorni)
Deep P
Deep P il 7 Dic 2016
Modificato: DGM il 12 Giu 2024
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 Commenti
Mike
Mike il 7 Dic 2016
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.
dpb
dpb il 7 Dic 2016
As for the last, read beginning at <Matlab numeric types>

Accedi per commentare.

Risposte (1)

DGM
DGM il 6 Giu 2024
Modificato: DGM il 12 Giu 2024
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

Categorie

Scopri di più su Image Processing Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by