how to convert double to uint8

710 次查看(过去 30 天)
meenu v
meenu v 2020-9-17
编辑: DGM 2024-2-21
for example
if G is 2052*831 double
i wanna convert it to 2052*831*3 uint8
  3 个评论
the cyclist
the cyclist 2020-9-17
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.

请先登录,再进行评论。

回答(2 个)

Image Analyst
Image Analyst 2020-9-17
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 个评论
Quan Zheng
Quan Zheng 2024-2-21
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 2024-2-21
编辑:DGM 2024-2-21
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.

请先登录,再进行评论。


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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by