Image conversion from uint16 to uint8 mismatch
63 次查看(过去 30 天)
显示 更早的评论
Hey all,
I am a little confused in converting images from uint16 to uint8. There is a matlab function im2uint8 that should do the job, but I would like to avoid using the toolbox and convert using uint8 command. The conversion is done as follows:
u16 = uint16([126 127 128 129 130]) % sample 16bit data
u8 = uint8(u16/256) % scale and convert to 8bits
u8_2 = im2uint8(u16) % compare to built in fcn
However, I get some discrepancies between the two conversions.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/485808/image.png)
For uint16 value of 128, the im2uint8 function gives zero, while the uint8 command gives 1. To my understanding it should be 1, but since Matlab gives it as 0, it made me confused. What am I missing here?
Thanks
Bronius
0 个评论
采纳的回答
Steve Eddins
2021-1-12
im2uint8 is using a scale factor of (255/65535), not 1/256.
Note that scaling the maximum of the uint16 range, 65535, by 1/256 produces 255.9961 (approximately), which is greater than the maximum value in the uint8 range. That's why 1/256 is not used.
2 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!