OpenCV2.2以后取消了CvvImage类的相关文件,其内部函数DrawToHDC,导致了在后续版本中无法再MFC的Picture空间中显示图片,一般的做法是将相应的文件拷贝的工程目录下即可,在这里,转载了一篇写了一个内置函数实现的显示,亲测可用。测试环境为VS2013+OpenCV2.4.9。
首先配置了OpenCV以及VS2013的环境后,即可开始实现;
第一步:建立一个简单的对话框程序。添加打开按钮以及Pic控件,将控件的ID改为ID_VIEW,同时建立按钮的响应事件。
接着在头文件的首部添加宏定义:#define WIDTHBYTES(bits) (((bits)+31)/32*4)//用于使图像宽度所占字节数为4byte的倍数
在CMFC_OpenCVtestDlg类中添加成员变量,属性为protected:
<span style="font-family:Microsoft YaHei;"><span style="font-family:Microsoft YaHei;font-size:12px;"><span style="white-space:pre"> </span>CRect rect; CStatic* pStc; //标识图像显示的Picture控件 CDC* pDC; //视频显示控件设备上下文 HDC hDC; //视频显示控件设备句柄 <span style="white-space:pre"> </span>CvCapture* capture; //视频获取结构</span></span>
对话框中的构造函数中添加相应的初始化代码:
<span style="font-family:Microsoft YaHei;"><span style="font-family:Microsoft YaHei;font-size:12px;"><span style="white-space:pre"> </span>pStc = NULL; pDC = NULL; capture = NULL;</span></span>
<span style="font-family:Microsoft YaHei;"><span style="font-family:Microsoft YaHei;font-size:12px;"><span style="white-space:pre"> </span>pStc = (CStatic *)GetDlgItem(IDC_VIEW);//IDC_VIEW为Picture控件ID pStc->GetClientRect(&rect);//将CWind类客户区的坐标点传给矩形 pDC = pStc->GetDC(); //得到Picture控件设备上下文 hDC = pDC->GetSafeHdc(); //得到控件设备上下文的句柄 </span></span>最后在CMFC_OpenCVtestDlg类中添加成员函数:
<span style="font-family:Microsoft YaHei;"><span style="font-family:Microsoft YaHei;">void CMFC_OpenCVtestDlg::drawpic(IplImage * img, unsigned int id) { 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); }</span> </span>
<span style="font-family:Microsoft YaHei;"><span style="font-family:Microsoft YaHei;font-size:12px;">void CMFC_OpenCVtestDlg::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 //CDialogEx::OnOK(); char *filename = "壁纸2.jpg";//图像路径 IplImage* img = cvLoadImage(filename); drawpic(img, IDC_VIEW);//IDC_STATIC_SHOW 为Picture Control 控件的ID cvReleaseImage(&img); } </span></span>