CvvImage类在MFC中使用的说明

来源:互联网 发布:淘宝子账号被限制登录 编辑:程序博客网 时间:2024/05/29 04:14


一、CvvImage介绍        

       平常使用Open CV时总是跳出一个个窗口,很难将项目进行系统集成,特别是在MFC等Windows环境中加载显示OpenCV中的图像,这时候就需要用到CvvImage类。由于opencv的问题,新版本没有了CvvImage。

       先介绍一下CvvImage类的定义:CvvImage.h

[cpp] view plain copy print?
  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  

         注意:参数中含有HDC(注:一种windows系统下定义的变量类型,用来描述设备描述表的句柄类型)类型的并不能保证移植到其他平台,例如Show/DrawToHDC等。后文中的DC,即device context(设备环境),一般可以理解为windows操作系统为方便绘图而抽象的”绘图表面“,“往窗口上绘图”,有时也被说成是“往窗口DC上绘图”。


【说明】

CvvImage::Create

[cpp] view plain copy print?
  1. bool CvvImage::Create(int w, int h, int bpp, int origin);  

创建一个图像。 成功返回true, 失败返回false。

w 图像宽
h 图像高
bpp 每个像素的bit数, 值等于像素深度乘以通道数
origin 0 - 顶—左结构, 1 - 底—左结构 (Windows bitmaps 风格)

例: 创建一个400行600列的, IPL_DEPTH_8U类型的3通道图像, 顶—左结构
CvvImage img;
bool flag = img.Create(600, 400, IPL_DEPTH_8U*3, 0);
if(!flag) printf("创建图像失败!");


CvvImage::Load
[cpp] view plain copy print?
  1. bool CvvImage::Load(const char* filename, int desired_color);   

装载一个图像。

filename 图像文件名称。
desired_color 图像波段数, 和cvLoadImage类似。


CvvImage::CopyOf

[cpp] view plain copy print?
  1. void CvvImage::CopyOf(CvvImage& img, int desired_color);  
  2. void CvvImage::CopyOf(IplImage* img, int desired_color);  
从img复制图像到当前的对象中。
img 要复制的图像。
desired_color 为复制后图像的通道数, 复制后图像的像素深度为8bit。

例:// 读一个图像,然后复制为1个3通道的彩色图像
CvvImage img1, img2;
img1.Load("example.tiff");
img2.CopyOf(img1, 3);


CvvImage::LoadRect

[cpp] view plain copy print?
  1. bool CvvImage::LoadRect(const char* filename, int desired_color, CvRect rect);  

从图像读出一个区域。

filename 图像名称。
desired_color 图像波段数, 和cvLoadImage类似。
rect 要读取的图像范围。

注 LoadRect是先用Load装载一个图像,然后再将rect设置为图像的ROI区域,然后复制图像得到,因此,如果一个图像很大(例如几百MB),即使想从中只读几个像素也是会失败的。


CvvImage::Show

[cpp] view plain copy print?
  1. void CvvImage::Show(HDC dc, int x, int y, int w, int h, int from_x, int from_y);  

绘制图像的部分到DC。 图像没有缩放。此函数仅在Windows下有效。

dc 设备描述符。
x 局部图像显示在DC上,从DC上的第x列开始。
y 局部图像显示在DC上,从DC上的第y列开始。
(x,y)为局部图像显示在DC上的起始位置。
w 局部图像宽度。
h 局部图像高度。
from_x 从图像的第from_x列开始显示。
from_y 从图像的第from_y行开始显示。

例:// 从图像10行20列开始,显示400行600列图像到设备描述符的100行200列开始的位置
CvvImage img;
img.Load("example.tiff");
img.Show(hDC, 200, 100, 600, 400, 20, 10);

 

CvvImage::Show

[cpp] view plain copy print?
  1. void CvvImage::Show(const char* window);  

显示一个图像。 和cvShowImage相似。



CvvImage::DrawToHDC

[cpp] view plain copy print?
  1. void CImage::DrawToHDC(HDC hDCDst, RECT* pDstRect);  

绘制图像的ROI区域到DC的pDstRect,如果图像大小和pDstRect不一致,图像会拉伸/压缩。此函数仅在Windows下有效。

hDCDst 设备描述符。
pDstRect 对应的设备描述符区域。

例:

[cpp] view plain copy print?
  1. CvvImage img;   
  2. img.Load("example.tiff");   
  3. CRect rect;   
  4. rect.left = 100;  
  5.  rect.top = 200;  
  6.  rect.right = rect.left + 600;   
  7. rect.bottom = rect.top + 400;   
  8. img.DrawToHDC(hDC, &rect);  

 

CvvImage::Fill

[cpp] view plain copy print?
  1. void CvvImage::Fill(int color);  

以color颜色填充图像。


CvvImage::Save

[cpp] view plain copy print?
  1. bool CvvImage::Save(const char* filename);  
保存图像

      由于OpenCV2的接口更方便使用,而CvvImage使用了IplImage类型,所以需要进行IplImage与Mat相互转换,下面看下它们的转换。


二、IplImage与Mat相互转换
1、cvMat之间
[cpp] view plain copy print?
  1. //注意:深拷贝 - 单独分配空间,两者相互独立    
  2. CvMat* a;    
  3. CvMat* b = cvCloneMat(a);   //copy a to b    

2、Mat之间复制
[cpp] view plain copy print?
  1. //注意:浅拷贝 -  不复制数据只创建矩阵头,数据共享(更改a,b,c的任意一个都会对另外2个产生同样的作用)  
  2. Mat a;  
  3. Mat b = a; //a "copy" to b  
  4. Mat c(a); //a "copy" to c  
  5.   
  6. //注意:深拷贝  
  7. Mat a;  
  8. Mat b = a.clone(); //a copy to b  
  9. Mat c;  
  10. a.copyTo(c); //a copy to c  

3、CvMat与Mat互转
[cpp] view plain copy print?
  1. //使用Mat的构造函数:Mat::Mat(const CvMat* m, bool copyData=false);    默认情况下copyData为false  
  2. CvMat* a;  
  3. //注意:以下三种效果一致,均为浅拷贝  
  4. Mat b(a);    //a "copy" to b  
  5. Mat b(a, false);    //a "copy" to b  
  6. Mat b = a;    //a "copy" to b  
  7.   
  8. //注意:当将参数copyData设为true后,则为深拷贝(复制整个图像数据)  
  9. Mat b = Mat(a, true); //a copy to b  
[cpp] view plain copy print?
  1. //注意:浅拷贝  
  2. Mat a;  
  3. CvMat b = a; //a "copy" to b  
  4.   
  5. //注意:深拷贝  
  6. Mat a;  
  7. CvMat *b;  
  8. CvMat temp = a; //转化为CvMat类型,而不是复制数据  
  9. cvCopy(&temp, b); //真正复制数据 cvCopy使用前要先开辟内存空间  

4、IplImage之间的复制
cvCopy与cvCloneImage使用区别:
cvCopy的原型是:
void cvCopy( const CvArr* src, CvArr* dst, const CvArr* mask=NULL );
在使用这个函数之前,你必须用cvCreateImage()一类的函数先开一段内存,然后传递给dst。cvCopy会把src中的数据复制到dst的内存中。

cvCloneImage的原型是:
IplImage* cvCloneImage( const IplImage* image );
在使用函数之前,不用开辟内存。该函数会自己开一段内存,然后复制好image里面的数据,然后把这段内存中的数据返回给你。

clone是把所有的都复制过来,也就是说不论你是否设置Roi,Coi等影响copy的参数,clone都会原封不动的克隆过来。
copy就不一样,只会复制ROI区域等。用clone复制之后,源图像在内存中消失后,复制的图像也变了,而用copy复制,源图像消失后,复制的图像不变


5、IplImage与Mat互转
[cpp] view plain copy print?
  1. //使用Mat的构造函数:Mat::Mat(const IplImage* img, bool copyData=false);    默认情况下copyData为false  
  2. IplImage* srcImg = cvLoadImage("Lena.jpg");  
  3. //注意:以下三种效果一致,均为浅拷贝  
  4. Mat M(srcImg);  
  5. Mat M(srcImg, false);  
  6. Mat M = srcImg;  
  7.   
  8. //注意:当将参数copyData设为true后,则为深拷贝(复制整个图像数据)  
  9. Mat M(srcImg, true);  
