解决Opencv高低版本不兼容问题

目前OpenCV版本已更新到2.4...由此出现了一系列问题,解决如下:

1、cxcore.h等头文件找不到:

法一、将opencv1.0中的各种.h或者.lib文件拷到opencv2.3.1对应include/library的路径的文件夹下。


e.g.   Cannot open include file: 'cxtypes.h': No such file or directory

像这种情况,在opencv1.0中又搜索不到的,果断把#include"cxtypes.h"删掉,其他问题慢慢解决。

2、CvFilter未定义:

opencv高级版本不在含有CvFilter,那么就把类型改成int吧……

3、CvvImage类找不到定义……

高级版本中,以前版本的CvvImage类不见了...为了能够继续使用这个类,下面把这个类的源代码贴出来,使用的时候将该代码加入到工程中便可以使用了。为了方便切换OpenCV的版本,这里用到了一些条件编译宏,来保证代码的灵活性。

      不过OpenCV 2.2.0取消CvvImage这个类,一定是有它的原因的,具体可以在做实验的时候体会这些原因。

========================================================================

CvvImage头文件

 

  1. #ifndef CVVIMAGE_CLASS_DEF  
  2. #define CVVIMAGE_CLASS_DEF  
  3.   
  4. #ifndef RC_OPENCV_2_1_0  
  5.   
  6. #include <opencv/cv.h>  
  7. #include <opencv/highgui.h>  
  8.   
  9. /* CvvImage class definition */  
  10. class  CvvImage  
  11. {  
  12. public:  
  13.     CvvImage();  
  14.     virtual ~CvvImage();  
  15.   
  16.     /* Create image (BGR or grayscale) */  
  17.     virtual bool  Create( int width, int height, int bits_per_pixel, int image_origin = 0 );  
  18.   
  19.     /* Load image from specified file */  
  20.     virtual bool  Load( const char* filename, int desired_color = 1 );  
  21.   
  22.     /* Load rectangle from the file */  
  23.     virtual bool  LoadRect( const char* filename,  
  24.         int desired_color, CvRect r );  
  25.   
  26. #if defined WIN32 || defined _WIN32  
  27.     virtual bool  LoadRect( const char* filename,  
  28.         int desired_color, RECT r )  
  29.     {  
  30.         return LoadRect( filename, desired_color,  
  31.             cvRect( r.left, r.top, r.right - r.left, r.bottom - r.top ));  
  32.     }  
  33. #endif  
  34.   
  35.     /* Save entire image to specified file. */  
  36.     virtual bool  Save( const char* filename );  
  37.   
  38.     /* Get copy of input image ROI */  
  39.     virtual void  CopyOf( CvvImage& image, int desired_color = -1 );  
  40.     virtual void  CopyOf( IplImage* img, int desired_color = -1 );  
  41.   
  42.     IplImage* GetImage() { return m_img; };  
  43.     virtual void  Destroy(void);  
  44.   
  45.     /* width and height of ROI */  
  46.     int Width() { return !m_img ? 0 : !m_img->roi ? m_img->width : m_img->roi->width; };  
  47.     int Height() { return !m_img ? 0 : !m_img->roi ? m_img->height : m_img->roi->height;};  
  48.     int Bpp() { return m_img ? (m_img->depth & 255)*m_img->nChannels : 0; };  
  49.   
  50.     virtual void  Fill( int color );  
  51.   
  52.     /* draw to highgui window */  
  53.     virtual void  Show( const char* window );  
  54.   
  55. #if defined WIN32 || defined _WIN32  
  56.     /* draw part of image to the specified DC */  
  57.     virtual void  Show( HDC dc, int x, int y, int width, int height,  
  58.         int from_x = 0, int from_y = 0 );  
  59.     /* draw the current image ROI to the specified rectangle of the destination DC */  
  60.     virtual void  DrawToHDC( HDC hDCDst, RECT* pDstRect );  
  61. #endif  
  62.   
  63. protected:  
  64.   
  65.     IplImage*  m_img;  
  66. };  
  67.   
  68. typedef CvvImage CImage;  
  69.   
  70. #endif  
  71.   
  72. #endif  


  CvvImage源文件

 

    1. #include "stdafx.h"    
    2.     
    3. #ifndef RC_OPENCV_2_1_0    
    4. #include "CvvImage.h"    
    5.     
    6. //////////////////////////////////////////////////////////////////////    
    7. // Construction/Destruction    
    8. //////////////////////////////////////////////////////////////////////    
    9.     
    10. CV_INLINE RECT NormalizeRect( RECT r );    
    11. CV_INLINE RECT NormalizeRect( RECT r )    
    12. {    
    13.     int t;    
    14.     
    15.     if( r.left > r.right )    
    16.     {    
    17.         t = r.left;    
    18.         r.left = r.right;    
    19.         r.right = t;    
    20.     }    
    21.     
    22.     if( r.top > r.bottom )    
    23.     {    
    24.         t = r.top;    
    25.         r.top = r.bottom;    
    26.         r.bottom = t;    
    27.     }    
    28.     
    29.     return r;    
    30. }    
    31.     
    32. CV_INLINE CvRect RectToCvRect( RECT sr );    
    33. CV_INLINE CvRect RectToCvRect( RECT sr )    
    34. {    
    35.     sr = NormalizeRect( sr );    
    36.     return cvRect( sr.left, sr.top, sr.right - sr.left, sr.bottom - sr.top );    
    37. }    
    38.     
    39. CV_INLINE RECT CvRectToRect( CvRect sr );    
    40. CV_INLINE RECT CvRectToRect( CvRect sr )    
    41. {    
    42.     RECT dr;    
    43.     dr.left = sr.x;    
    44.     dr.top = sr.y;    
    45.     dr.right = sr.x + sr.width;    
    46.     dr.bottom = sr.y + sr.height;    
    47.     
    48.     return dr;    
    49. }    
    50.     
    51. CV_INLINE IplROI RectToROI( RECT r );    
    52. CV_INLINE IplROI RectToROI( RECT r )    
    53. {    
    54.     IplROI roi;    
    55.     r = NormalizeRect( r );    
    56.     roi.xOffset = r.left;    
    57.     roi.yOffset = r.top;    
    58.     roi.width = r.right - r.left;    
    59.     roi.height = r.bottom - r.top;    
    60.     roi.coi = 0;    
    61.     
    62.     return roi;    
    63. }    
    64.     
    65. void  FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin )    
    66. {    
    67.     assert( bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));    
    68.     
    69.     BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);    
    70.     
    71.     memset( bmih, 0, sizeof(*bmih));    
    72.     bmih->biSize = sizeof(BITMAPINFOHEADER);    
    73.     bmih->biWidth = width;    
    74.     bmih->biHeight = origin ? abs(height) : -abs(height);    
    75.     bmih->biPlanes = 1;    
    76.     bmih->biBitCount = (unsigned short)bpp;    
    77.     bmih->biCompression = BI_RGB;    
    78.     
    79.     if( bpp == 8 )    
    80.     {    
    81.         RGBQUAD* palette = bmi->bmiColors;    
    82.         int i;    
    83.         for( i = 0; i < 256; i++ )    
    84.         {    
    85.             palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;    
    86.             palette[i].rgbReserved = 0;    
    87.         }    
    88.     }    
    89. }    
    90.     
    91. CvvImage::CvvImage()    
    92. {    
    93.     m_img = 0;    
    94. }    
    95.     
    96. void CvvImage::Destroy()    
    97. {    
    98.     cvReleaseImage( &m_img );    
    99. }    
    100.     
    101. CvvImage::~CvvImage()    
    102. {    
    103.     Destroy();    
    104. }    
    105.     
    106. bool  CvvImage::Create( int w, int h, int bpp, int origin )    
    107. {    
    108.     const unsigned max_img_size = 10000;    
    109.     
    110.     if( (bpp != 8 && bpp != 24 && bpp != 32) ||    
    111.         (unsigned)w >=  max_img_size || (unsigned)h >= max_img_size ||    
    112.         (origin != IPL_ORIGIN_TL && origin != IPL_ORIGIN_BL))    
    113.     {    
    114.         assert(0); // most probably, it is a programming error    
    115.         return false;    
    116.     }    
    117.     
    118.     if( !m_img || Bpp() != bpp || m_img->width != w || m_img->height != h )    
    119.     {    
    120.         if( m_img && m_img->nSize == sizeof(IplImage))    
    121.             Destroy();    
    122.     
    123.         /* prepare IPL header */    
    124.         m_img = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, bpp/8 );    
    125.     }    
    126.     
    127.     if( m_img )    
    128.         m_img->origin = origin == 0 ? IPL_ORIGIN_TL : IPL_ORIGIN_BL;    
    129.     
    130.     return m_img != 0;    
    131. }    
    132.     
    133. void  CvvImage::CopyOf( CvvImage& image, int desired_color )    
    134. {    
    135.     IplImage* img = image.GetImage();    
    136.     if( img )    
    137.     {    
    138.         CopyOf( img, desired_color );    
    139.     }    
    140. }    
    141.     
    142.     
    143. #define HG_IS_IMAGE(img)                                                      
    144.     ((img) != 0 && ((const IplImage*)(img))->nSize == sizeof(IplImage) &&     
    145.     ((IplImage*)img)->imageData != 0)    
    146.     
    147.     
    148. void  CvvImage::CopyOf( IplImage* img, int desired_color )    
    149. {    
    150.     if( HG_IS_IMAGE(img) )    
    151.     {    
    152.         int color = desired_color;    
    153.         CvSize size = cvGetSize( img );     
    154.     
    155.         if( color < 0 )    
    156.             color = img->nChannels > 1;    
    157.     
    158.         if( Create( size.width, size.height,    
    159.             (!color ? 1 : img->nChannels > 1 ? img->nChannels : 3)*8,    
    160.             img->origin ))    
    161.         {    
    162.             cvConvertImage( img, m_img, 0 );    
    163.         }    
    164.     }    
    165. }    
    166.     
    167.     
    168. bool  CvvImage::Load( const char* filename, int desired_color )    
    169. {    
    170.     IplImage* img = cvLoadImage( filename, desired_color );    
    171.     if( !img )    
    172.         return false;    
    173.     
    174.     CopyOf( img, desired_color );    
    175.     cvReleaseImage( &img );    
    176.     
    177.     return true;    
    178. }    
    179.     
    180.     
    181. bool  CvvImage::LoadRect( const char* filename,    
    182.                          int desired_color, CvRect r )    
    183. {    
    184.     if( r.width < 0 || r.height < 0 ) return false;    
    185.     
    186.     IplImage* img = cvLoadImage( filename, desired_color );    
    187.     if( !img )    
    188.         return false;    
    189.     
    190.     if( r.width == 0 || r.height == 0 )    
    191.     {    
    192.         r.width = img->width;    
    193.         r.height = img->height;    
    194.         r.x = r.y = 0;    
    195.     }    
    196.     
    197.     if( r.x > img->width || r.y > img->height ||    
    198.         r.x + r.width < 0 || r.y + r.height < 0 )    
    199.     {    
    200.         cvReleaseImage( &img );    
    201.         return false;    
    202.     }    
    203.     
    204.     /* truncate r to source image */    
    205.     if( r.x < 0 )    
    206.     {    
    207.         r.width += r.x;    
    208.         r.x = 0;    
    209.     }    
    210.     if( r.y < 0 )    
    211.     {    
    212.         r.height += r.y;    
    213.         r.y = 0;    
    214.     }    
    215.     
    216.     if( r.x + r.width > img->width )    
    217.         r.width = img->width - r.x;    
    218.     
    219.     if( r.y + r.height > img->height )    
    220.         r.height = img->height - r.y;    
    221.     
    222.     cvSetImageROI( img, r );    
    223.     CopyOf( img, desired_color );    
    224.     
    225.     cvReleaseImage( &img );    
    226.     return true;    
    227. }    
    228.     
    229.     
    230. bool  CvvImage::Save( const char* filename )    
    231. {    
    232.     if( !m_img )    
    233.         return false;    
    234.     cvSaveImage( filename, m_img );    
    235.     return true;    
    236. }    
    237.     
    238.     
    239. void  CvvImage::Show( const char* window )    
    240. {    
    241.     if( m_img )    
    242.         cvShowImage( window, m_img );    
    243. }    
    244.     
    245.     
    246. void  CvvImage::Show( HDC dc, int x, int y, int w, int h, int from_x, int from_y )    
    247. {    
    248.     if( m_img && m_img->depth == IPL_DEPTH_8U )    
    249.     {    
    250.         uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];    
    251.         BITMAPINFO* bmi = (BITMAPINFO*)buffer;    
    252.         int bmp_w = m_img->width, bmp_h = m_img->height;    
    253.     
    254.         FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin );    
    255.     
    256.         from_x = MIN( MAX( from_x, 0 ), bmp_w - 1 );    
    257.         from_y = MIN( MAX( from_y, 0 ), bmp_h - 1 );    
    258.     
    259.         int sw = MAX( MIN( bmp_w - from_x, w ), 0 );    
    260.         int sh = MAX( MIN( bmp_h - from_y, h ), 0 );    
    261.     
    262.         SetDIBitsToDevice(    
    263.             dc, x, y, sw, sh, from_x, from_y, from_y, sh,    
    264.             m_img->imageData + from_y*m_img->widthStep,    
    265.             bmi, DIB_RGB_COLORS );    
    266.     }    
    267. }    
    268.     
    269.     
    270. void  CvvImage::DrawToHDC( HDC hDCDst, RECT* pDstRect )     
    271. {    
    272.     if( pDstRect && m_img && m_img->depth == IPL_DEPTH_8U && m_img->imageData )    
    273.     {    
    274.         uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];    
    275.         BITMAPINFO* bmi = (BITMAPINFO*)buffer;    
    276.         int bmp_w = m_img->width, bmp_h = m_img->height;    
    277.     
    278.         CvRect roi = cvGetImageROI( m_img );    
    279.         CvRect dst = RectToCvRect( *pDstRect );    
    280.     
    281.         if( roi.width == dst.width && roi.height == dst.height )    
    282.         {    
    283.             Show( hDCDst, dst.x, dst.y, dst.width, dst.height, roi.x, roi.y );    
    284.             return;    
    285.         }    
    286.     
    287.         if( roi.width > dst.width )    
    288.         {    
    289.             SetStretchBltMode(    
    290.                 hDCDst,           // handle to device context    
    291.                 HALFTONE );    
    292.         }    
    293.         else    
    294.         {    
    295.             SetStretchBltMode(    
    296.                 hDCDst,           // handle to device context    
    297.                 COLORONCOLOR );    
    298.         }    
    299.     
    300.         FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin );    
    301.     
    302.         ::StretchDIBits(    
    303.             hDCDst,    
    304.             dst.x, dst.y, dst.width, dst.height,    
    305.             roi.x, roi.y, roi.width, roi.height,    
    306.             m_img->imageData, bmi, DIB_RGB_COLORS, SRCCOPY );    
    307.     }    
    308. }    
    309.     
    310.     
    311. void  CvvImage::Fill( int color )    
    312. {    
    313.     cvSet( m_img, cvScalar(color&255,(color>>8)&255,(color>>16)&255,(color>>24)&255) );    
    314. }    
    315. #endif   
原文地址:https://www.cnblogs.com/ssjie/p/4958580.html