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.
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

 채택된 답변

Steve Eddins
Steve Eddins 2021년 1월 12일

1 개 추천

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

True. That makes sense.
Thanks a lot!
If the answer works for you, will you please accept it?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Convert Image Type에 대해 자세히 알아보기

제품

릴리스

R2017b

질문:

2021년 1월 12일

댓글:

2021년 1월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by