Why are there extra bits in the BMP file I generated from MATLAB 7.8 (R2009a)?

7 views (last 30 days)
When I generate a bitmap file from MS Paint in Windows XP, SP3, I do the following:
  • Go to Image->Attributes and set width and height both to 3 pixels.
  • Use View->Zoom->Custom to set magnification to 800%. Select mid-gray from palette and use pencil tool to individually set the pixels.
  • Use the SAVE command to save the image as a 24-Bit Bitmap.
However, when I try to create the same file in MATLAB using the following commands, the file is much larger. There are many bytes of information prepended to my data.
a = [128 128 128; 128 128 128; 128 128 128];
imwrite(a, 'im.bmp', 'bmp')The files are different sizes.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
In this example MS Paint file was saved as a truecolor BMP file (Paint's default setting), whereas the MATLAB file was saved as a 256 Color Bitmap. 256 Color Bitmaps save a colormap along with the image data; this is why the file size is so much larger. If you go into MS Paint and instead save the image as a 256 Color Bitmap, you will see that the resultant file size is comparable with the MATLAB file. The colormap may be different, however, so the file contents are not exactly the same.
To generate a truecolor BMP from MATLAB, the array containing the image data must be a 3 dimensional RGB array. As an example, the following code will generate from MATLAB the truecolor BMP file that was generated from MS Paint:
for k = 1:3
b(:,:,k) = uint8([128 128 128; 128 128 128; 128 128 128]);
end
imwrite(b, 'imTC.bmp', 'bmp')

More Answers (0)

Categories

Find more on Color and Styling in Help Center and File Exchange

Products


Release

R2009a

Community Treasure Hunt

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

Start Hunting!