Reputation: 10415
I want to convert my original 8-bit depth gray scale image to a 7-bit depth gray scale image (pixel values should be in the range [0 127]
.
I use the below syntax, however pixels with value equal to 255
will change to 128
(it seems they are rounded after division, i.e. 127.5
changes to 128
). How can I resolve this issue and keep my pixel values in the range [0 127]
after division?
RGB = imread('camera_man128x128.png')% read 8-bit image
RGB = RGB*0.5; %change pixel value to be in range to 0~127
%however pixels with value 255 change to 128.
Upvotes: 1
Views: 2058
Reputation: 125874
The easiest way to do it is to use bitshift
:
RGB = bitshift(RGB, -1);
This shifts the bit pattern of each uint8
value one to the right, equivalent to multiplication by 2-1 (i.e. division by 2), such that 255
will become 127
.
Upvotes: 3
Reputation: 19689
Convert your data type to double
and then multiply with 0.5. Use floor
to round towards negative infinity and then convert back to uint8
.
RGB = uint8(floor(double(RGB)*0.5));
Upvotes: 2