使用Opencv调用摄像头并在MFC中进行视频显示

1.打开摄像机

cv::VideoCapture g_camera(0);
if (!g_camera.isOpened())
{
	g_camera = cv::VideoCapture(1);
}
if (!g_camera.isOpened())
{
	std::cerr << "Failed opening g_camera" << std::endl;
	return 1;
}
if (g_camera.isOpened())
{
	  g_camera.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
	  g_camera.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
}
//sometimes the first few frames are empty
{
  cv::Mat frame;
  while (frame.empty())
  {
	  g_camera >> frame;
	  cv::waitKey(10);
  }
}

while (1)
{
  //get image from camera
  cv::Mat frame;
  g_camera >> frame;
}
将Mat转换为Windows图片格式CImage,在视图类OnDraw函数中绘制每一帧图像:

CClientDC dc(this);
HDC DC = dc.GetSafeHdc();
ImageUtility imgut;
imgut.MatToCImage(m, g_img);
g_img.Draw(DC, rc1);
ImageUtility源码参见:http://blog.csdn.net/shenshen211/article/details/52489086 点击打开链接
 
 

你可能感兴趣的:(MFCWin32,图像处理)