0

我是 OpenCV 的新手,目前正在关注使用 Windows Form App (Visual C++ 2008) 显示图像的教程。本教程指导我在将其应用于项目中头文件的 stdafx.h 之前独立制作一个头文件(称为 cvToBitmap.h)。头文件的作用是将 IplImage 格式的图像转换为位图格式。但是,当我编译程序时,在头程序中出现了 50 个程序错误。

下面是cvToBitmap.h源程序

#ifndef _CVTOBITMAP_H_
#define _CVTOBITMAP_H_

static void FillBitmapInfo (BITMAPINFO* bmi, int 
    width, int height, int bpp, int origin)
{
    assert ((bmi&&width>=0)&&(height>=0)&&((bpp==8)
        ||(bpp==24)||(bpp==32)));
    BITMAPINFOHEADER* bmih=&(bmi->bmiHeader);
    memset(bmih,0,sizeof(*bmih));
    bmih->biSize=sizeof(BITMAPINFOHEADER);
    bmih->biWidth=width;
    bmih->biHeight=origin?abs(height):-abs(height);
    bmih->biPlanes=1;
    bmih->biBitCount=(unsigned short)bpp;
    bmih->biCompression=BI_RGB;
    if(bpp==8){
        RGBQUAD* palatte = bmi->bmiColors;
        for(int i=0;i<256;i++){
            palette[i].rgbBlue=palette[i].rgbGreen=palette[i].rgbRed=(BYTE)i;
            palette[i].rgbReserved=0;
        }
    }
}

System::Drawing::Bitmap^ IplImageToBitmap(IplImage* src){
    SIZE size = (0,0);
    int channels=0;
    void* dst_ptr=0;
    const int channels0=3;
    int origin=0;
    CvMat stub,dst,*image;
    bool changed_size=false;

    HDC hdc=CreateCompatibleDC(0);
    if(CV_IS_IMAGE_HDR(src))
        origin=src->origin;
    image=cvGetMat(src,&stub);
    uchar buffer(sizeof(BITMAPINFO*)buffer;
    size.cx=src->width;
    size.cy=src->height;
    channels=channels0;
    FillBitmapInfo(binfo,size.cx,size.cy,channels*8,1);
    HBITMAP hBitmap=reateDIBSection(hdc, binfo,DIB_RGB_COLORS, &dst_ptr,0,0);
    if (hBitmap==NULL)
        return nullptr;
    cvInitMatHeader(&dst, size.cy,size.cx,CV_8UC3, dst_ptr, (size.cx*channels+3)&-4);
    cvConvertImage(image, &ddst, origin=0 ? CV_CVTIMG:0);
    System::Drawing::Bitmap^ bmpImage=
        System::Drawing::Image::FromHbitmap(
        System::IntPtr(hBitmap));
    DeleteObject(hBitmap);
    DeleteDC(hdc);
    return bmpImage;
}
#endif 

以下是错误信息

1>C:\opencv\build\include\cvToBitmap.h(6) : error C2065: 'BITMAPINFO' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(6) : error C2065: 'bmi' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(7) : error C2062: type 'int' unexpected
1>C:\opencv\build\include\cvToBitmap.h(8) : error C2143: syntax error : missing ';' before '{'
1>C:\opencv\build\include\cvToBitmap.h(8) : error C2447: '{' : missing function header (old-style formal list?)
1>C:\opencv\build\include\cvToBitmap.h(29) : error C2065: 'SIZE' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(29) : error C2146: syntax error : missing ';' before identifier 'size'
1>C:\opencv\build\include\cvToBitmap.h(29) : error C2065: 'size' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(37) : error C2065: 'HDC' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(37) : error C2146: syntax error : missing ';' before identifier 'hdc'
1>C:\opencv\build\include\cvToBitmap.h(37) : error C2065: 'hdc' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(37) : error C3861: 'CreateCompatibleDC': identifier not found
1>C:\opencv\build\include\cvToBitmap.h(41) : error C2065: 'BITMAPINFO' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(41) : error C2059: syntax error : ')'
1>C:\opencv\build\include\cvToBitmap.h(42) : error C2065: 'size' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(42) : error C2228: left of '.cx' must have class/struct/union
1>        type is ''unknown-type''
1>C:\opencv\build\include\cvToBitmap.h(43) : error C2065: 'size' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(43) : error C2228: left of '.cy' must have class/struct/union
1>        type is ''unknown-type''
1>C:\opencv\build\include\cvToBitmap.h(45) : error C2065: 'binfo' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(45) : error C2065: 'size' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(45) : error C2228: left of '.cx' must have class/struct/union
1>        type is ''unknown-type''
1>C:\opencv\build\include\cvToBitmap.h(45) : error C2065: 'size' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(45) : error C2228: left of '.cy' must have class/struct/union
1>        type is ''unknown-type''
1>C:\opencv\build\include\cvToBitmap.h(45) : error C3861: 'FillBitmapInfo': identifier not found
1>C:\opencv\build\include\cvToBitmap.h(46) : error C2065: 'HBITMAP' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(46) : error C2146: syntax error : missing ';' before identifier 'hBitmap'
1>C:\opencv\build\include\cvToBitmap.h(46) : error C2065: 'hBitmap' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(46) : error C2065: 'hdc' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(46) : error C2065: 'binfo' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(46) : error C2065: 'DIB_RGB_COLORS' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(46) : error C3861: 'reateDIBSection': identifier not found
1>C:\opencv\build\include\cvToBitmap.h(47) : error C2065: 'hBitmap' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(49) : error C2065: 'size' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(49) : error C2228: left of '.cy' must have class/struct/union
1>        type is ''unknown-type''
1>C:\opencv\build\include\cvToBitmap.h(49) : error C2065: 'size' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(49) : error C2228: left of '.cx' must have class/struct/union
1>        type is ''unknown-type''
1>C:\opencv\build\include\cvToBitmap.h(49) : error C2065: 'size' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(49) : error C2228: left of '.cx' must have class/struct/union
1>        type is ''unknown-type''
1>C:\opencv\build\include\cvToBitmap.h(50) : error C2065: 'ddst' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(50) : error C2065: 'CV_CVTIMG' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(51) : error C2065: 'bmpImage' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(51) : error C2275: 'System::Drawing::Bitmap' : illegal use of this type as an expression
1>C:\opencv\build\include\cvToBitmap.h(53) : error C2065: 'hBitmap' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(54) : error C2065: 'hBitmap' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(54) : error C3861: 'DeleteObject': identifier not found
1>C:\opencv\build\include\cvToBitmap.h(55) : error C2065: 'hdc' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(55) : error C3861: 'DeleteDC': identifier not found
1>C:\opencv\build\include\cvToBitmap.h(56) : error C2065: 'bmpImage' : undeclared identifier
1>C:\opencv\build\include\cvToBitmap.h(57) : error C2143: syntax error : missing ')' before '}'
1>C:\opencv\build\include\cvToBitmap.h(57) : error C2143: syntax error : missing ';' before ')'
    1>TampilForm - 50 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我不知道,缺少什么部分..请帮助..谢谢..

4

1 回答 1

0

您应该包含带有BITMAPINFO结构的标题 - 添加#include <windows.h>到您的头文件中。

于 2013-06-02T18:41:13.717 回答