Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to output 8 bit BMP file with imwrite

554 views
Skip to first unread message

Brian

unread,
Jun 18, 2009, 7:41:47 PM6/18/09
to
I was hoping to use matlab to convert a large number of 24 bit depth
bitmaps to 8 bit, but despite both being supported I can't seem to
find a way to make imwrite export as anything other than 24 bit. Is
there any way to achieve this with imwrite or another function? The
code I am using is below and makes 24 bit depth copies of the images
as is.

for x=0:39
pic=strcat('location1',num2str(x),'.bmp');
pic2=strcat('location2',num2str(x),'.bmp');
a = uint8(imread(pic));
imwrite(a,pic2,'bmp');
end

This came from the need to convert the frames of an avi to 8 bit depth
bitmaps, and all the programs I could find not giving the option to
export at lower bit depths. So if anyone knows how to get matlab or
really anything to extract the frames as 8 bit depth bitmaps on its
own then that would be just as useful if not more.

Nicolaie Popescu-Bodorin

unread,
Jun 18, 2009, 8:26:01 PM6/18/09
to
Brian,

pic=strcat('location1',num2str(x),'.bmp');
pic2=strcat('location2',num2str(x),'.bmp');
a = uint8(imread(pic));
imwrite(a,pic2,'bmp');

testa=imread(pic2);
isa(testa,'uint8')
imfinfo(pic2)

The image is uint8 ...

Regards,
Bodorin

Jim Rockford

unread,
Jun 18, 2009, 8:35:21 PM6/18/09
to
On Jun 18, 8:26 pm, "Nicolaie Popescu-Bodorin"


I think he was asking something different. Both 8 bit BMP and 24 bit
BMP are stored as uint8. If you load in a BMP file that is 24 bit,
the imfinfo function will reveal that the bit depth parameter is 24.
For an 8 bit BMP, the bit depth parameter is 8. I believe he is
wondering how to convert a BMP with 24 bit bit depth into one with 8
bit depth.

I had wondered this myself at one point and am curious to know if
Matlab can do this. Anyone know?

Jim


ImageAnalyst

unread,
Jun 18, 2009, 8:36:46 PM6/18/09
to

----------------------------------------------------------------------------------------------------
Brian:
You extract them as they are written. If you want to convert them
after that, then that's fine but it's an extra step. Did you convert
"a" to a monochrome image?
It's probably still a 24 bit RGB image.
Here's some sample code that may help you:
clc;
clear all;
close all;
workspace;
% Read in standard RGB MATLAB demo image.
rgbImage = imread('peppers.png');
[rows columns numberOfColorPlanesRGB] = size(rgbImage)

% Convert to monochrome using default formula.
monoImage = uint8(rgb2gray(rgbImage));
[rows columns numberOfColorPlanesMono] = size(monoImage)
filename = 'rgbImage.bmp';
imwrite(rgbImage, filename, 'bmp');
filename = 'monoImage.bmp';
imwrite(monoImage, filename, 'bmp');

% Test that it's still 8 bit monochrome by reading it back in.
readBackIn = imread(filename);
[rows columns numberOfColorPlanesNew] = size(readBackIn)

Note that on disk the monochrome version is 194 kb while the RGB
version is 577 kb, so that also verfies that it's 8 bit monochrome.

Message has been deleted

Nicolaie Popescu-Bodorin

unread,
Jun 18, 2009, 9:26:01 PM6/18/09
to
you could build your own codec storing 4 bit luminance, 4 bit red-blue chrominance.
or 8 bit luminance, 8 bit red-blue chrominance.
But usually, 8bit images are gray, 24bit images are RGB.

Regards,
Bodorin

Brian <abi...@gmail.com> wrote in message <0b497d67-2222-4d68...@l32g2000vba.googlegroups.com>...
> How to do that conversion step is what I'm looking for. What you've
> posted is useful, but ideally I'd still have a color image after the
> conversion, simply with less color variation than the 24 bit depth.

0 new messages