• Open Source Computer Vision Library

CvvImage类参考手册

Wikipedia,自由的百科全书

CvvImage使用前需要包含 highgui.h 头文件

#include <highgui.h>

警告:参数中含有HDC类型的并不能保证移植到其他平台,例如Show/DrawToHDC等。

目录

CvvImage::Create

bool CvvImage::Create(int w, int h, int bpp, int origin);

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

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

例:

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

CvvImage::CopyOf

void  CvvImage::CopyOf(CvvImage& img, int desired_color);
void  CvvImage::CopyOf(IplImage* img, int desired_color);

从img复制图像到当前的对象中。

img
要复制的图像。
desired_color
为复制后图像的通道数, 复制后图像的像素深度为8bit。

例:

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

CvvImage::Load

bool CvvImage::Load(const char* filename, int desired_color);

装载一个图像。

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

CvvImage::LoadRect

bool CvvImage::LoadRect(const char* filename, int desired_color, CvRect rect);

从图像读出一个区域。

filename
图像名称。
desired_color
图像波段数, 和cvLoadImage类似。
rect
要读取的图像范围。
LoadRect是先用Load装载一个图像, 然后再将rect设置为图像的ROI区域, 然后复制图像得到,因此, 如果一个图像很大(例如几百MB), 即使想从中只读几个像素也是会失败的。

CvvImage::Save

bool  CvvImage::Save(const char* filename);

保存图像。 和cvSaveImage相似。

CvvImage::Show

void  CvvImage::Show(const char* window);

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

CvvImage::Show

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行开始显示。


例:

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

CvvImage::DrawToHDC

void  CImage::DrawToHDC(HDC hDCDst, RECT* pDstRect);

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

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

例:

  1. CvvImage img;
  2. img.Load("example.tiff");
  3.  
  4. CRect rect;
  5. rect.left = 100;
  6. rect.top = 200;
  7. rect.right = rect.left + 600;
  8. rect.bottom = rect.top + 400;
  9.  
  10. img.DrawToHDC(hDC, &rect);

CvvImage::Fill

void CvvImage::Fill(int color);

以color颜色填充图像。


注意事项

highgui.h文件中有一个CImage宏被定义为CvvImage:

#define CImage CvvImage

但是, 由于CImage太常见, 很容易造成冲突, 因此建议不要使用该宏(可以直接删去)。

CvvImage类定义

  1. /* CvvImage class definition */
  2. class CV_EXPORTS CvvImage
  3. {
  4. public:
  5. CvvImage();
  6. virtual ~CvvImage();
  7.  
  8. /* Create image (BGR or grayscale) */
  9. virtual bool Create( int width, int height, int bits_per_pixel, int image_origin = 0 );
  10.  
  11. /* Load image from specified file */
  12. virtual bool Load( const char* filename, int desired_color = 1 );
  13.  
  14. /* Load rectangle from the file */
  15. virtual bool LoadRect( const char* filename,
  16. int desired_color, CvRect r );
  17.  
  18. #ifdef 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.  
  27. /* Save entire image to specified file. */
  28. virtual bool Save( const char* filename );
  29.  
  30. /* Get copy of input image ROI */
  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. /* width and height of ROI */
  38. int Width() { return !m_img ? 0 : !m_img->roi ? m_img->width : m_img->roi->width; };
  39. int Height() { return !m_img ? 0 : !m_img->roi ? m_img->height : m_img->roi->height;};
  40. int Bpp() { return m_img ? (m_img->depth & 255)*m_img->nChannels : 0; };
  41.  
  42. virtual void Fill( int color );
  43.  
  44. /* draw to highgui window */
  45. virtual void Show( const char* window );
  46.  
  47. #ifdef WIN32
  48. /* draw part of image to the specified DC */
  49. virtual void Show( HDC dc, int x, int y, int width, int height,
  50. int from_x = 0, int from_y = 0 );
  51. /* draw the current image ROI to the specified rectangle of the destination DC */
  52. virtual void DrawToHDC( HDC hDCDst, RECT* pDstRect );
  53. #endif
  54.  
  55. protected:
  56.  
  57. IplImage* m_img;
  58. };
  59.  

编写者

Views
Personal tools