Filter löschen
Filter löschen

how to convert double to uint8

411 Ansichten (letzte 30 Tage)
meenu v
meenu v am 17 Sep. 2020
Bearbeitet: DGM am 21 Feb. 2024
for example
if G is 2052*831 double
i wanna convert it to 2052*831*3 uint8
  3 Kommentare
the cyclist
the cyclist am 17 Sep. 2020
Based on the answers I am seeing, people are guessing different things about what you mean by that third dimension (or ignoring it completely). You need to clarify what you want, so that we are not guessing.
meenu v
meenu v am 17 Sep. 2020
hi, meant an RGB image

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Image Analyst
Image Analyst am 17 Sep. 2020
Try this:
% First convert G to a 3-D variable.
G = cat(3, G, G, G);
% Now convert it to uint8. It will clip values outside the range 0-255, and will round values in the range.
G = uint8(G);
  4 Kommentare
Quan Zheng
Quan Zheng am 21 Feb. 2024
Hi, I have ran into a issue a bit tougher than this. Now I have a 'double' file contains water saturations with 4 decimal points for different labels, and the number of some data are quite close to each other. Consequently, the similar numbers have been recognized as one integer after multiplying 255 and converting to uint8 and the quantity of water saturation data decreased from 220 to 120. May I ask if there are any methods which can fix this issue? I am appreciate for your time. Thanks!
DGM
DGM am 21 Feb. 2024
Bearbeitet: DGM am 21 Feb. 2024
uint8 and other integer formats can only represent integers over a fixed range. 8-bit integer formats can only represent 256 uniformly-spaced unique values. If you need more precision than that, you either should use a wider integer class (e.g. uint16) or floating point (e.g. double). What's appropriate or acceptable depends on what the end goal is. If it's required to represent it as an image which has broad decoder support, float probably won't be an option, but uint16 may be. In any case, don't use JPG.
That said, there are always ways that one could cause unnecessarily severe data loss due to truncation or rounding depending on the particular ways the conversion is handled. I have to mention that, since I haven't seen the code you used.
Also consider that if only part of your data requires higher resolution (e.g values nearest zero), you might consider storing a nonlinear transformation of the data (e.g. sqrt(mydata)).
% unit-scale float data with a bunch of closely spaced values near zero
data = [0:0.3:5 100:10:150].'/255;
% store the data by scaling linearly
udata1 = im2uint8(data);
fdata1 = im2double(udata1) % uniqueness gets lost due to rounding
fdata1 = 23×1
0 0 0.0039 0.0039 0.0039 0.0078 0.0078 0.0078 0.0078 0.0118
% store nonlinearly-scaled data
g = 0.5;
udata2 = im2uint8(data.^g);
fdata2 = im2double(udata2).^(1/g) % uniqueness is preserved
fdata2 = 23×1
0 0.0012 0.0022 0.0035 0.0044 0.0062 0.0068 0.0081 0.0096 0.0104
Of course, that might not help much with the total rounding error, but it at least helps preserve uniqueness. This should probably shouldn't be considered unless simpler options are exhausted.

Melden Sie sich an, um zu kommentieren.


Samiu Haque
Samiu Haque am 17 Sep. 2020
Use cast() function.
cast(G,'uint8');
  1 Kommentar
the cyclist
the cyclist am 17 Sep. 2020
See my comment, and note that OP says they are converting
2052*831
double to
2052*831*3
uint8.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by