CvvImage.h CvvImage.cpp文件

由于OpenCV版本的更新,高版本中不在使用CvvImage类显示图片,不过我们可以通过自己手动添加打方式加入工程文件中。

注:最好把两个文件放入工程文件中,这样在其他文件中调用该类就可以直接使用#include"CvvImage.h"而不用把它的完整路径写入include中。

下面附上CvvImage.h和CvvImage.cpp源码:

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


CvvImage.cpp

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

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值