forked from zjucsxxd/VideoStitch-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_stitch.cpp
214 lines (193 loc) · 5.12 KB
/
video_stitch.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// VideoStitchOpenCV.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/stitcher.hpp"
#include "video_stitcher.h"
using namespace cv;
using namespace std;
static void printUsage()
{
cout <<
"视频拼接.\n\n"
"VideoStitch [flags]\n"
"flags:\n"
" --camera n width height 摄像机模式,n个,分辨率width*height\n"
" video1 video2 ... 视频模式,输入视频路径(视频模式和摄像机模式只能开启一种)\n"
" --save save_path 输出视频路径(不填则强行开启拼接预览)\n"
" -v 开启拼接预览\n"
" --range start end 拼接范围,从start到end帧,end=-1表示拼接到结尾\n"
" -gpu 尝试使用GPU加速\n"
" -plane 尝试平面投影,仅在拼接视角小于140°时可用\n"
" -trim 尝试裁剪未填充区域,仅在平面投影时可用\n"
" --trim x1 y1 x2 y2 按照x1 y1 x2 y2构成的矩形裁剪最终结果\n"
" --debug debug_path debug 模式,设定debug保存目录debug_path,debug数据中包含相机参数,要确保该路径存在!\n"
" --cp camera_param_path 使用camera_param_path的摄像机参数\n";
}
static bool is_camera = false, is_view = false, is_save = false, is_debug = false;
static int cam_width, cam_height, cam_num;
static vector<string> video_names;
static string save_path, debug_path, cam_param_path = "";
static bool is_try_gpu = false, is_trim = false, is_trim_rect = false;
static int range_start = 0, range_end = -1;
static string warp_type = "cylindrical";
static Rect trim_rect;
static int parseCmdArgs(int argc, char* argv[])
{
if(argc == 1)
{
printUsage();
return -1;
}
video_names.clear();
for(int i = 1; i < argc; i++)
{
if (string(argv[i]) == "--help" || string(argv[i]) == "/?")
{
printUsage();
return -1;
}
else if(string(argv[i]) == "--camera")
{
cam_num = atoi(argv[i + 1]);
cam_width = atoi(argv[i + 2]);
cam_height = atoi(argv[i + 3]);
is_camera = true;
i += 3;
}
else if(string(argv[i]) == "--save")
{
save_path = argv[i+1];
is_save = true;
i++;
}
else if(string(argv[i]) == "-v")
is_view = true;
else if(string(argv[i]) == "--range")
{
range_start = atoi(argv[i + 1]);
range_end = atoi(argv[i + 2]);
i += 2;
}
else if(string(argv[i]) == "-gpu")
is_try_gpu = true;
else if(string(argv[i]) == "-trim")
is_trim = true;
else if(string(argv[i]) == "--trim")
{
is_trim = is_trim_rect = true;
int x1 = atoi(argv[i + 1]);
int y1 = atoi(argv[i + 2]);
int x2 = atoi(argv[i + 3]);
int y2 = atoi(argv[i + 4]);
trim_rect = Rect(x1, y1, x2 - x1, y2 - y1);
i += 4;
}
else if(string(argv[i]) == "--debug")
{
is_debug = true;
debug_path = argv[i+1];
i++;
}
else if(string(argv[i]) == "--cp")
{
cam_param_path = argv[i+1];
i++;
}
else if(string(argv[i]) == "-plane")
warp_type = "plane";
else
video_names.push_back(argv[i]);
}
if(!is_save)
is_view = true;
if(!is_camera && video_names.size() == 0)
{
printUsage();
return -1;
}
return 0;
}
//两个样例:
// VideoStitch data/6-2/my_cam_0.avi data/6-2/my_cam_1.avi data/6-2/my_cam_2.avi data/6-2/my_cam_3.avi data/6-2/my_cam_4.avi data/6-2/my_cam_5.avi -v -gpu
// VideoStitch --camera 5 1280 720 -v -gpu --debug data/tmp/ --cp data/tmp/camera_param_5.dat
static int VideoStitch(int argc, char* argv[])
{
for(int i = 0; i < argc; i++)
printf("%s\n", argv[i]);
int retval = parseCmdArgs(argc, argv);
if(retval)
return retval;
for(int i = 0; i < video_names.size(); i++)
cout << video_names[i] << endl;
// 输入视频流
vector<VideoCapture> captures;
if(is_camera)
{
for(int cam_idx = 0; cam_idx < cam_num; cam_idx++)
{
VideoCapture cam_cap;
if(cam_cap.open(cam_idx))
{
cam_cap.set(CV_CAP_PROP_FRAME_WIDTH, cam_width);
cam_cap.set(CV_CAP_PROP_FRAME_HEIGHT, cam_height);
cam_cap.set(CV_CAP_PROP_FPS, 15);
captures.push_back(cam_cap);
cout << "camera " << cam_idx << " opened successfully." << endl;
}
else
break;
}
if(captures.size() == 0)
{
cout << "No camera captured. Please check!" << endl;
return -1;
}
}
else
{
int video_num = video_names.size();
captures.resize(video_num);
for(int i = 0; i < video_num; i++)
{
captures[i].open(video_names[i]);
if(!captures[i].isOpened())
{
cout << "Fail to open " << video_names[i] << endl;
for(int j = 0; j < i; j++) captures[j].release();
return -1;
}
}
}
cout << "Video capture success" << endl;
MyVideoStitcher video_stitcher;
// 显示/保存
video_stitcher.setPreview(is_view);
video_stitcher.setSave(is_save);
video_stitcher.setRange(range_start, range_end);
// 拼接参数
video_stitcher.setTryGPU(is_try_gpu);
video_stitcher.setTrim(is_trim);
if(cam_param_path != "")
video_stitcher.loadCameraParam(cam_param_path);
if(is_debug)
video_stitcher.setDebugDirPath(debug_path);
if(is_trim_rect)
video_stitcher.setTrim(trim_rect);
video_stitcher.setWarpType(warp_type);
// 拼接
video_stitcher.stitch(captures, save_path);
// 释放资源
for(int i = 0; i < captures.size(); i++)
captures[i].release();
cout << "Released all" << endl;
return 0;
}
int main(int argc, char* argv[])
{
VideoStitch(argc, argv);
system("pause");
return 0;
}