Reputation:
Greetings, I am trying to find an easy way to manipulate an image so that I can decrease the quality of it from 8bit to 3bit.
What would be the easiest way to achieve this? Cheers
Upvotes: 3
Views: 2272
Reputation: 34601
If you want to linearly scale it, just divide each pixel value by 255/7 (i.e. if the original image is stored in a matrix I, let the low-res image J = I/(255/7)).
UPDATED: Due to a mistake in my scaling constant.
Here's an example:
Upvotes: 4
Reputation: 125854
If you have an image that is stored as a uint8
type in MATLAB, then the pixel values will range from 0 to 255. To limit the values to only 3 bits of precision (thus using only the numbers 0 to 7), you can scale the data as in the following example:
>> data = uint8([0 23 128 200 255]); % Create some data of type uint8
>> scaledData = data*(7/255)
scaledData =
0 1 4 5 7
>> class(scaledData)
ans =
uint8
Note that even though the scaled values are limited to the range of 0 to 7, the variable storing them is still a uint8
data type since this is the smallest MATLAB will go. The unused higher bits are simply 0.
Depending on how you output the scaled image data to a file (if you want to do that), you may be able to reduce the precision of the stored values to less than 8 bits (for example, PNG files can store 4-bit types).
Upvotes: 2
Reputation: 410
int8
is the smallest integer value in Matlab. You could use only 3 out of 8 bits in int8 by shifting to right any value of any pixel in image.
If you have access to Fixed-Point Toolbox instead, you can use numerictype
objects with the simple notation:
T = numerictype(s,w)
Cit. from Matlab's manual:
T = numerictype(s,w) creates a numerictype object with Fixed-point: unspecified scaling, Signed property value s, and word length w.
Upvotes: 1