无法创建位图图像; fileheader / infoheader是错误的

时间:2013-02-05 13:46:12

标签: c++ image bitmap

我正在尝试从高功率相机(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部分。我可以使用他们提供的软件来查看相机的图像,所以我知道相机工作正常(这也是我用来确保光线足够亮到白色屏幕,黑暗足够黑色屏幕)。但是,无论是fileheader还是infoheader(或两者)都有问题,我似乎无法弄清楚它是什么。

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

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。这些工作正常并简单地返回整数(至少从我在汽车中看到的内容)。

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

TL; DR:我的fileheader和/或infoheader出了什么问题?

1 个答案:

答案 0 :(得分:0)

您可能缺少文件标题中的两个保留字段。

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