CvvImage在高级别的Opencv2.4.11下的配置以及错误解决办法。
由于高版本的OpenCV2.4.11里取消了CImage(CvvImage),在此我们可以用老的版本替代。
在需要的地方引入 #include "CvvImage.h" 就可以用了。
需要将CvvIamge.h和CvvImage.cpp重新加入进来
1 #pragma once 2 // CvvImage.h 3 #ifndef CVVIMAGE_CLASS_DEF 4 #define CVVIMAGE_CLASS_DEF 5 6 #include "opencv.hpp" 7 8 class CvvImage{ 9 public: 10 CvvImage(); 11 virtual ~CvvImage(); 12 13 virtual bool Create( int width, int height, int bits_per_pixel, int image_origin = 0 ); 14 15 virtual bool Load( const char* filename, int desired_color = 1 ); 16 17 virtual bool LoadRect( const char* filename, 18 int desired_color, CvRect r ); 19 20 #if defined WIN32 || defined _WIN32 21 virtual bool LoadRect( const char* filename, 22 int desired_color, RECT r ) 23 { 24 return LoadRect( filename, desired_color, 25 cvRect( r.left, r.top, r.right - r.left, r.bottom - r.top )); 26 } 27 #endif 28 29 virtual bool Save( const char* filename ); 30 31 virtual void CopyOf( CvvImage& image, int desired_color = -1 ); 32 virtual void CopyOf( IplImage* img, int desired_color = -1 ); 33 34 IplImage* GetImage() { return m_img; }; 35 virtual void Destroy(void); 36 37 int Width() { return !m_img ? 0 : !m_img->roi ? m_img->width : m_img->roi->width; }; 38 int Height() { return !m_img ? 0 : !m_img->roi ? m_img->height : m_img->roi->height;}; 39 int Bpp() { return m_img ? (m_img->depth & 255)*m_img->nChannels : 0; }; 40 41 virtual void Fill( int color ); 42 43 virtual void Show( const char* window ); 44 45 #if defined WIN32 || defined _WIN32 46 virtual void Show( HDC dc, int x, int y, int width, int height, int from_x = 0, int from_y = 0 ); 47 virtual void DrawToHDC( HDC hDCDst, RECT* pDstRect ); 48 #endif 49 50 protected: 51 52 IplImage* m_img; 53 }; 54 55 //typedef CvvImage CImage; 56 namespace cv 57 { 58 typedef CvvImage CImage; 59 } 60 61 #endif
1 //CvvImage.cpp 2 3 #include "StdAfx.h" 4 #include "CvvImage.h" 5 6 // Construction/Destruction 7 8 CV_INLINE RECT NormalizeRect( RECT r ); 9 CV_INLINE RECT NormalizeRect( RECT r ) { 10 int t; 11 12 if( r.left > r.right ){ 13 t = r.left; 14 r.left = r.right; 15 r.right = t; 16 } 17 18 if( r.top > r.bottom ) { 19 t = r.top; 20 r.top = r.bottom; 21 r.bottom = t; 22 } 23 24 return r; 25 } 26 27 CV_INLINE CvRect RectToCvRect( RECT sr ); 28 CV_INLINE CvRect RectToCvRect( RECT sr ) { 29 sr = NormalizeRect( sr ); 30 return cvRect( sr.left, sr.top, sr.right - sr.left, sr.bottom - sr.top ); 31 } 32 33 CV_INLINE RECT CvRectToRect( CvRect sr ); 34 CV_INLINE RECT CvRectToRect( CvRect sr ) { 35 RECT dr; 36 dr.left = sr.x; 37 dr.top = sr.y; 38 dr.right = sr.x + sr.width; 39 dr.bottom = sr.y + sr.height; 40 41 return dr; 42 } 43 44 CV_INLINE IplROI RectToROI( RECT r ); 45 CV_INLINE IplROI RectToROI( RECT r ) { 46 IplROI roi; 47 r = NormalizeRect( r ); 48 roi.xOffset = r.left; 49 roi.yOffset = r.top; 50 roi.width = r.right - r.left; 51 roi.height = r.bottom - r.top; 52 roi.coi = 0; 53 54 return roi; 55 } 56 57 void FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin ) { 58 assert( bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32)); 59 60 BITMAPINFOHEADER* bmih = &(bmi->bmiHeader); 61 62 memset( bmih, 0, sizeof(*bmih)); 63 bmih->biSize = sizeof(BITMAPINFOHEADER); 64 bmih->biWidth = width; 65 bmih->biHeight = origin ? abs(height) : -abs(height); 66 bmih->biPlanes = 1; 67 bmih->biBitCount = (unsigned short)bpp; 68 bmih->biCompression = BI_RGB; 69 70 if( bpp == 8 ) { 71 RGBQUAD* palette = bmi->bmiColors; 72 int i; 73 for( i = 0; i < 256; i++ ) { 74 palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i; 75 palette[i].rgbReserved = 0; 76 } 77 } 78 } 79 80 CvvImage::CvvImage() { m_img = 0; } 81 82 void CvvImage::Destroy() { cvReleaseImage( &m_img ); } 83 84 CvvImage::~CvvImage() { Destroy(); } 85 86 bool CvvImage::Create( int w, int h, int bpp, int origin ) { 87 const unsigned max_img_size = 10000; 88 89 if( (bpp != 8 && bpp != 24 && bpp != 32) || (unsigned)w >= max_img_size || (unsigned)h >= max_img_size || 90 (origin != IPL_ORIGIN_TL && origin != IPL_ORIGIN_BL)) { 91 assert(0); // most probably, it is a programming error 92 return false; 93 } 94 95 if( !m_img || Bpp() != bpp || m_img->width != w || m_img->height != h ) { 96 if( m_img && m_img->nSize == sizeof(IplImage)) 97 Destroy(); 98 99 m_img = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, bpp/8 ); 100 } 101 102 if( m_img ) 103 m_img->origin = origin == 0 ? IPL_ORIGIN_TL : IPL_ORIGIN_BL; 104 105 return m_img != 0; 106 } 107 108 void CvvImage::CopyOf( CvvImage& image, int desired_color ) { 109 IplImage* img = image.GetImage(); 110 if( img ) { 111 CopyOf( img, desired_color ); 112 } 113 } 114 115 #define HG_IS_IMAGE(img) \ 116 ((img) != 0 && ((const IplImage*)(img))->nSize == sizeof(IplImage) && \ 117 ((IplImage*)img)->imageData != 0) 118 119 void CvvImage::CopyOf( IplImage* img, int desired_color ){ 120 if( HG_IS_IMAGE(img) ) { 121 int color = desired_color; 122 CvSize size = cvGetSize( img ); 123 124 if( color < 0 ) 125 color = img->nChannels > 1; 126 127 if( Create( size.width, size.height, (!color ? 1 : img->nChannels > 1 ? img->nChannels : 3)*8, img->origin )) { 128 cvConvertImage( img, m_img, 0 ); 129 } 130 } 131 } 132 133 bool CvvImage::Load( const char* filename, int desired_color ){ 134 IplImage* img = cvLoadImage( filename, desired_color ); 135 if( !img ) return false; 136 137 CopyOf( img, desired_color ); 138 cvReleaseImage( &img ); 139 140 return true; 141 } 142 143 bool CvvImage::LoadRect( const char* filename, int desired_color, CvRect r ){ 144 if( r.width < 0 || r.height < 0 ) return false; 145 146 IplImage* img = cvLoadImage( filename, desired_color ); 147 if( !img ) return false; 148 149 if( r.width == 0 || r.height == 0 ) { 150 r.width = img->width; 151 r.height = img->height; 152 r.x = r.y = 0; 153 } 154 155 if( r.x > img->width || r.y > img->height || r.x + r.width < 0 || r.y + r.height < 0 ) { 156 cvReleaseImage( &img ); 157 return false; 158 } 159 160 if( r.x < 0 ) { 161 r.width += r.x; 162 r.x = 0; 163 } 164 if( r.y < 0 ) { 165 r.height += r.y; 166 r.y = 0; 167 } 168 169 if( r.x + r.width > img->width ) r.width = img->width - r.x; 170 171 if( r.y + r.height > img->height ) r.height = img->height - r.y; 172 173 cvSetImageROI( img, r ); 174 CopyOf( img, desired_color ); 175 176 cvReleaseImage( &img ); 177 return true; 178 } 179 180 bool CvvImage::Save( const char* filename ){ 181 if( !m_img ) return false; 182 cvSaveImage( filename, m_img ); 183 return true; 184 } 185 186 void CvvImage::Show( const char* window ){ 187 if( m_img ) cvShowImage( window, m_img ); 188 } 189 190 191 void CvvImage::Show( HDC dc, int x, int y, int w, int h, int from_x, int from_y ){ 192 if( m_img && m_img->depth == IPL_DEPTH_8U ) { 193 uchar buffer[sizeof(BITMAPINFOHEADER) + 1024]; 194 BITMAPINFO* bmi = (BITMAPINFO*)buffer; 195 int bmp_w = m_img->width, bmp_h = m_img->height; 196 197 FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin ); 198 199 from_x = MIN( MAX( from_x, 0 ), bmp_w - 1 ); 200 from_y = MIN( MAX( from_y, 0 ), bmp_h - 1 ); 201 202 int sw = MAX( MIN( bmp_w - from_x, w ), 0 ); 203 int sh = MAX( MIN( bmp_h - from_y, h ), 0 ); 204 205 SetDIBitsToDevice( 206 dc, x, y, sw, sh, from_x, from_y, from_y, sh, 207 m_img->imageData + from_y*m_img->widthStep, 208 bmi, DIB_RGB_COLORS ); 209 } 210 } 211 212 void CvvImage::DrawToHDC( HDC hDCDst, RECT* pDstRect ){ 213 if( pDstRect && m_img && m_img->depth == IPL_DEPTH_8U && m_img->imageData ) { 214 uchar buffer[sizeof(BITMAPINFOHEADER) + 1024]; 215 BITMAPINFO* bmi = (BITMAPINFO*)buffer; 216 int bmp_w = m_img->width, bmp_h = m_img->height; 217 218 CvRect roi = cvGetImageROI( m_img ); 219 CvRect dst = RectToCvRect( *pDstRect ); 220 221 if( roi.width == dst.width && roi.height == dst.height ) { 222 Show( hDCDst, dst.x, dst.y, dst.width, dst.height, roi.x, roi.y ); 223 return; 224 } 225 226 if( roi.width > dst.width ) { 227 SetStretchBltMode( hDCDst, HALFTONE ); // handle to device context 228 229 } 230 else { 231 SetStretchBltMode(hDCDst, COLORONCOLOR ); 232 } 233 234 FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin ); 235 236 ::StretchDIBits( 237 hDCDst, 238 dst.x, dst.y, dst.width, dst.height, 239 roi.x, roi.y, roi.width, roi.height, 240 m_img->imageData, bmi, DIB_RGB_COLORS, SRCCOPY ); 241 } 242 } 243 244 void CvvImage::Fill( int color ){ 245 cvSet( m_img, cvScalar(color&255,(color>>8)&255,(color>>16)&255,(color>>24)&255) ); 246 }