#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])//本人是在VC2003下写的代码,如果在其他环境下调试请注意修改
{
CvCapture *pCamCapture = cvCaptureFromCAM(0);
if (!pCamCapture)
{
cout << "cvCaptureFromCAM() error..." << endl;
cvWaitKey(0);
return -1;
}
int frameH = (int) cvGetCaptureProperty(pCamCapture, CV_CAP_PROP_FRAME_HEIGHT);
int frameW = (int) cvGetCaptureProperty(pCamCapture, CV_CAP_PROP_FRAME_WIDTH);
CvVideoWriter *pWriter = NULL;
int isColor = 1;
double fps = 20.0; // or 30
//pWriter = cvCreateVideoWriter("c://out.avi", CV_FOURCC('P','I','M','1'), fps,cvSize(frameW,frameH), isColor);
//int outCompressCodec = -1 ;//如果你不知道什么codec,设置为-1
cvNamedWindow("cam",1);
IplImage *pFrame=NULL;
IplImage *image=NULL;
for (int i = 0; i < 50; ++i)
{
pFrame = cvQueryFrame(pCamCapture);
if(i==0)
{
int outCompressCodec = cvGetCaptureProperty(pCamCapture,CV_CAP_PROP_FOURCC);
pWriter=cvCreateVideoWriter("C://out.avi",outCompressCodec, 15, cvSize(pFrame->width, pFrame->height), 1 );
}
if (!pFrame)
{
cout<<"pFrame==null"<<endl;
return 0;
}
cvShowImage("cam", pFrame);
image = cvCreateImage( cvGetSize(pFrame), 8, 3 );
image->origin = pFrame->origin;
cvCopy(pFrame,image);
if (!pWriter)
{
cout << "pWriter Error" << endl;
cvWaitKey(0);
return -1;
}
cvWriteFrame(pWriter, image);
cvWaitKey(30);
}
cvWaitKey(30);
cvDestroyWindow("cam");
cvReleaseVideoWriter(&pWriter);
cvReleaseCapture(&pCamCapture);
pFrame=NULL;//这里可以不用释放.如果不在这里设定为空的话,会重新释放pCamCapture,会出错
cvReleaseImage(&pFrame);
cvReleaseImage(&image);
return 0;
}
我还用VC写了一个读从摄象头里面读取图象,然后在从自己保存的avi文件把视频读出来显示.代码也在下面,希望大家交流指点.
void CCamDetectorDlg::OnBnClickedOpen()
{
// TODO: 在此添加控件通知处理程序代码
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer ();
int nPos;
nPos=sPath.ReverseFind ('//');
sPath=sPath.Left(nPos);
CString lpszFile = sPath + "//out.avi";
CvCapture *pCamCapture = cvCaptureFromCAM(-1);
if (!pCamCapture)
{
//cout << "cvCaptureFromCAM() error..." << endl;
MessageBox("cvCaptureFromCAM() error...",NULL,MB_ICONEXCLAMATION | MB_ICONWARNING | MB_OK);
cvWaitKey(0);
cvReleaseCapture(&pCamCapture);
return ;
}
int frameH = (int) cvGetCaptureProperty(pCamCapture, CV_CAP_PROP_FRAME_HEIGHT);
int frameW = (int) cvGetCaptureProperty(pCamCapture, CV_CAP_PROP_FRAME_WIDTH);
cvSetCaptureProperty(pCamCapture,CV_CAP_PROP_FOURCC,CV_FOURCC('D', 'I', 'V', '3'));
CvVideoWriter *pWriter = NULL;
int isColor = 1;
double fps = 20.0; // or 30
//pWriter = cvCreateVideoWriter("c://out.avi", CV_FOURCC('P','I','M','1'), fps,cvSize(frameW,frameH), isColor);
//int outCompressCodec = -1 ;//如果你不知道什么codec,设置为-1
int outCompressCodec=CV_FOURCC('D', 'I', 'V', '3');
int nFrmNum=0;
HWND wnd=NULL;
cvNamedWindow("cam",1);
IplImage *pFrame=NULL;
IplImage *image=NULL;
//for (int i = 0; ; ++i)
while(pFrame = cvQueryFrame(pCamCapture))
{
if((wnd=(HWND)cvGetWindowHandle("cam")))
{
nFrmNum++;
Sleep(40);
//pFrame = cvQueryFrame(pCamCapture);
if(nFrmNum==1)
{
//outCompressCodec = (int)cvGetCaptureProperty(pCamCapture,CV_CAP_PROP_FOURCC);
pWriter=cvCreateVideoWriter(lpszFile,outCompressCodec, 15, cvSize(pFrame->width, pFrame->height), 1 );
if (!pWriter)
{
MessageBox("CAM pWriter Error!",NULL,MB_ICONEXCLAMATION | MB_ICONWARNING | MB_OK);
cvWaitKey(0);
break ;
}
cvShowImage("cam", pFrame);
/*image = cvCreateImage( cvGetSize(pFrame), 8, 3 );
image->origin = pFrame->origin;
cvCopy(pFrame,image);
*/
//cvWriteFrame(pWriter, image);
cvWriteFrame(pWriter, pFrame);
cvWaitKey(30);
}
else{
cvShowImage("cam", pFrame);
//image = cvCreateImage( cvGetSize(pFrame), 8, 3 );
//image->origin = pFrame->origin;
//cvCopy(pFrame,image);
//cvWriteFrame(pWriter, image);
cvWriteFrame(pWriter, pFrame);
cvWaitKey(30);
}
}
else
break;
}
cvWaitKey(30);
cvReleaseVideoWriter(&pWriter);
cvReleaseCapture(&pCamCapture);
pFrame=NULL;
//cvReleaseImage(&pFrame);
cvReleaseImage(&image);
cvDestroyWindow("cam");
}
void CCamDetectorDlg::OnBnClickedShowVideo()
{
// TODO: 在此添加控件通知处理程序代码
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer ();
int nPos;
nPos=sPath.ReverseFind ('//');
sPath=sPath.Left(nPos);
CString lpszFile = sPath + "//out.avi";
//声明IplImage指针
IplImage* pFrame = NULL;
CvCapture* pCapture = NULL;
cvNamedWindow("video", 1);
FromHandle((HWND)cvGetWindowHandle("video"))->SetFocus();
int nFrmNum = 0;
if( !(pCapture = cvCaptureFromAVI(lpszFile)))
{
CString strError="";
strError.Format("Can not open video file %s/n", "out.avi");
MessageBox(strError,NULL,MB_ICONEXCLAMATION | MB_ICONWARNING | MB_OK);
cvReleaseCapture(&pCapture);
return ;
}
int count = (int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
//cvSetCaptureProperty(pCapture, CV_CAP_PROP_POS_AVI_RATIO, (double)0.9);
//cvSetCaptureProperty(pCapture, CV_CAP_PROP_FOURCC, CV_FOURCC('D', 'I', 'V', '3'));
for(int i=0;i<count;i++)
{
if(!cvGrabFrame(pCapture))
break;
pFrame=cvRetrieveFrame(pCapture);
if(!pFrame)
break;
nFrmNum++;
cvShowImage("video", pFrame);
cvWaitKey(20);
}
/*while(pFrame = cvQueryFrame( pCapture ))
{
nFrmNum++;
cvShowImage("video", pFrame);
cvWaitKey(20);
}
*/
//销毁窗口
CString strName="";
strName.Format("播放视频总共%d帧 /n", nFrmNum);
MessageBox(strName,NULL,MB_ICONASTERISK | MB_ICONINFORMATION | MB_OK);
//释放图像和矩阵
cvDestroyWindow("video");
cvWaitKey(20);
cvReleaseCapture(&pCapture);
//cvReleaseImage(&pFrame);
}