BMP file FormatCategory: Computer vision 2012-11-12 21:11 338 person reading Comments (4) Favorite Report
Because the company's main business is image recognition-related, so the image processing, recognition is the focus of my study. Although the writing program is not young, but for the field of image processing, I am still a recruit. Many of the basic concepts are still in the blind area, so want to learn while doing the process, some of the concepts of carding and learning experience to record.
BMP file Format
BMP (Bitmap-file) graphic file, also known as bitmap file, is the graphics file format used by Windows, all image processing software running in Windows environment supports BMP image file format. Each image-drawing operation within the Windows system is based on BMP. A BMP file consists of four parts:
Bitmap File header bitmap Information segment palette bitmap data
A BMP file, which can be expressed in code, is as follows:
typedef struct tagbitmap_file{
Bitmapfileheader Bitmapheader;
Bitmapinfoheader Bitmapinfoheader;
PaletteEntry palette[256];
UCHAR *buffer; UCHAR size 1 bytes (with byte), under VC6
} bitmap_file;
1. bmp File Header: Bitmapfileheader
Word
DWORD
Word
Word
DWORD
} Bitmapfileheader;
Bftype |
Description file type, this value must be 0x4d42, that is, the character ' BM ', otherwise it is not BMP at all |
Bfsize |
Describes the size of the bitmap file, in bytes |
BfReserved1 |
Reserved, must be set to 0 |
BfReserved2 |
Reserved, must be set to 0 |
Bfoffbits |
Describes the byte offset from the beginning of the file header to the actual image data. This parameter is very useful because the length of the bitmap information header and palette varies depending on the situation, so you can use this offset value to quickly read the data in place from the file. |
Next, open a BMP file with notepad++:
(Test BMP file)
Over here:
BFTYPE:0X040D (BM)
bfsize:0x0004a436 = = 304182 bytes = = 297K Bytes, indicating that the size of this bitmap file is 297K bytes, and I see the match:
Skips 4 bytes of reserved bytes,
bfoffbits:0x00000036 = = 54 bytes
2. Bitmap Information segment: Bitmapinfoheader
DWORD
LONG
LONG
Word
Word
DWORD
DWORD
LONG
LONG
DWORD
DWORD
Bisize |
Describes the number of bytes required for the BITMAPINFOHEADER structure |
Biwidth |
Indicates the width of the image, in pixels |
Biheight |
Indicates the height of the image, in pixels. Note: This value, in addition to describing the height of the image, has another use to indicate whether the image is a backward bitmap or a forward bitmap. If the value is a positive number, the image is inverted, that is, the first line of data is actually the last line of the image, and if the value is a negative number, the image is positive. Most BMP files are inverted bitmaps, that is, when the height value is a positive number. |
biplanes |
Represents a BMP picture of the plane genus, obviously the display has only one plane, so constant equals 1 |
biBitCount |
Indicates the number of bits/pixels, with values of 1, 4, 8, 16, 24, or 32. |
Bicompression |
Description The type of image data compression, where:
BI_RGB: No compression Bi_rle8:8 bits per pixel RLE compression encoding, compression format consists of 2 bytes (repeat pixel count and color index); BI_RLE4: RLE compression encoding of 4 bits per pixel, compressed format consists of 2 bytes Bi_bitfields: bits per pixel are determined by the specified mask. Bi_jpeg:jpeg format |
biSizeImage |
Indicates the size of the image, in bytes. When in BI_RGB format, it can be set to 0. |
Bixpelspermeter |
Describes the horizontal resolution, expressed in pixels/meters. |
Biypelspermeter |
Describes the vertical resolution, expressed in pixels/meters. |
biClrUsed |
Describes the number of color indexes in the color table that the bitmap is actually using (set to 0, which means that all palette items are used). |
Biclrimportant |
Indicates that the number of color indexes that have a significant effect on the image display, if 0, is important. |
Bisize: The number of bytes 0x00000028 ==40 bytes of the bitmap information segment structure Bitmapinfoheader
biwidth:352 pixels;
biheight:288 pixels;
Match the picture information:
Biplanes:1
Bibitcount:24 bitmap
Bicompression: no compression;
bisizeimage:0x4a400 = = 304128 bytes, when used in BI_RGB format, can be set to 0, why this is 304128. How did the 304128 come out here?
Original biSizeImage = biwidth*biheight* number of bytes per pixel
So here we should be: 352*288*24/8 = 304128. (We have a bit/pixel here of 24, so the number of bytes per pixel is 3)
Note: Here the biwidth must be a multiple of 4, if not a multiple of 4, you need to take a multiple of 4, such as 241, then take 244; Why must be a multiple of 4. Here is a line alignment issue:
Because Windows has a minimum of 4 bytes in a row scan, when
Picture width × The number of bytes per pixel. = Integer multiples of 4
Fill the missing byte at the back of each line with 0
bixpelspermeter:0x00000000
biypelspermeter:0x00000000
biclrused:0x00000000 using all palette entries
biclrimportant:00000000
3. Color palette
Is there a color palette for this BMP image above? The answer is in the negative.
Because from bfoffbits = 54 bytes can be seen, just is sizeof (Bitmapfileheader) +sizeof (Bitmapinfoheader);
So what exactly is a palette of things. What is the use of. Why do we have this picture without the debug board?
Let's start with the tri-color RGB concept.
We know that all colors in nature can be composed of red, green, and Blue (r,g,b). Some colors contain more red components, such as crimson, some contain less red, such as light red. For the number of red components, can be divided into 0 to 255 a total of 256 levels, 0-level means no red components, 255-level indicates that contains 100% red ingredients. Similarly, green and blue are also divided into 256 levels. This classification concept is called quantification.
Table 1.1 RGB combined values for common colors
When each pixel in a picture is given a different RGB value, it can show a colorful color, thus forming a color map.
Let's illustrate what a palette is. Why you need a palette.
There is a color graph with a length width of 200 pixels and a color number of 16 colors, each of which is represented by the R, G, and b three components. Because each component has 256 levels, it is represented by a 8-bit (bit), which is a byte (byte), so each pixel needs 3 bytes. The whole image needs to be 200x200x3, about 120k bytes, not a small number. If we use the following method, we can save more.
Because it's a 16-color chart, which means that there are up to 16 colors in this picture, we can use one table: each row in the table records the R, G, and b values of a color. So when we represent a pixel color, we just need to point out that the color is in the first line, that is, the index value of the color in the table. For example, if the No. 0 behavior of a table is 255,0,0 (red), then when a pixel is red, only 0 can be indicated.
Let's take another look: 16 states can be represented by 4 bits (bit), so one pixel takes half a byte. The entire image needs to be 200x200x0.5, about 20k bytes, plus the table takes up bytes of 3x16=48 bytes. The total number of bytes occupied is about 1/6 of the preceding, save a lot of it.
This table of R, G, B is what we often call the palette (Palette), and the other is the Color lookup table Lut (look up table), which seems more accurate. The structure of the palette in Windows is defined as follows:
So why do we have this BMP without a palette?
is because we this BMP is a 24-bit true color bmp, so-called True Color graph (true color), it is the number of colors up to 256x256x256 species, that is, we mentioned above the R, G, B color representation of all the colors in the method. A true Color chart does not mean that a picture contains all the colors, but that it has the ability to display all colors, which can contain up to all colors. When representing a true color graph, each pixel is represented directly by the R, G, b three component bytes instead of the palette technique. The reason is obvious: if you use a palette, it means 24 bits for a pixel, because the index of each color is 24 bits (because there is a total of 256x256x256 colors, that is, the palette has 256x256x256 rows), and the number of bytes directly represented by the R,g,b three components. Not only is it inexpensive, but it also adds a large palette of 256x256x256x3 bytes. So the true color graph directly with R, G, b three components, it is also called a 24-bit color map.
So it seems that BMP file can not be generalize, whether it uses a color palette or an RGB mask, the real meaning of data in the bitmap data directly related to biBitCount, different types of bitmaps, wherein the design principle is different, the following is a comparison:
4. Bitmap data
The above basically the color palette and the relevant color extraction strategy is clear, and then back to our example above. 24-bit BMP graph, each 3 bytes representing one pixel, and 3 bytes representing the component values of R, G, B, respectively