创建位图图像时遇到问题;fileheader/infoheader错误

Having trouble creating a bitmap image; fileheader/infoheader is wrong

本文关键字:fileheader infoheader 错误 问题 位图 图像 遇到 创建      更新时间:2023-10-16

我正试图根据高功率相机(Allied Vision Technologies Prosilica GT2750)拍摄的图像创建一个24位图图像。到目前为止,我有以下内容:(注意VmbUInt32被定义为"typedef unsigned __int32 VmbUInt32_t;"而VmbUchar_t只是一个无符号字符)

        VmbUint32_t imageSize = 0;
        pFrame->GetImageSize(imageSize); //imageSize = 6054400
        VmbUint32_t imageWidth = 0;
        pFrame->GetWidth(imageWidth);    //imageWidth = 2752
        VmbUint32_t imageHeight = 0;
        pFrame->GetHeight(imageHeight);  //imageHeight = 2200
        //////////////////////////////////////////////////////////////////////////
        //////////  Set Bitmap Settings   ////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////
        //file header
        BITMAPFILEHEADER* bf = new BITMAPFILEHEADER;
        bf->bfType              = 0x4d42;
        bf->bfSize              = imageSize + 40 + 14; //image size + infoheader size + fileheader size 
        bf->bfOffBits           = 54;
        //info header
        BITMAPINFOHEADER* bi = new BITMAPINFOHEADER;
        bi->biSize              = 40;
        bi->biWidth             = imageWidth;
        bi->biHeight            = imageHeight;
        bi->biPlanes            = 1;
        bi->biBitCount          = 24;
        bi->biCompression       = 0;
        bi->biSizeImage         = imageSize;
        bi->biXPelsPerMeter     = 2835;
        bi->biYPelsPerMeter     = 2835;
        bi->biClrUsed           = 0;
        bi->biClrImportant      = 0;

        //image data
        VmbUchar_t* imageData = new VmbUchar_t[imageSize];
        pFrame->GetImage(imageData);

        //////////////////////////////////////////////////////////////////////////
        //////////  Output File to .bmp   ////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////

        std::ofstream outFile;
        outFile.open("test.bmp", std::ios::binary|std::ios::out);
        outFile.write(reinterpret_cast<char *>(bf), sizeof(BITMAPFILEHEADER));
        outFile.write(reinterpret_cast<char *>(bi), sizeof(BITMAPINFOHEADER));
        outFile.write(reinterpret_cast<char *>(imageData), imageSize);
        outFile.close();

基本上,数据被正确地写入test.bmp,但test.bmp在标准图像查看器中仍然无法处理。当我在十六进制编辑器中查看test.bmp时,似乎一切正常。我知道imageData工作正常,因为当我把灯调到最大值时,imageData只是一堆FF(又名白色),当我关灯时,imageData的每个像素都是00、01或02。(又名黑色或接近黑色)。因此,文件的imageData部分被正确写入。我可以使用他们提供的软件来查看相机的图像,这样我就知道相机工作得很好(这也是我用来确保光线足够亮,可以是白色屏幕,足够暗,可以是黑色屏幕)。然而,文件头或信息头(或两者)肯定有问题,我似乎无法弄清楚它是什么

这是文件头和信息头的十六进制数据:

42 4D 36 62 5C 00 CD CD CD CD 36 00 00 00 28 00 00 00 C0 
0D 0A 00 00 98 08 00 00 01 00 18 00 00 00 00 00 00 62 5C 
00 13 0B 00 00 13 0B 00 00 00 00 00 00 00 00 00 00

每次运行的imageSize、imageWidth和imageHeight都相同(分别为6054400、2752、2200),获取它们的函数来自相机的API。这些函数工作正常,只返回整数(至少从我在autos中看到的情况来看)。

其他信息:我正在使用Visual Studio 2010。

TL;DR:我的文件头和/或信息头出了什么问题?

您的File头中可能缺少两个保留字段。

文件头:4字节BMP文件的大小(以字节为单位)预留2个字节;实际值取决于创建图像的应用程序预留2个字节;实际值取决于创建图像的应用程序4字节——可以找到位图图像数据(像素阵列)的字节的偏移量,即起始地址。