您的位置:首页 > 运维架构

OpenCV 2.2版本以上显示图片到 MFC 的 Picture Control 控件中

2014-10-24 22:20 489 查看


[日期:2014-06-06] 来源:Linux社区 作者:oHanTanYanYing [字体:大 中 小]

OpenCV 2.2 以及后面的版本取消掉了 CvvImage.h 和CvvImage.cpp 两个文件,直接导致了苦逼的程序员无法调用里面的显示函数来将图片显示到 MFC 的 Picture Control 控件中。为此,网上很多人表示只要将那两个文件人为的提取出来然后放到工程里面就解决问题了,也提供了两个文件的下载,但是这麻烦不说,还会导致一些奇奇怪怪的报错(至少本人是这样的,很崩溃!)。所以在了解了一些gdi绘图之后结合网上的代码写了如下的函数,只需调用就可以将OpenCV的图片显示在上面了,初步测试效率跟原来两个文件差不多,如果有大神请帮我完善这份代码!

--------------------------------------分割线 --------------------------------------

编辑推荐:

Ubuntu Linux下安装OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm
Ubuntu 12.04 安装 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm
CentOS下OpenCV无法读取视频文件 http://www.linuxidc.com/Linux/2011-07/39295.htm
Ubuntu 12.04下安装OpenCV 2.4.5总结 http://www.linuxidc.com/Linux/2013-06/86704.htm
Ubuntu 10.04中安装OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm
基于QT和OpenCV的人脸识别系统 http://www.linuxidc.com/Linux/2011-11/47806.htm
--------------------------------------分割线 --------------------------------------

配置好 OpenCV 后,在文件头部添加如下一行代码:

#define WIDTHBYTES(bits) (((bits)+31)/32*4)//用于使图像宽度所占字节数为4byte的倍数

在对话框类中声明函数:

void drawpic(IplImage* img, unsigned int id);//绘图到 MFC 的 Picture Control 控件相关函数,参数一为 OpenCV 的图像数据结构类,参数二为 Picture Control 控件的id

定义函数如下:

void CMFCOpenCVShowDlg::drawpic(IplImage* img, unsigned int id)//CMFCOpenCVShowDlg 为对话框类名

{

BYTE *g_pBits;

HDC g_hMemDC;

HBITMAP g_hBmp, g_hOldBmp;

CDC *pDC;

CStatic *pic;

int width, height;

CRect rect;

pDC = GetDlgItem(id)->GetDC();

pic = (CStatic*)GetDlgItem(id);

pic->GetClientRect(&rect);

width = rect.Width();

height = rect.Height();

g_hMemDC = ::CreateCompatibleDC(pDC->m_hDC);//创建兼容DC

BYTE bmibuf[sizeof(BITMAPINFO)+256 * sizeof(RGBQUAD)];

memset(bmibuf, 0, sizeof(bmibuf));

BITMAPINFO *pbmi = (BITMAPINFO*)bmibuf;

pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

pbmi->bmiHeader.biWidth = img->width;

pbmi->bmiHeader.biHeight = img->height;

pbmi->bmiHeader.biPlanes = 1;

pbmi->bmiHeader.biBitCount = 24;

pbmi->bmiHeader.biCompression = BI_RGB;

g_hBmp = ::CreateDIBSection(g_hMemDC, pbmi, DIB_RGB_COLORS, (void**)&g_pBits, 0, 0);//创建应用程序可以直接写入的、与设备无关的位图(DIB)

g_hOldBmp = (HBITMAP)::SelectObject(g_hMemDC, g_hBmp);//复原兼容DC数据

BitBlt(g_hMemDC, 0, 0, width, height, pDC->m_hDC, 0, 0, SRCCOPY);

//修改图像内容:g_pBits

int l_width = WIDTHBYTES(img->width* pbmi->bmiHeader.biBitCount);

for (int row = 0; row < img->height; row++)

memcpy(&g_pBits[row*l_width], &img->imageData[ (img->height - row - 1)*l_width], l_width);

TransparentBlt(pDC->m_hDC, 0, 0, width, height, g_hMemDC, 0, 0, img->width, img->height, RGB(0, 0, 0));

SelectObject(g_hMemDC, g_hOldBmp);

//释放内存资源

ReleaseDC(pDC);

DeleteDC(g_hMemDC);

DeleteObject(pic);

DeleteObject(g_hBmp);

DeleteObject(g_hOldBmp);

}

在需要将图片显示到 Picture Control 控件的地方添加如下代码即可:

char *filename = "pic1.jpg";//图像路径

IplImage* img = cvLoadImage(filename);

drawpic(img, IDC_STATIC_SHOW);//IDC_STATIC_SHOW 为Picture Control 控件的ID

cvReleaseImage(&img);

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-06/102746.htm

本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2014-06/102746.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: