1、MFC下OpenCV开发环境搭建
所需文件:openCV2.4.9编译库。
建立过程:
(1)新建MFC对话框工程。
(2)设置工程属性
(2.1)设置”C/C++”->”附加库包含目录”为:F:\opencv\build\include
(2.2)设置”链接器”->”附加依赖项”如下:
opencv_calib3d249.lib
opencv_contrib249.lib
opencv_core249.lib
opencv_features2d249.lib
opencv_flann249.lib
opencv_gpu249.lib
opencv_highgui249.lib
opencv_imgproc249.lib
opencv_legacy249.lib
opencv_ml249.lib
opencv_nonfree249.lib
opencv_objdetect249.lib
opencv_ocl249.lib
opencv_photo249.lib
opencv_stitching249.lib
opencv_superres249.lib
opencv_ts249.lib
opencv_video249.lib
opencv_videostab249.lib
(2.3)设置”链接器”->”附加库目录”为:F:\opencv\build\x64\vc12\lib
(2.4)设置”C/C++”->”预处理器”,添加_CRT_SECURE_NO_WARNINGS
2、使用OpenCV打开摄像机并获取视频帧
需要使用cv::VideoCapture类创建摄像机。
cv::VideoCapture *pcCamera;//PC摄像机
打开摄像机:
if (pcCamera == NULL)
{
pcCamera = new cv::VideoCapture(0);
}
if (pcCamera->isOpened() == FALSE)
{
AfxMessageBox(_T("摄像头未打开!"));
pcCamera->open(0);
}
从摄像机获取视频帧:
cv::Mat frame;//视频帧
(*(mainDlg->pcCamera)) >> frame;//从摄像机中取出视频帧
将openCV的MAT转换为GDI+的Bitmap:
首先需要将MAT转换为IplImage:
IplImage frameImage = frame.operator IplImage();
然后使用转换函数将IplImage转换为Bitmap:
Bitmap *bitmapFrame = IplImageToBitmap(&frameImage);
关闭摄像机:
if (!pcCamera)
{
if (pcCamera->isOpened())
{
pcCamera->release();
delete pcCamera;
pcCamera = NULL;
}
}
3、使用GDI+绘制Bitmap:
声明类成员变量:
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
在窗体初始化函数中对GDI+进行初始化:
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);//安装GDI+
GDI+绘图:
HWND hWnd;
GetDlgItem(IDC_PANEL_VIDEO, &hWnd);
Graphics *graph = Graphics::FromHWND(hWnd);
graph->DrawImage(bitmapFrame, 0, 0);
delete bitmapFrame;
bitmapFrame = NULL;
delete graph;
graph = NULL;
在窗体关闭函数中对GDI+进行卸载:
GdiplusShutdown(gdiplusToken);//卸载GDI+
OpenCV IplImage和GDI+ Bitmap的相互转换请参见:
OpenCV IplImage和GDI+ Bitmap的相互转换 - 晨曦朝霞 - 博客频道 - CSDN.NET http://blog.csdn.net/zhangchen9091/article/details/50630383