I have been reading books about image processing in VC ++ recently, and finally clarified several concepts, structures, and classes that have been obfuscated before, as shown below. If you have any mistakes, please criticize and correct them. Thank you very much. In the next step, I want to learn more about opencv and hope to summarize some things.
1. DDB and DIB bitmap
A Windows bitmap is actually a bit array corresponding to the display pixel. It has two types: a bitmap called GDI (Graphic device interface, the other is the DIB bitmap (device-independent Bitmap ).
A GDI bitmap contains a Windows data structure related to the Windows GDI module. The data structure is related to devices. Therefore, a bitmap is also called a device-Dependent Bitmap ). When the user program obtains the bitmap data information, the bitmap display method depends on the display card. Due to the device dependency of the GDI bitmap, when the bitmap is transmitted to another PC over the network, problems may occur.
DiB has many programming advantages over GDI bitmap. For example, it comes with color information, making it easier to manage the palette. In addition, any machine running Windows can process Dib, which is usually stored in the disk as A. BMP file or as a resource in the EXE or DLL file of the program.
Ii. cbitmap class and bitmap Structure
The cbitmap class inherits from cgdiobject and encapsulates the bitmap of the graphic device interface (GDI). It provides a member function to manipulate the bitmap. Use a cbitmap object to construct the object. Use one of the initialization member functions to connect a bitmap handle to the object, and then call the member functions of the object.
The cbitmap class is mainly used to process DDB bitmaps. It encapsulates data structures and operation functions related to DDB bitmap operation functions.The struct bitmap defines the DDB bitmap type, width, height, color, and pixel value. Its definition is as follows:
Typedef struct _ tagbitmap
{
Long bmtype; // set to 0
Long bmwidth; // width in pixels
Long bmheight; // height in pixels
Long bmwidthbytes; // width of row in bytes
Word bmplanes; // Number of color planes
Word bmbitspixel; // number of bits per pixel
Lpvoid bmbits; // pointer to pixel bits
}
Bitmap, * pbitmap;
The member functions of cbitmap, such as loadbitmap, createcompatiblebitmap, setbitmapbits, and getbitmap, define operations such as loading, creating, setting bit values, and querying attributes of DDB bitmaps.
You must use the CDC: SelectObject function to select the bitmap that creates or loads the memory into the device context, and then use the bitblt or stretchblt function of CDC to display the bitblt of the two functions. The prototype of these two functions is as follows:
Bool bitblt (int x, int y, int nwith, int nheight, CDC * psrcdc, int xsrc, int ysrc, DWORD dwrop );
This function copies the bitmap in the context of the source device to the device context. The two devices can be either the memory device or the same device context.
Iii. Image and bitmap
In VC ++. in. net, the image class of GDI + encapsulates BMP, GIF, JPEG, PNG, Tiff, WMF (Windows Meta files), and EMF (enhanced WMF) functions of image file transfer, display, format conversion, and simple processing (such as scaling, rotation, and stretching.The bitmap class (note that bitmap is not a structure) is an image inherited from the image class (the other class inherited from the image is the Metafile class), which encapsulates common features of Windows bitmap operations.For example, bitmap: setpixel and bitmap: getpixel are used to read and write bitmap pixels respectively, which provides a possibility for image softening and sharpening. These functions are basically the same as the new cimage function of MFC. If they are only used for image reading and display, bitmap or image is a good choice. If they are used for image processing, then cimage may be more in line with the programming habits of MFC programmers.
Iv. cimage class
The cimage class is VC ++. net. It can be transferred from an external disk to an image file in JPEG, GIF, BMP, and PNG formats for display, and these file formats can be converted to each other.
Cimage can process both DIB bitmap and non-DIB bitmap.But you can use the DIB bitmap to create or cimage: load. You can also use attach to connect a non-DIB bitmap to a cimage object, but you cannot use the following methods. These methods only support DIB bitmap: getbits, getcolortable, getmaxcolortable, entries, getpitch, getpixeladdress, isindexed, and setcolortable. To determine whether a connected bitmap is a DIB bitmap, call isdibsection.
Because cimage has different performance in different Windows operating systems, pay special attention to it during use. For example, cimage: plgblt and cimage: maskblt can only be used in Windows NT 4.0 or later versions, but cannot run in Windows 95/98 applications. Cimage: alphablend and cimage: transparentblt can only be used in Windows 2000/98 or later versions. Even if you are running a program in Windows 2000, you must change the prefix of winver and _ win32_winnt in the stdafx. h file to 0x0500 for normal use. Cimage can be used in MFC or ATL. When using cimage to create a project, it must contain the atlimage. h file.
Cimage encapsulates the Dib (device-independent Bitmap) function, so that we can process each bitmap pixel.
Cimage provides the hbitmap operator. Therefore, hbitmap can be replaced by cimage.
V. Traditional image processing methods in VC ++ and image processing methods in VC ++. net
Because the dib diagram does not depend on a specific device, it can be used to permanently store images. DiB generally uses*. BMP fileStored in the disk, sometimes stored in*. Dib File. Applications running on different output devices can exchange images through DiB. Therefore, DIB bitmap is often used in digital image processing.Dib can also use an RLE Algorithm to compress image data, but DiB is generally not compressed.
Before VC ++. net, MFC did not provide ready-made classes to encapsulate Dib, which caused a lot of inconvenience to MFC users.To use Dib, you must first understand the dib structure.
In the memory, a complete Dib consists of two parts: a bitmapinfo structure and an array that stores pixel arrays. Bitmapinfo describes the bitmap size, color mode, color palette, and other attributes. It is defined as typedef struct tagbitmapinfo {
Bitmapinfoheader bmiheader;
Rgbquad bmicolors [1]; // color table
} Bitmapinfo;
The rgbquad structure is used to describe the color, which is defined
Typedef struct tagrgbquad {
Byte rgbblue; // blue intensity
Byte rgbgreen; // green intensity
Byte rgbred; // red intensity
Byte rgbreserved; // reserved byte, 0
} Rgbquad;
Note that the color sequence in the rgbquad structure is BGR rather than RGB.
The bitmapinfoheader structure contains various Dib information, which is defined
Typedef struct tagbitmapinfoheader {
DWORD bisize; // the size of the Structure
Long biwidth; // The bitmap width (in pixels)
Long biheight; // The height of the bitmap (in pixels)
Word biplanes; // It must be 1
Word bibitcount // The number of bits per pixel (1, 4, 8, 16, 24, or 32)
DWORD bicompression; // compression method. Generally, it is 0 or bi_rgb (uncompressed)
DWORD bisizeimage; // the size of the image in bytes (only used to compress the bitmap)
Long bixpelspermeter; // describe the horizontal resolution of Bitmap Based on the number of bytes per meter on the target device
Long biypelspermeter; // describe the vertical resolution of a bitmap based on the number of bytes per meter on the target device
DWORD biclrused;/* color number of the color table. If it is 0, the bitmap uses the maximum color number specified by bibitcount */
DWORD biclrimportant; // number of important colors. If this value is 0, all colors are important.
} Bitmapinfoheader;
Different from DDB, the dib byte array is stored row by row starting from the bottom row of the image, that is, the image is inverted and then scanned row by row. In addition, the number of bytes in each scanned row in the byte array must be a multiple of 4. If the number is not enough, fill it with 0.
Dib can be stored in *. BMP or *. Dib files. The Dib file starts with the bitmapfileheader structure and is defined
Typedef struct tagbitmapfileheader {
Word bftype; // file type, which must be "BM"
DWORD bfsize; // File Size
Word bfreserved1; // 0
Word bfreserved2; // 0
DWORD bfoffbits; // the offset of the stored pixel array relative to the file header
} Bitmapfileheader;
The structure is followed by a bitmapinfoheader structure, followed by a color table consisting of the rgbquad structure (if any). The final storage of the file is the dib pixel array.
DiB color information is stored in its own color table. Generally, the program creates a logical color palette for DIB based on the color table. Before outputting A Dib, the program should select its logical color palette into the context of the relevant device and implement it into the system color palette, and then call the relevant GDI function (such :: setdibitstodevice or: stretchdibits) Output DiB. In the output process, the GDI function converts DIB to DDB. This work involves two steps: converting the dib color format into the same color format as the output device; converts the logical color index of Dib pixels into a system palette index.
If you see so many structures, are you already dazzled, but even more unfortunate. The Dib mentioned above is DIB in windows, and the DiB is OS/2 dib. The main difference between DiB and Windows DiB is the bitmap information structure (Information header and color table structure ). Their image data is stored in the same way. In OS/2 Dib, bitmapfileheader, bitmapinfoheader, and rgbquad correspond to bitmapcoreheader, bitmapcoreinfo, and rgbtriple respectively. Their definitions are as follows:
Typedef struct tagbitmapcoreheader // bmch
{
DWORD bcsize; // size of the structure = 12
Word bcwidth; // width of image in pixels
Word bcheight; // height of image in pixels
Word bcplanes; // = 1
Word bcbitcount; // bits per pixel (1, 4, 8, or 24)
}
Bitmapcoreheader, * pbitmapcoreheader;
Typedef struct tagbitmapcoreinfo // bmci
{
Bitmapcoreheader bmciheader; // core-header Structure
Rgbtriple bmcicolors [1]; // Color Table Array
}
Bitmapcoreinfo, * pbitmapcoreinfo;
Typedef struct tagrgbtriple // rgbt
{
Byte rgbtblue; // Blue Level
Byte rgbtgreen; // green level
Byte rgbtred; // RED LEVEL
}
Rgbtriple;
Therefore, you should firstDetermine the dib type based on the bitmapinfoheader size.And then perform the operation.
Because MFC does not provide a encapsulated and easy-to-use DIB class, users will face heavy Windows API programming tasks when using DiB. Do not believe that you can refer to the diblook routine of MFC provided by Microsoft. Therefore, traditional image processing methods generally encapsulate the APIs of DIB bitmap operations in these Win32 sdks as a common class to reduce the programming burden in subsequent algorithm writing.
VC ++. NET provides input/output modules for a variety of common image file formats (such as BMP, Tif, GIF, JPEG, and PNG). The cimage class in ATL greatly simplifies image data operations, therefore, VC ++.. Net image processing should be based on cimage.
There are some titles on the market that use VC ++. net image processing books, but the programming method is still an old one, without jumping out of the device-related Bitmap (DDB) and device-independent Bitmap (DIB) Concept of the box, the programming method is complex, cumbersome, and inefficient. Using the VC ++. NET environment for image processing should be based on the cimage class and completely jump out of the DDB and DiB concepts to make the processing method simple and simplified.
Reprinted from:Http://conner-wang.spaces.live.com