您好,欢迎来到年旅网。
搜索
您的当前位置:首页OpenCV

OpenCV

来源:年旅网
OpenCV CvvImage的使⽤

在OpenCV低版本中,有CvvImage这个类,但是在⾼版本中不再有这个类了,但是当我们需要⽤这个类时,如何添加呢,下⾯是我所总结的⼀些东西,希望对各位有帮助吧。

OpenCv的安装⾃⼰去搞定,我⽤的是OpenCV-2-3-1的版本,使⽤的vs2010

在你的⼯程中添加CvvImage.h和CvvImage.cpp这两个⽂件,⼀般来说此时是编译不能通过的。还需要做下⾯的⼯作:将Intel TBB 3.0⽂件夹复制到D:\\Program Files⽬录下

然后将tbb_debug.dll⽂件复制到D:\\Program Files\\OpenCV-2.3.1-win-superpack\\opencv\\build\\common\bb\\ia32\\vc10⽬录下,

接着在可执⾏⽬录中添加D:\\Program Files\\OpenCV-2.3.1-win-superpack\\opencv\\build\\includeD:\\Program Files\\OpenCV-2.3.1-win-superpack\\opencv\\build\\include\\opencvD:\\Program Files\\OpenCV-2.3.1-win-superpack\\opencv\\build\\include\\opencv2D:\\Program Files\\Intel TBB 3.0\\bin\\ia32\\vc10注意:看你的Opencv安装的⽬录⽽不相同接着需要添加环境变量

如果你是在⽤户模式下,就在⽤户变量的path⾥⾯添加D:\\Program Files\\Intel TBB 3.0\\bin\\ia32\\vc10

如果你是在administrantor模式下,就在系统变量的path⾥⾯添加D:\\Program Files\\Intel TBB 3.0\\bin\\ia32\\vc10最后重新启动,在编译你的⼯程;CvvImage头⽂件[cpp]

1. #ifndef CVVIMAGE_CLASS_DEF2. #define CVVIMAGE_CLASS_DEF3. #ifndef RC_OPENCV_2_1_04. #include5. #include6.

7. class CvvImage8. {

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. #if defined WIN32 || defined _WIN32

20. virtual bool LoadRect( const char* filename,21. int desired_color, RECT r )22. {

23. return LoadRect( filename, desired_color,

24. cvRect( r.left, r.top, r.right - r.left, r.bottom - r.top ));25. }

26. #endif27.

28. virtual bool Save( const char* filename );29.

30. virtual void CopyOf( CvvImage& image, int desired_color = -1 );31. virtual void CopyOf( IplImage* img, int desired_color = -1 );32. IplImage* GetImage() { return m_img; };33. virtual void Destroy(void);34.

35. int Width() { return !m_img ? 0 : !m_img->roi ? m_img->width : m_img->roi->width; };36. int Height() { return !m_img ? 0 : !m_img->roi ? m_img->height : m_img->roi->height;};37. int Bpp() { return m_img ? (m_img->depth & 255)*m_img->nChannels : 0; };38. virtual void Fill( int color );39.

40. virtual void Show( const char* window );41. #if defined WIN32 || defined _WIN3242.

43. virtual void Show( HDC dc, int x, int y, int width, int height,44. int from_x = 0, int from_y = 0 );45.

46. virtual void DrawToHDC( HDC hDCDst, RECT* pDstRect );47. #endif48. protected:

49. IplImage* m_img;50. };

51. typedef CvvImage CImage;52. #endif53. #endifCvvImage源⽂件[cpp]

1. #include \"stdafx.h\"

2. #ifndef RC_OPENCV_2_1_03. #include \"CvvImage.h\"

4. //////////////////////////////////////////////////////////////////////5. // Construction/Destruction

6. //////////////////////////////////////////////////////////////////////

7. CV_INLINE RECT NormalizeRect( RECT r );8. CV_INLINE RECT NormalizeRect( RECT r )9. {10. int t;

11. if( r.left > r.right )12. {

13. t = r.left;

14. r.left = r.right;15. r.right = t;16. }

17. if( r.top > r.bottom )18. {

19. t = r.top;

20. r.top = r.bottom;21. r.bottom = t;22. }

23. return r;24. }

