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

조회 수: 24 (최근 30일)
-bit depth reduction -what function to be used
  댓글 수: 4
Mayan Rumbak
Mayan Rumbak 2020년 11월 3일
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 2020년 11월 3일
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));

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

답변 (1개)

Image Analyst
Image Analyst 2015년 3월 28일
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
Image Analyst
Image Analyst 2015년 3월 28일
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.

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

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by