这里将我看过的VC显示视频方法总结一下。一种看了三种方法。
方法一:OpenCV VFW
方法二:OpenCV DirectShow
方法三:直接的采用DirectShow,下面俄罗斯人写的
Video Preview and Frames Capture to Memory with SampleGrabber in Buffered Mode.
这些方法都是我编辑别人的,没有什么创新,只是一个使用方法。所以我只是介绍使用方法以及相关代码,可能有其他更好的办法。我只是一个刚刚入门者,有不妥之处,敬请拍砖。
OpenCV内置视频显示是采用VFW模式,我用的版本是2.45
代码如下:
1:先引入
#include <cv.h>
#include <highgui.h>
2:头文件定义变量
CvCapture* m_Camera;
IplImage* m_CameraFrame;
3、设置一个按钮,定义事件
void CEVisionDlg::OpenVideo()
{
m_CameraFrame = 0;
m_Camera = 0;
HWND hWnd;
HWND hParent;
RECT rcDlg; //ScreenToClient
int vWndType1Height;
int vWndType1Windth;
m_Camera = cvCreateCameraCapture( CV_CAP_ANY );
int intCamSRCWidth,intCamSRCHeight;
int intImgSRCHeight,intImgSRCWindth;
intCamSRCWidth=640;
intCamSRCHeight=480;
intImgSRCWindth=640;
intImgSRCHeight=480;
cvSetCaptureProperty( m_Camera, CV_CAP_PROP_FRAME_WIDTH, double(intCamSRCWidth));
cvSetCaptureProperty( m_Camera, CV_CAP_PROP_FRAME_HEIGHT, double(intCamSRCHeight));
intCamSRCWidth = int(cvGetCaptureProperty( m_Camera, CV_CAP_PROP_FRAME_WIDTH));
intCamSRCHeight = int (cvGetCaptureProperty( m_Camera, CV_CAP_PROP_FRAME_HEIGHT));
if (m_Camera > 0)
{
GetDlgItem(IDC_SHOW_IMG)->GetWindowRect(&rcDlg);
vWndType1Windth = rcDlg.right - rcDlg.left -10;
vWndType1Height = vWndType1Windth * intImgSRCHeight;
vWndType1Height = vWndType1Height / intImgSRCWindth;
cvNamedWindow("AIWndSrcCamera", CV_WINDOW_AUTOSIZE); //创建一个新窗口
hWnd = (HWND) cvGetWindowHandle("AIWndSrcCamera"); //通过名字获取窗口句柄
hParent = ::GetParent(hWnd);
::SetParent(hWnd, GetDlgItem(IDC_SHOW_IMG)->m_hWnd);
::ShowWindow(hParent, SW_HIDE);
cvResizeWindow("AIWndSrcCamera", vWndType1Windth,vWndType1Height);
IplImage* m_pFrame = cvCreateImage( cvSize(intImgSRCWindth,intImgSRCHeight), IPL_DEPTH_8U, 3);
while (true)
{
m_CameraFrame =cvQueryFrame (m_Camera);
if(!m_CameraFrame)
return;
else
{
if( m_CameraFrame->origin == IPL_ORIGIN_TL )
cvResize (m_CameraFrame, m_pFrame, CV_INTER_NN);
else
cvFlip( m_CameraFrame, m_pFrame, 0 );
cvShowImage("AIWndSrcCamera", m_pFrame);
if ((cvWaitKey(10) & 255) == 27 ) return ;
}
}
cvReleaseImage(&m_pFrame);
}
}
版权声明:本文为light169原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。