search

Home  >  Q&A  >  body text

c语言 - bmp图像如何用c/c++处理?

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

巴扎黑巴扎黑2805 days ago905

reply all(10)I'll reply

  • 怪我咯

    怪我咯2017-04-17 13:11:18

    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);
    };

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 13:11:18

    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.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 13:11:18

    OpenCV

    reply
    0
  • 黄舟

    黄舟2017-04-17 13:11:18

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

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:11:18

    imagick http://www.imagemagick.org

    reply
    0
  • 黄舟

    黄舟2017-04-17 13:11:18

    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.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:11:18

    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.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 13:11:18

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

    reply
    0
  • 阿神

    阿神2017-04-17 13:11:18

    opencv gdal

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 13:11:18

    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.

    reply
    0
  • Cancelreply