CvvImage类以及在MFC中显示IplImage图像的方法
2012-04-03 18:55
561 查看
平常使用Open CV时总是跳出一个个窗口,很难将项目进行系统集成,特别是在MFC等Windows环境中加载显示Open CV中的IplImage图像;
使用Open CVhighgui.h 中定义的CvvImage类,可以很好的实现Open CV和Windows MFC显示接口;先介绍一下CvvImage类:
由于CvvImage是在 highgui.h 头文件中声明的,因此如果您的程序中需要使用,则必须在开头包含此头文件
CvvImage对应CImage宏:
注意事项:
由于CImage太常见, 很容易造成冲突, 因此建议不要使用该宏(可以直接删去此宏定义)。
警告:参数中含有HDC(注:一种windows系统下定义的变量类型,用来描述设备描述表的句柄类型)类型的并不能保证移植到其他平台,例如Show/DrawToHDC等。
后文中的DC,即device context(设备环境),一般可以理解为windows操作系统为方便绘图而抽象的”绘图表面“,“往窗口上绘图”,有时也被说成是“往窗口DC上绘图”。
创建一个图像。 成功返回true, 失败返回false。
w
图像宽
h
图像高
bpp
每个像素的bit数, 值等于像素深度乘以通道数
origin
0 - 顶—左结构, 1 - 底—左结构 (Windows bitmaps 风格)
例:
img 要复制的图像。 desired_color 为复制后图像的通道数, 复制后图像的像素深度为8bit。 例:
filename
图像文件名称。
desired_color
图像波段数, 和cvLoadImage类似。
从图像读出一个区域。
filename 图像名称。
desired_color 图像波段数, 和cvLoadImage类似。
rect要读取的图像范围。
注LoadRect是先用Load装载一个图像, 然后再将rect设置为图像的ROI区域, 然后复制图像得到,因此, 如果一个图像很大(例如几百MB), 即使想从中只读几个像素也是会失败的。
保存图像。 和cvSaveImage相似。
显示一个图像。 和cvShowImage相似。
绘制图像的部分到DC。 图像没有缩放。此函数仅在Windows下有效。
dc 设备描述符。
x 局部图像显示在DC上,从DC上的第x列开始。
y 局部图像显示在DC上,从DC上的第y列开始。
(x,y) 为局部图像显示在DC上的起始位置。
w 局部图像宽度。
h 局部图像高度。
from_x 从图像的第from_x列开始显示。
from_y 从图像的第from_y行开始显示。
例:
绘制图像的ROI区域到DC的pDstRect, 如果图像大小和pDstRect不一致, 图像会拉伸/压缩。此函数仅在Windows下有效。
hDCDst
设备描述符。
pDstRect
对应的设备描述符区域。
例:
使用Open CVhighgui.h 中定义的CvvImage类,可以很好的实现Open CV和Windows MFC显示接口;先介绍一下CvvImage类:
由于CvvImage是在 highgui.h 头文件中声明的,因此如果您的程序中需要使用,则必须在开头包含此头文件
#include <highgui.h>
CvvImage对应CImage宏:
#define CImage CvvImage
注意事项:
由于CImage太常见, 很容易造成冲突, 因此建议不要使用该宏(可以直接删去此宏定义)。
警告:参数中含有HDC(注:一种windows系统下定义的变量类型,用来描述设备描述表的句柄类型)类型的并不能保证移植到其他平台,例如Show/DrawToHDC等。
后文中的DC,即device context(设备环境),一般可以理解为windows操作系统为方便绘图而抽象的”绘图表面“,“往窗口上绘图”,有时也被说成是“往窗口DC上绘图”。
CvvImage::Create
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::CopyOf void CvvImage::CopyOf(CvvImage& img, int desired_color); 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::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行开始显示。
例:
// 从图像10行20列开始, 显示400行600列图像到设备描述符的100行200列开始的位置 CvvImage img; img.Load("example.tiff"); 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
对应的设备描述符区域。
例:
CvvImage img;img.Load("example.tiff"); CRect rect; rect.left= 100; rect.top= 200; rect.right= rect.left+ 600; rect.bottom= rect.top+ 400; img.DrawToHDC(hDC,&rect);
CvvImage::Fill void CvvImage::Fill(int color); 以color颜色填充图像。CvvImage类定义
/* CvvImage class definition */ class CV_EXPORTS CvvImage { public: CvvImage(); virtual ~CvvImage(); /* Create image (BGR or grayscale) */ virtual bool Create( int width, int height, int bits_per_pixel, int image_origin = 0 ); /* Load image from specified file */ virtual bool Load( const char* filename, int desired_color = 1 ); /* Load rectangle from the file */ virtual bool LoadRect( const char* filename, int desired_color, CvRect r ); #ifdef WIN32 virtual bool LoadRect( const char* filename, int desired_color, RECT r ) { return LoadRect( filename, desired_color, cvRect( r.left, r.top, r.right - r.left, r.bottom - r.top )); } #endif /* Save entire image to specified file. */ virtual bool Save( const char* filename ); /* Get copy of input image ROI */ virtual void CopyOf( CvvImage& image, int desired_color = -1 ); virtual void CopyOf( IplImage* img, int desired_color = -1 ); IplImage* GetImage() { return m_img; }; virtual void Destroy(void); /* width and height of ROI */ int Width() { return !m_img ? 0 : !m_img->roi ? m_img->width : m_img->roi->width; }; int Height() { return !m_img ? 0 : !m_img->roi ? m_img->height : m_img->roi->height;}; int Bpp() { return m_img ? (m_img->depth & 255)*m_img->nChannels : 0; }; virtual void Fill( int color ); /* draw to highgui window */ virtual void Show( const char* window ); #ifdef WIN32 /* draw part of image to the specified DC */ virtual void Show( HDC dc, int x, int y, int width, int height, int from_x = 0, int from_y = 0 ); /* draw the current image ROI to the specified rectangle of the destination DC */ virtual void DrawToHDC( HDC hDCDst, RECT* pDstRect ); #endif protected: IplImage* m_img; }; 有了上面对CvvImage类的介绍,可以很容易的在Windows窗口中显示图像了,下面是在MFC的CView窗口中 中显示IplImage图像的函数代码 void CMyView::ShowIplImage(IplImage* img) { CDC* pDC = GetDC(); HDC hDC= pDC->GetSafeHdc(); CRect rect; rect.SetRect(0 , 0 , img->width , img->height); CvvImage cimg; cimg.CopyOf(img); cimg.DrawToHDC(hDC,&rect); ReleaseDC(pDC); } /*在对话框Picture控件上显示IplImage图像 iplimg是待显示的图像,ID是picture控件的ID */ void Cdialog_iplImageDlg::DrawPicToHDC(IplImage* iplimg , UINT ID) { CDC *pDC = GetDlgItem(ID)->GetDC(); HDC hDC= pDC->GetSafeHdc(); CRect rect; GetDlgItem(ID)->GetClientRect(&rect); CvvImage cimg; cimg.CopyOf(iplimg); cimg.DrawToHDC(hDC,&rect); ReleaseDC(pDC); }
相关文章推荐
- CvvImage类以及在MFC中显示IplImage图像的方法(转)
- [转]CvvImage类以及在MFC中显示IplImage图像的方法
- CvvImage类以及在MFC中显示IplImage图像的方法
- CvvImage类以及在MFC中显示IplImage图像的方法
- 利用 OpenCV 在MFC中显示图像问题以及解决方法
- 【VC++】在MFC中显示IplImage图像的方法
- MFC中CImage类显示的半透明PNG存在的问题以及处理方法
- MFC子线程更改图像数据后更新主窗口图像显示方法
- MFC中在picture control中显示图像的方法
- MFC对话框的picture控件中显示IplImage*格式图像
- int类型转换成字符串和字符串转换成int类型以及在MFC静态文本框显示数字的方式方法
- 继承opencv的CImage类在MFC的单文档中编辑并显示图像的方法
- MFC+OpenCV 利用Show方法使图像不拉伸显示在Picture控件上
- 利用MFC和opencv读取视频显示图像方法
- MFC+Opencv显示图像的几种方法
- 关于MFC中显示OpenCV Mat矩阵中的图像(一种新的解决方法)
- MFC+Opencv显示图像的几种方法
- OPENCV+MFC单文档显示图像的两种方法
- vc/mfc获取rgb图像数据后动态显示及保存图片的方法
- vc/mfc获取rgb图像数据后动态显示及保存图片的方法