Reputation: 73
I already know uint8
contains intensity values between 0 and 255 (28-1) and single contains values between 0 and 1, it is used to hold larger values without upsetting the range error. But, apart from that, are there any other differences?
What is the difference between imagesc((I/64)*64)
and imagesc((Is/64)*64)
, where I
is uint8
and Is
is single
?
Upvotes: 1
Views: 4143
Reputation: 18187
I = uint8(255*rand(1e3));
Is = single(I)/255;
tmpI = (I/64)*64;
tmpIs = (Is/64)*64;
% plot for prosterity
% figure;
% subplot(211)
% imagesc(tmpI)
% subplot(212)
% imagesc(tmpIs)
numel(unique(tmpI(:))) % gives 5
numel(unique(tmpIs(:))) % gives 256
Dividing an integer basically means binning of values, and then it stretches the data back to the original extend for plotting. In this case, you get 256/64 = 4
bins, with 0 as well, thus 5 possible values for your uint8
image. However, using single
you retain all unique numbers, since the precision is a lot higher.
If you'd do the same test with a lot (order 2^52) elements in the rand
and use double
you'd see that that again has 2^32 times the number of unique elements of single
, just as uint16
will have 2^8 the number of unique elements of uint8
.
Upvotes: 1
Reputation: 24179
imagesc
just calls image
underneath. As for image
, it behaves a bit differently if integers or floats are supplied, as can be learned from image
's documentation:
If
C
is of type double, then an RGB triplet value of[0 0 0]
corresponds to black and[1 1 1]
corresponds to white.If
C
is an integer type, then the image uses the full range of data to determine the color. For example, ifC
is of typeuint8
, then[0 0 0]
corresponds to black and[255 255 255]
corresponds to white. IfCData
is of typeint8
, then[-128 -128 -128]
corresponds to black and[127 127 127]
corresponds to white....
Converting Between Data Types
To convert indexed image data from an integer type to type
double
, add 1. For example, ifX8
is indexed image data of typeuint8
, convert it to typedouble
using:X64 = double(X8) + 1;
To convert indexed image data from type
double
to an integer type, subtract 1 and useround
to ensure that all the values are integers. For example, ifX64
is indexed image data of typedouble
, convert it touint8
using:X8 = uint8(round(X64 - 1));
To convert true color image data from an integer type to type
double
, rescale the data. For example, ifRGB8
is true color image data of typeuint8
, convert it to double using:RGB64 = double(RGB8)/255;
To convert true color image data from type
double
to an integer type, rescale the data and useround
to ensure that all the values are integers. For example, ifRGB64
is image data of typedouble
, convert it touint8
using:RGB8 = uint8(round(RGB64*255));
Upvotes: 3