1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
// ConsoleApplication2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <opencv2\opencv.hpp>
using
namespace
cv;
using
namespace
std;
int
main()
{
IplImage* pSaveFrame=NULL;
CvCapture* capture=cvCaptureFromCAM(-1);
CvVideoWriter* video=NULL;
IplImage* frame=NULL;
char
filename[20];
int
i=0;
int
p[3];
p[0] = CV_IMWRITE_JPEG_QUALITY;
p[1] = 10;
//质量值
p[2] = 0;
int
n;
if
(!capture)
//如果不能打开摄像头给出警告
{
cout<<
"Can not open the camera."
<<endl;
return
-1;
}
else
{
frame=cvQueryFrame(capture);
//首先取得摄像头中的一帧
video=cvCreateVideoWriter(
"camera.avi"
,-1,15,
cvSize(frame->width,frame->height));
//创建CvVideoWriter对象并分配空间
//保存的文件名为camera.avi,编码要在运行程序时选择,大小就是摄像头视频的大小,帧频率是32
if
(video)
//如果能创建CvVideoWriter对象则表明成功
{
cout<<
"VideoWriter has created."
<<endl;
}
cvNamedWindow(
"Camera Video"
,1);
//新建一个窗口
while
(1)
{
frame=cvQueryFrame(capture);
//从CvCapture中获得一帧
if
(!frame)
{
cout<<
"Can not get frame from the capture."
<<endl;
break
;
}
n=cvWriteFrame(video,frame);
//判断是否写入成功,如果返回的是1,表示写入成功
cout<<n<<endl;
cvShowImage(
"Camera Video"
,frame);
//显示视频内容的图片
pSaveFrame=cvCreateImage(cvSize(frame->width,frame->height),frame->depth,frame->nChannels);
sprintf_s(filename,
"%d.jpg"
,i);
i++;
cvResize(frame,pSaveFrame,CV_INTER_LINEAR);
cvSaveImage(filename,pSaveFrame,p);
cvReleaseImage(&pSaveFrame);
if
(cvWaitKey(1)>0)
break
;
//有其他键盘响应,则退出
}
cvReleaseVideoWriter(&video);
cvReleaseCapture(&capture);
cvDestroyWindow(
"Camera Video"
);
}
return
0;
}
|
opencv摄像头视频+帧保存为图片
最新推荐文章于 2024-08-06 17:32:31 发布