#include "stdafx.h" #include <iostream> #include "cv.h" #include "highgui.h" #include "cxcore.h" int _tmain(int argc, _TCHAR* argv[]) { IplImage* frame = NULL; CvCapture* capture = cvCreateCameraCapture(0); CvVideoWriter* video=NULL; if(!capture) { cout<<"Camera does not work correctly!\n"<<endl; } else { frame=cvQueryFrame(capture); //首先取得摄像头中的一帧 video=cvCreateVideoWriter("D:\\XXX.avi", CV_FOURCC('X', 'V', 'I', 'D'), 25, cvSize(frame->width,frame->height)); //创建CvVideoWriter对象并分配空间 //保存的文件名为camera.avi,编码要在运行程序时选择,大小就是摄像头视频的大小,帧频率是32 if(video) //如果能创建CvVideoWriter对象则表明成功 { cout<<"VideoWriter has created."<<endl; } cvNamedWindow("Camera Video",1); //新建一个窗口 while(1) { if(!cvQueryFrame(capture) ) { cout<<"do not receive any picture!"; break; } else { cout<<cvWriteFrame(video,frame)<<endl; cvShowImage("Camera Video",frame); if(cvWaitKey(2)>0) break; } } } cvReleaseCapture(&capture); cvDestroyWindow("avi"); return 0; }
注意,cvWriteFrame();函数只能够把三通道图像保存为视频,对于单通道的图像可以采用复制成三通道图像的方式来保存录制的视频。
int _tmain(int argc, _TCHAR* argv[]) { IplImage* frame = NULL; IplImage* frame_single = NULL; CvCapture* capture = cvCreateCameraCapture(0); CvVideoWriter* video=NULL; frame = cvQueryFrame(capture); //首先取得摄像头中的一帧 frame_single = cvCreateImage(cvGetSize(frame),frame->depth,1); video = cvCreateVideoWriter("D:\\camera_mao_bian_guang.avi", CV_FOURCC('X', 'V', 'I', 'D'),15, cvSize(frame->width,frame->height)); cvNamedWindow("Camera Video",1); //新建一个窗口 while(1) { frame = cvQueryFrame(capture); cvCvtColor(frame,frame_single,CV_BGR2GRAY); cvMerge(frame_single,frame_single,frame_single,NULL,frame); cvWriteFrame(video,frame); cvShowImage("Camera Video",frame_single); if(cvWaitKey(2)>0) break; } cvReleaseCapture(&capture); cvDestroyWindow("Camera Video"); return 0; }
char FilePath[100]; sprintf(FilePath,"D:\\PERSONAL\\VC++\\OpenCV\\image\\hyhead4.jpg"); IplImage* src = cvLoadImage(FilePath); mf_displayCVimage(IDC_PICTURE_HSV,src);另一种:
char FilePath[100] = {NULL}; CString str_path = NULL; if(dlg.DoModal()==IDOK) str_path = dlg.GetPathName(); int len = str_path.GetLength(); for(int i = 0;i<len;i++) { FilePath[i] = str_path.GetAt(i); } IplImage* src = cvLoadImage(FilePath,0);
char path[] path相当于指针。