c语言 - bmp图像如何用c/c++处理?
巴扎黑
巴扎黑 2017-04-17 13:09:18
0
10
875

求代码,用c/c++进行bmp图像的处理,处理内容包括旋转,灰度图,二值图等等。

巴扎黑
巴扎黑

reply all(10)
刘奇

What a coincidence?
I found that all the libraries found are very huge and have a high level of abstraction, which cannot meet the needs.
I just wrote a C++ class for reading and writing bmp, which supports 8-bit grayscale images and 24-bit RGB images.

https://github.com/edimetia3d/simpleBMP

The code is very short. In fact, you can understand what is done by looking at the source code for 10 minutes.

A brief explanation:
There is no abstraction. Reading is to map all the data in the bmp (yes, every byte is used) to a defined variable.
Saving means writing these variables back to disk.

In fact, I originally planned to write it completely in C. I am not too interested in dynamic memory, so I used vector
For the read and write part, I still use fread fwrite. If the vector part is replaced by dynamic memory, it can be used in C Running below.
For the meaning of each variable, see
http://www.cnblogs.com/xiekeli/archive/2012/05/09/2491191.html
The specific mapping relationship is as follows.

typedef struct {
    unsigned short bfType;
    unsigned long bfSize;
    unsigned short bfReserved1;
    unsigned short bfReserved2;
    unsigned long bfOffBits;
} ClBitMapFileHeader;

typedef struct {
    unsigned long biSize;
    long biWidth;
    long biHeight;
    unsigned short biPlanes;
    unsigned short biBitCount;
    unsigned long biCompression;
    unsigned long biSizeImage;
    long biXPelsPerMeter;
    long biYPelsPerMeter;
    unsigned long biClrUsed;
    unsigned long biClrImportant;
} ClBitMapInfoHeader;

typedef struct {
    unsigned char rgbBlue;
    unsigned char rgbGreen;
    unsigned char rgbRed;
    unsigned char rgbReserved;
} ClrgbMap;

class ClImgBMP {
public:
    ClBitMapFileHeader bmpFileHeaderData;
    ClBitMapInfoHeader bmpInfoHeaderData;
    ClrgbMap colorMap[256];
    vector<unsigned char> imgData;

    bool LoadImage(const char* path);
    bool SaveImage(const char* path);
};
伊谢尔伦

Use Windows Imaging Component to read images (compatible with common formats such as bmp, jpg, png, etc.)

1. Use CoCreateInstance to create an IWICImagingFactory
2. Use IWICImagingFactory::CreateDecoderFromFilename to create an IWICBitmapDecoder
3. Use IWICBitmapDecoder::GetFrame to get an IWICBitmapFrameDecoder
4. Use IWICImagingFactory::CreateFormatConverter Create an IWICFormatConverter and Initialize it using your favorite format, and use the object created in the third step to call the IWICFormatConverter::Initialize function
5. If the fourth step is successful, then convert the IWICFormatConverter into an IWICBitmapSource, otherwise convert the IWICBitmapFrameDecoder into an IWICBitmapSource
6. Use IWICImagingFactory::CreateBitmapFromSource to get IWICBitmap from IWICBitmapSource

Use Direct2D to get graphics

Since there is a lot of d2d information, please search it yourself. The first is to create an empty bitmap, then use this bitmap to create an ID2D1RenderTarget, and then use ID2D1RenderTarget::CreateBitmapFromWicBitmap to create an ID2D1Bitmap from the IWICBitmap just obtained. In this way, you have two bitmaps, the first is read from the file, and the second is a blank bitmap you created. Finally, use the various effects, shaders and functions of the render target. Use any effect you like, such as rotation, grayscale, binary, etc., to draw the read bitmap onto the blank bitmap. Finally, save the bitmap and you're done.

Summary

The most difficult part is reading and writing files, and the effects are all ready-made. Direct2D is easier to use than GDI+, has higher performance, and is in no way compatible with Windows XP.

大家讲道理

OpenCV

黄舟

I usually use this library myself
https://github.com/nothings/stb

巴扎黑

imagick http://www.imagemagick.org

黄舟

The BMP format is really too simple. To be honest, there is nothing wrong with interpreting it by hand.

Depending on the size of your specific needs and the quality of your hardware (extreme assumption: such as a low-end 32-bit ARM Cortex M0 microcontroller), any of the following is a good choice:

  • opencv, itself a c++ library

  • imagemagick’s magick++ library http://www.imagemagick.org/script/magick++.php

  • There are many simple and specialized bmp libraries to choose from, but usually these libraries can only read bmp.
    For example, I have used EasyBMP http://easybmp.sourceforge.net/

  • It’s okay to do it by hand

In fact, opencv is most recommended. This can maximize the convenience of inputting other file formats and subsequent various graphics algorithms.

Ty80

There are two ways,
1 Use a third-party library. In addition to the above mentioned, you can also choose skia

2 Search the Internet for a code that reads bmp to a two-dimensional array and a code that writes a two-dimensional array to bmp. You need a relatively simple function that can be implemented by yourself. Rotation relies on matrix transformation and interpolation. Grayscale images and binary images can be calculated directly according to the definition.

小葫芦

STB
https://github.com/nothings/stb
FreeImage
NvImage
There is also a library I made myself
ImagePP
https://github. com/lalalaring/ImagePP

阿神

opencv gdal

刘奇

BMP data is not compressed in any way and is placed after the file header. It is not difficult to decode by hand. It is mainly file header + data. And the file header is not difficult, it is very clear. For the specific BMP header, the author can search for it.

I suggest that if you have time, you can solve it by hand, so that you can better understand the format of BMP files.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!