25. CV_INLINE CvRect RectToCvRect( RECT sr );26. CV_INLINE CvRect RectToCvRect( RECT sr )27. {

28. sr = NormalizeRect( sr );

29. return cvRect( sr.left, sr.top, sr.right - sr.left, sr.bottom - sr.top );30. }

31. CV_INLINE RECT CvRectToRect( CvRect sr );32. CV_INLINE RECT CvRectToRect( CvRect sr )33. {

34. RECT dr;

35. dr.left = sr.x;36. dr.top = sr.y;

37. dr.right = sr.x + sr.width;38. dr.bottom = sr.y + sr.height;39. return dr;40. }

41. CV_INLINE IplROI RectToROI( RECT r );42. CV_INLINE IplROI RectToROI( RECT r )43. {

44. IplROI roi;

45. r = NormalizeRect( r );46. roi.xOffset = r.left;47. roi.yOffset = r.top;

48. roi.width = r.right - r.left;49. roi.height = r.bottom - r.top;50. roi.coi = 0;51. return roi;52. }

53. void FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin ). {

55. assert( bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));56. BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);57. memset( bmih, 0, sizeof(*bmih));

58. bmih->biSize = sizeof(BITMAPINFOHEADER);59. bmih->biWidth = width;

60. bmih->biHeight = origin ? abs(height) : -abs(height);61. bmih->biPlanes = 1;

62. bmih->biBitCount = (unsigned short)bpp;63. bmih->biCompression = BI_RGB;. if( bpp == 8 )65. {

66. RGBQUAD* palette = bmi->bmiColors;67. int i;

68. for( i = 0; i < 256; i++ )69. {

70. palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;71. palette[i].rgbReserved = 0;72. }73. }74. }

75. CvvImage::CvvImage()76. {

77. m_img = 0;78. }

79. void CvvImage::Destroy()80. {

81. cvReleaseImage( &m_img );82. }

83. CvvImage::~CvvImage()84. {

85. Destroy();86. }

87. bool CvvImage::Create( int w, int h, int bpp, int origin )88. {

. const unsigned max_img_size = 10000;90. if( (bpp != 8 && bpp != 24 && bpp != 32) ||

91. (unsigned)w >= max_img_size || (unsigned)h >= max_img_size ||92. (origin != IPL_ORIGIN_TL && origin != IPL_ORIGIN_BL))93. {

94. assert(0); // most probably, it is a programming error95. return false;96. }

97. if( !m_img || Bpp() != bpp || m_img->width != w || m_img->height != h )98. {

99. if( m_img && m_img->nSize == sizeof(IplImage))100. Destroy();101.

102. m_img = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, bpp/8 );103. }

104. if( m_img )

105. m_img->origin = origin == 0 ? IPL_ORIGIN_TL : IPL_ORIGIN_BL;106. return m_img != 0;107. }

108. void CvvImage::CopyOf( CvvImage& image, int desired_color )109. {

110. IplImage* img = image.GetImage();111. if( img )112. {

113. CopyOf( img, desired_color );114. }115. }

116. #define HG_IS_IMAGE(img) \\

117. ((img) != 0 && ((const IplImage*)(img))->nSize == sizeof(IplImage) && \\118. ((IplImage*)img)->imageData != 0)

119. void CvvImage::CopyOf( IplImage* img, int desired_color )120. {

121. if( HG_IS_IMAGE(img) )122. {

123. int color = desired_color;

124. CvSize size = cvGetSize( img );125. if( color < 0 )

126. color = img->nChannels > 1;

127. if( Create( size.width, size.height,

128. (!color ? 1 : img->nChannels > 1 ? img->nChannels : 3)*8,129. img->origin ))130. {

131. cvConvertImage( img, m_img, 0 );132. }133. }134. }