[cpp] view plain copy print?
  1. //注意:浅拷贝 - 同样只是创建图像头,而没有复制数据  
  2. Mat M;  
  3. IplImage img = M;  
  4. IplImage img = IplImage(M);  
  5. //深拷贝  
  6. cv::Mat img2;  
  7. IplImage imgTmp = img2;  
  8. IplImage *input = cvCloneImage(&imgTmp);  

6、IplImage与CvMat互转
[cpp] view plain copy print?
  1. //法一:cvGetMat函数  
  2. IplImage* img;  
  3. CvMat temp;  
  4. CvMat* mat = cvGetMat(img, &temp);    //深拷贝  
  5. //法二:cvConvert函数  
  6. CvMat *mat = cvCreateMat(img->height, img->width, CV_64FC3);    //注意height和width的顺序  
  7. cvConvert(img, mat);    //深拷贝  
[cpp] view plain copy print?
  1. //法一:cvGetImage函数  
  2. CvMat M;  
  3. IplImage* img = cvCreateImageHeader(M.size(), M.depth(), M.channels());  
  4. cvGetImage(&M, img);    //深拷贝:函数返回img  
  5. //也可写成  
  6. CvMat M;  
  7. IplImage* img = cvGetImage(&M, cvCreateImageHeader(M.size(), M.depth(), M.channels()));  
  8. //法二:cvConvert函数  
  9. CvMat M;  
  10. IplImage* img = cvCreateImage(M.size(), M.depth(), M.channels());  
  11. cvConvert(&M, img);    //深拷贝  


三、Mat版CvvIMage
        下面是一个Mat版的CvvImage类,可直接使用Mat
[cpp] view plain copy print?
  1. #pragma once  
  2. #ifndef CVVIMAGE_CLASS_DEF  
  3. #define CVVIMAGE_CLASS_DEF  
  4. #include "opencv2/opencv.hpp"  
  5. /* CvvImage class definition */  
  6. class  CvvImage : public Mat  
  7. {  
  8. public:  
  9.    CvvImage();  
  10.    virtual ~CvvImage();  
  11.    CvvImage(Mat& mat);  
  12.    /* Create image (BGR or grayscale) */  
  13.    virtual bool  Create( int width, int height, int bits_per_pixel );  
  14.    /* Load image from specified file */  
  15.    virtual bool  Load( const char* filename, int desired_color = 1 );  
  16.    /* Load rectangle from the file */  
  17.    virtual bool  LoadRect( const char* filename,  
  18.       int desired_color, Rect 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.          Rect( r.left, r.top, r.right - r.left, r.bottom - r.top ));  
  26.    }  
  27. #endif  
  28.    /* Save entire image to specified file. */  
  29.    virtual bool  Save( const char* filename );  
  30.    /* Get copy of input image ROI */  
  31.    virtual void  CopyOf( Mat& image, int desired_color = -1 );  
  32.    /* width and height of ROI */  
  33.    int Width() { return cols; };  
  34.    int Height() { return rows;};  
  35.    int Bpp() {   
  36.        return ((8<<((1<<depth()/2)>>1)) )*channels(); };  
  37.    /* draw to highgui window */  
  38.    virtual void  Show( const char* window );  
  39.   
  40. #if defined WIN32 || defined _WIN32  
  41.    /* draw part of image to the specified DC */  
  42.    virtual void  Show( HDC dc, int x, int y, int width, int height,  
  43.       int from_x = 0, int from_y = 0 );  
  44.    /* draw the current image ROI to the specified rectangle of the destination DC */  
  45.    virtual void  DrawToHDC( HDC hDCDst, RECT* pDstRect );  
  46. #endif  
  47.   
  48. };  
  49. typedef CvvImage CImage;  
  50. #endif  



下载地址:CvvImage类源码   
                  CvvImage——Mat版
阅读全文
0 0
原创粉丝点击