CvvImage.cpp

16 篇文章 0 订阅
7 篇文章 0 订阅

由于OpenCV2.2里面,把原来的CvvImage整个类给删除掉了,因此在MFC下使用带来诸多不方便,大家可以通过提前opencv2.1中的代码的方法来解决(弄一个h文件和一个cpp文件,然后放到你的项目里面一起编译就可以了)。

但是在s2010中会出现error C2039: “DrawToHDC”: 不是“ATL::CImage”的成员的错误。

这主要是因为命名冲突引起的,引入命名空间可以解决。

将CvvImage.h的倒数第二句改成:

namespace cv
{
typedef CvvImage CImage;
}

在使用CImage之前包含CcvImage.h并且将CImage写成cv::CImage即可。

当然最简单的办法还是将CImage直接换成CvvImage


参考网址:http://www.opencv.org.cn/index.php/MFC%E4%B8%AD%E5%BF%AB%E9%80%9F%E5%BA%94%E7%94%A8OpenCV

接下来的是从老版本来的源文件,已配置的可以略过.

[cpp]  view plain copy
  1. CvvImage.h  
  2.   
  3. //------------------------------------------------------------------------------------------------  
  4.   
  5. #pragma once  
  6.   
  7. #ifndef CVVIMAGE_CLASS_DEF  
  8. #define CVVIMAGE_CLASS_DEF  
  9.   
  10. #include "opencv2/highgui/highgui.hpp"  
  11. #include "opencv2/core/core.hpp"  
  12. #include "opencv2/imgproc/imgproc.hpp"  
  13. #include "opencv2/imgproc/imgproc_c.h"  
  14.   
  15.   
  16. class  CvvImage  
  17. {  
  18. public:  
  19.  CvvImage();  
  20.  virtual ~CvvImage();  
  21.   
  22.    
  23.  virtual bool  Create( int width, int height, int bits_per_pixel, int image_origin = 0 );  
  24.   
  25.    
  26.  virtual bool  Load( const char* filename, int desired_color = 1 );  
  27.   
  28.    
  29.  virtual bool  LoadRect( const char* filename,  
  30.   int desired_color, CvRect r );  
  31.   
  32. #if defined WIN32 || defined _WIN32  
  33.  virtual bool  LoadRect( const char* filename,  
  34.   int desired_color, RECT r )  
  35.  {  
  36.   return LoadRect( filename, desired_color,  
  37.    cvRect( r.left, r.top, r.right - r.left, r.bottom - r.top ));  
  38.  }  
  39. #endif  
  40.   
  41.    
  42.  virtual bool  Save( const char* filename );  
  43.   
  44.    
  45.  virtual void  CopyOf( CvvImage& image, int desired_color = -1 );  
  46.  virtual void  CopyOf( IplImage* img, int desired_color = -1 );  
  47.   
  48.  IplImage* GetImage() { return m_img; };  
  49.  virtual void  Destroy(void);  
  50.   
  51.    
  52.  int Width() { return !m_img ? 0 : !m_img->roi ? m_img->width : m_img->roi->width; };  
  53.  int Height() { return !m_img ? 0 : !m_img->roi ? m_img->height : m_img->roi->height;};  
  54.  int Bpp() { return m_img ? (m_img->depth & 255)*m_img->nChannels : 0; };  
  55.   
  56.  virtual void  Fill( int color );  
  57.   
  58.    
  59.  virtual void  Show( const char* window );  
  60.   
  61. #if defined WIN32 || defined _WIN32  
  62.    
  63.  virtual void  Show( HDC dc, int x, int y, int width, int height,  
  64.   int from_x = 0, int from_y = 0 );  
  65.    
  66.  virtual void  DrawToHDC( HDC hDCDst, RECT* pDstRect );  
  67. #endif  
  68.   
  69. protected:  
  70.   
  71.  IplImage*  m_img;  
  72. };  
  73.   
  74. typedef CvvImage CImage;  
  75.   
  76. #endif  
  77.   
  78. //---------------------------------------------------------------  
  79.   
  80.     CvvImage.cpp  
  81.   
  82. //------------------------------------------------------------------------------------------------  
  83.   
  84. #include "StdAfx.h"  
  85. #include "CvvImage.h"  
  86.   
  87. //  
  88. // Construction/Destruction  
  89. //  
  90.   
  91. CV_INLINE RECT NormalizeRect( RECT r );  
  92. CV_INLINE RECT NormalizeRect( RECT r )  
  93. {  
  94.  int t;  
  95.   
  96.  if( r.left > r.right )  
  97.  {  
  98.   t = r.left;  
  99.   r.left = r.right;  
  100.   r.right = t;  
  101.  }  
  102.   
  103.  if( r.top > r.bottom )  
  104.  {  
  105.   t = r.top;  
  106.   r.top = r.bottom;  
  107.   r.bottom = t;  
  108.  }  
  109.   
  110.  return r;  
  111. }  
  112.   
  113. CV_INLINE CvRect RectToCvRect( RECT sr );  
  114. CV_INLINE CvRect RectToCvRect( RECT sr )  
  115. {  
  116.  sr = NormalizeRect( sr );  
  117.  return cvRect( sr.left, sr.top, sr.right - sr.left, sr.bottom - sr.top );  
  118. }  
  119.   
  120. CV_INLINE RECT CvRectToRect( CvRect sr );  
  121. CV_INLINE RECT CvRectToRect( CvRect sr )  
  122. {  
  123.  RECT dr;  
  124.  dr.left = sr.x;  
  125.  dr.top = sr.y;  
  126.  dr.right = sr.x + sr.width;  
  127.  dr.bottom = sr.y + sr.height;  
  128.   
  129.  return dr;  
  130. }  
  131.   
  132. CV_INLINE IplROI RectToROI( RECT r );  
  133. CV_INLINE IplROI RectToROI( RECT r )  
  134. {  
  135.  IplROI roi;  
  136.  r = NormalizeRect( r );  
  137.  roi.xOffset = r.left;  
  138.  roi.yOffset = r.top;  
  139.  roi.width = r.right - r.left;  
  140.  roi.height = r.bottom - r.top;  
  141.  roi.coi = 0;  
  142.   
  143.  return roi;  
  144. }  
  145.   
  146. void  FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin )  
  147. {  
  148.  assert( bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));  
  149.   
  150.  BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);  
  151.   
  152.  memset( bmih, 0, sizeof(*bmih));  
  153.  bmih->biSize = sizeof(BITMAPINFOHEADER);  
  154.  bmih->biWidth = width;  
  155.  bmih->biHeight = origin ? abs(height) : -abs(height);  
  156.  bmih->biPlanes = 1;  
  157.  bmih->biBitCount = (unsigned short)bpp;  
  158.  bmih->biCompression = BI_RGB;  
  159.   
  160.  if( bpp == 8 )  
  161.  {  
  162.   RGBQUAD* palette = bmi->bmiColors;  
  163.   int i;  
  164.   for( i = 0; i < 256; i++ )  
  165.   {  
  166.    palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;  
  167.    palette[i].rgbReserved = 0;  
  168.   }  
  169.  }  
  170. }  
  171.   
  172. CvvImage::CvvImage()  
  173. {  
  174.  m_img = 0;  
  175. }  
  176.   
  177. void CvvImage::Destroy()  
  178. {  
  179.  cvReleaseImage( &m_img );  
  180. }  
  181.   
  182. CvvImage::~CvvImage()  
  183. {  
  184.  Destroy();  
  185. }  
  186.   
  187. bool  CvvImage::Create( int w, int h, int bpp, int origin )  
  188. {  
  189.  const unsigned max_img_size = 10000;  
  190.   
  191.  if( (bpp != 8 && bpp != 24 && bpp != 32) ||  
  192.   (unsigned)w >=  max_img_size || (unsigned)h >= max_img_size ||  
  193.   (origin != IPL_ORIGIN_TL && origin != IPL_ORIGIN_BL))  
  194.  {  
  195.   assert(0); // most probably, it is a programming error  
  196.   return false;  
  197.  }  
  198.   
  199.  if( !m_img || Bpp() != bpp || m_img->width != w || m_img->height != h )  
  200.  {  
  201.   if( m_img && m_img->nSize == sizeof(IplImage))  
  202.    Destroy();  
  203.   
  204.     
  205.   m_img = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, bpp/8 );  
  206.  }  
  207.   
  208.  if( m_img )  
  209.   m_img->origin = origin == 0 ? IPL_ORIGIN_TL : IPL_ORIGIN_BL;  
  210.   
  211.  return m_img != 0;  
  212. }  
  213.   
  214. void  CvvImage::CopyOf( CvvImage& image, int desired_color )  
  215. {  
  216.  IplImage* img = image.GetImage();  
  217.  if( img )  
  218.  {  
  219.   CopyOf( img, desired_color );  
  220.  }  
  221. }  
  222.   
  223.   
  224. #define HG_IS_IMAGE(img)                                                  \  
  225.  ((img) != 0 && ((const IplImage*)(img))->nSize == sizeof(IplImage) && \  
  226.  ((IplImage*)img)->imageData != 0)  
  227.   
  228.   
  229. void  CvvImage::CopyOf( IplImage* img, int desired_color )  
  230. {  
  231.  if( HG_IS_IMAGE(img) )  
  232.  {  
  233.   int color = desired_color;  
  234.   CvSize size = cvGetSize( img );  
  235.   
  236.   if( color < 0 )  
  237.    color = img->nChannels > 1;  
  238.   
  239.   if( Create( size.width, size.height,  
  240.    (!color ? 1 : img->nChannels > 1 ? img->nChannels : 3)*8,  
  241.    img->origin ))  
  242.   {  
  243.    cvConvertImage( img, m_img, 0 );  
  244.   }  
  245.  }  
  246. }  
  247.   
  248.   
  249. bool  CvvImage::Load( const char* filename, int desired_color )  
  250. {  
  251.  IplImage* img = cvLoadImage( filename, desired_color );  
  252.  if( !img )  
  253.   return false;  
  254.   
  255.  CopyOf( img, desired_color );  
  256.  cvReleaseImage( &img );  
  257.   
  258.  return true;  
  259. }  
  260.   
  261.   
  262. bool  CvvImage::LoadRect( const char* filename,  
  263.        int desired_color, CvRect r )  
  264. {  
  265.  if( r.width < 0 || r.height < 0 ) return false;  
  266.   
  267.  IplImage* img = cvLoadImage( filename, desired_color );  
  268.  if( !img )  
  269.   return false;  
  270.   
  271.  if( r.width == 0 || r.height == 0 )  
  272.  {  
  273.   r.width = img->width;  
  274.   r.height = img->height;  
  275.   r.x = r.y = 0;  
  276.  }  
  277.   
  278.  if( r.x > img->width || r.y > img->height ||  
  279.   r.x + r.width < 0 || r.y + r.height < 0 )  
  280.  {  
  281.   cvReleaseImage( &img );  
  282.   return false;  
  283.  }  
  284.   
  285.    
  286.  if( r.x < 0 )  
  287.  {  
  288.   r.width += r.x;  
  289.   r.x = 0;  
  290.  }  
  291.  if( r.y < 0 )  
  292.  {  
  293.   r.height += r.y;  
  294.   r.y = 0;  
  295.  }  
  296.   
  297.  if( r.x + r.width > img->width )  
  298.   r.width = img->width - r.x;  
  299.   
  300.  if( r.y + r.height > img->height )  
  301.   r.height = img->height - r.y;  
  302.   
  303.  cvSetImageROI( img, r );  
  304.  CopyOf( img, desired_color );  
  305.   
  306.  cvReleaseImage( &img );  
  307.  return true;  
  308. }  
  309.   
  310.   
  311. bool  CvvImage::Save( const char* filename )  
  312. {  
  313.  if( !m_img )  
  314.   return false;  
  315.  cvSaveImage( filename, m_img );  
  316.  return true;  
  317. }  
  318.   
  319.   
  320. void  CvvImage::Show( const char* window )  
  321. {  
  322.  if( m_img )  
  323.   cvShowImage( window, m_img );  
  324. }  
  325.   
  326.   
  327. void  CvvImage::Show( HDC dc, int x, int y, int w, int h, int from_x, int from_y )  
  328. {  
  329.  if( m_img && m_img->depth == IPL_DEPTH_8U )  
  330.  {  
  331.   uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];  
  332.   BITMAPINFO* bmi = (BITMAPINFO*)buffer;  
  333.   int bmp_w = m_img->width, bmp_h = m_img->height;  
  334.   
  335.   FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin );  
  336.   
  337.   from_x = MIN( MAX( from_x, 0 ), bmp_w - 1 );  
  338.   from_y = MIN( MAX( from_y, 0 ), bmp_h - 1 );  
  339.   
  340.   int sw = MAX( MIN( bmp_w - from_x, w ), 0 );  
  341.   int sh = MAX( MIN( bmp_h - from_y, h ), 0 );  
  342.   
  343.   SetDIBitsToDevice(  
  344.    dc, x, y, sw, sh, from_x, from_y, from_y, sh,  
  345.    m_img->imageData + from_y*m_img->widthStep,  
  346.    bmi, DIB_RGB_COLORS );  
  347.  }  
  348. }  
  349.   
  350.   
  351. void  CvvImage::DrawToHDC( HDC hDCDst, RECT* pDstRect )  
  352. {  
  353.  if( pDstRect && m_img && m_img->depth == IPL_DEPTH_8U && m_img->imageData )  
  354.  {  
  355.   uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];  
  356.   BITMAPINFO* bmi = (BITMAPINFO*)buffer;  
  357.   int bmp_w = m_img->width, bmp_h = m_img->height;  
  358.   
  359.   CvRect roi = cvGetImageROI( m_img );  
  360.   CvRect dst = RectToCvRect( *pDstRect );  
  361.   
  362.   if( roi.width == dst.width && roi.height == dst.height )  
  363.   {  
  364.    Show( hDCDst, dst.x, dst.y, dst.width, dst.height, roi.x, roi.y );  
  365.    return;  
  366.   }  
  367.   
  368.   if( roi.width > dst.width )  
  369.   {  
  370.    SetStretchBltMode(  
  371.     hDCDst,           // handle to device context  
  372.     HALFTONE );  
  373.   }  
  374.   else  
  375.   {  
  376.    SetStretchBltMode(  
  377.     hDCDst,           // handle to device context  
  378.     COLORONCOLOR );  
  379.   }  
  380.   
  381.   FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), m_img->origin );  
  382.   
  383.   ::StretchDIBits(  
  384.    hDCDst,  
  385.    dst.x, dst.y, dst.width, dst.height,  
  386.    roi.x, roi.y, roi.width, roi.height,  
  387.    m_img->imageData, bmi, DIB_RGB_COLORS, SRCCOPY );  
  388.  }  
  389. }  
  390.   
  391.   
  392. void  CvvImage::Fill( int color )  
  393. {  
  394.  cvSet( m_img, cvScalar(color&255,(color>>8)&255,(color>>16)&255,(color>>24)&255) );  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值