2013-06-02 41 views
0

我是OpenCV的新手,目前正在使用Windows Form App(Visual C++ 2008)顯示圖像。本教程指導我在將項目應用於項目頭文件的stdafx.h之前獨立製作頭文件(稱爲cvToBitmap.h)。頭文件用於將IplImage格式的圖像轉換爲位圖格式。但是,當我編譯程序時,它在頭文件程序中發生了50個程序錯誤。錯誤頭文件將IplImage轉換爲位圖

下面是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 ========== 

我不知道,什麼是缺失的部分。請幫助..謝謝。

+0

您需要包含'windows.h' –

回答

0

你應該包含標題BITMAPINFO結構 - 添加#include <windows.h>到你的頭文件。

+0

oke謝謝您的解決方案。 :D真的有用! – anjaryes