135. bool CvvImage::Load( const char* filename, int desired_color )136. {

137. IplImage* img = cvLoadImage( filename, desired_color );138. if( !img )139. return false;

140. CopyOf( img, desired_color );141. cvReleaseImage( &img );142. return true;143. }

144. bool CvvImage::LoadRect( const char* filename,145. int desired_color, CvRect r )146. {

147. if( r.width < 0 || r.height < 0 ) return false;

148. IplImage* img = cvLoadImage( filename, desired_color );149. if( !img )150. return false;

151. if( r.width == 0 || r.height == 0 )152. {

153. r.width = img->width;1. r.height = img->height;155. r.x = r.y = 0;156. }

157. if( r.x > img->width || r.y > img->height ||158. r.x + r.width < 0 || r.y + r.height < 0 )

159. {

160. cvReleaseImage( &img );161. return false;162. }163.

1. if( r.x < 0 )165. {

166. r.width += r.x;167. r.x = 0;168. }

169. if( r.y < 0 )170. {

171. r.height += r.y;172. r.y = 0;173. }

174. if( r.x + r.width > img->width )175. r.width = img->width - r.x;

176. if( r.y + r.height > img->height )177. r.height = img->height - r.y;178. cvSetImageROI( img, r );

179. CopyOf( img, desired_color );180. cvReleaseImage( &img );181. return true;182. }

183. bool CvvImage::Save( const char* filename )184. {

185. if( !m_img )186. return false;

187. cvSaveImage( filename, m_img );188. return true;1. }

190. void CvvImage::Show( const char* window )191. {

192. if( m_img )

193. cvShowImage( window, m_img );194. }

195. void CvvImage::Show( HDC dc, int x, int y, int w, int h, int from_x, int from_y )196. {

197. if( m_img && m_img->depth == IPL_DEPTH_8U )198. {

199. uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];200. BITMAPINFO* bmi = (BITMAPINFO*)buffer;

201. int bmp_w = m_img->width, bmp_h = m_img->height;

202. FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin );203. from_x = MIN( MAX( from_x, 0 ), bmp_w - 1 );204. from_y = MIN( MAX( from_y, 0 ), bmp_h - 1 );205. int sw = MAX( MIN( bmp_w - from_x, w ), 0 );206. int sh = MAX( MIN( bmp_h - from_y, h ), 0 );207. SetDIBitsToDevice(

208. dc, x, y, sw, sh, from_x, from_y, from_y, sh,

209. m_img->imageData + from_y*m_img->widthStep,210. bmi, DIB_RGB_COLORS );211. }212. }

213. void CvvImage::DrawToHDC( HDC hDCDst, RECT* pDstRect )214. {

215. if( pDstRect && m_img && m_img->depth == IPL_DEPTH_8U && m_img->imageData )216. {

217. uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];218. BITMAPINFO* bmi = (BITMAPINFO*)buffer;

219. int bmp_w = m_img->width, bmp_h = m_img->height;220. CvRect roi = cvGetImageROI( m_img );

221. CvRect dst = RectToCvRect( *pDstRect );

222. if( roi.width == dst.width && roi.height == dst.height )223. {

224. Show( hDCDst, dst.x, dst.y, dst.width, dst.height, roi.x, roi.y );225. return;226. }

227. if( roi.width > dst.width )228. {

229. SetStretchBltMode(

230. hDCDst, // handle to device context231. HALFTONE );232. }233. else234. {

235. SetStretchBltMode(

236. hDCDst, // handle to device context237. COLORONCOLOR );238. }

239. FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin );240. ::StretchDIBits(241. hDCDst,

242. dst.x, dst.y, dst.width, dst.height,243. roi.x, roi.y, roi.width, roi.height,

244. m_img->imageData, bmi, DIB_RGB_COLORS, SRCCOPY );245. }246. }

247. void CvvImage::Fill( int color )248. {

249. cvSet( m_img, cvScalar(color&255,(color>>8)&255,(color>>16)&255,(color>>24)&255) );250. }

251. #endif

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- oldu.cn 版权所有 浙ICP备2024123271号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务