how to convert double to uint8
조회 수: 413 (최근 30일)
이전 댓글 표시
for example
if G is 2052*831 double
i wanna convert it to 2052*831*3 uint8
댓글 수: 3
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
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
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
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
% store nonlinearly-scaled data
g = 0.5;
udata2 = im2uint8(data.^g);
fdata2 = im2double(udata2).^(1/g) % uniqueness is preserved
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
2020년 9월 17일
Use cast() function.
cast(G,'uint8');
댓글 수: 1
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 Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!