0
votes

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.
2
You cannot have 7-bit data type in MATLAB. You can rescale your data to 0-127 integers though. Do you want that?Sardar Usama
@SardarUsama yes, that't right. I want to store values in unit8 but only I want to change data scale from 0~127.VSB
Floor your data, or divide by the max in your data after division and the multiply by 127Adriaan
Please edit your question to include the information you provided in the last comment and remove the ambiguous term '7-bit'Sardar Usama

2 Answers

2
votes

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.

1
votes

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));