How to reduce the bit depth of the bmp image from 8-bit to n-bit, where n<8 andn>=1. Choose n=3.

25 views (last 30 days)
-bit depth reduction -what function to be used
  4 Comments
Mayan Rumbak
Mayan Rumbak on 3 Nov 2020
I need something similar...
I have a 4-bit depth BMP that I would like to read - manipulate - and save back as BMP with 4-bit depth.
but I don't know how to do it...
Does anyone have an idea?
Image Analyst
Image Analyst on 3 Nov 2020
Yes:
rgbImage = imread(filename); % Read in.
% Code to manipulate - whatever that means.
% Make 4 bit
if max(rgbImage(:)) > 15 % If it's more than 4 bits now, rescale
rgbImage = rescale(rgbImage, [0, 15]);
end
% Save back out.
imwrite(outputFileName, uint8(rgbImage));

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 28 Mar 2015
Just divide the image by a power of 2. For example to convert an 8 bit image to another, lower bits image:
image7bit = image8bit / 2;
image6bit = image8bit / 4;
image5bit = image8bit / 8;
image4bit = image8bit / 16;
image3bit = image8bit / 32;
image2bit = image8bit / 64;
image1bit = image8bit / 128;
  1 Comment
Image Analyst
Image Analyst on 28 Mar 2015
sunnie, this does not change the number of bits to n. It changes the number of colors to n. The resulting image is an indexed image - an array where the values go from 1 to n. The image is an 8 bit image but the values go from 1 to 4 in your case. 4 can be represented with 3 bits (4 in binary is 100). Then there's a color map with it that specifies what each RGB color will be for the values 1, 2, 3, and 4. If you specified 6 you'd get 6 colors, not 64 which is what 6 bits would be. Why do you think you need 4 bits? Why did you not take my advise? You do know, right, that even if the values are less than 8 bits, the values are stored in a uint8 variable which means it will be 8 bits? You can only get 4 bits if you store it in a compressed manner, like with imwrite and tell it to use PNG or some other compressed format. And you would not be guaranteed of getting exactly 4 because you don't really have control over how imwrite() does its compressing. You could do it yourself but I don't want to get into that because I don't think it's necessary for you.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!