大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
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;*/
is_camera = false;
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(CAP_PROP_FRAME_WIDTH, cam_width);
cam_cap.set(CAP_PROP_FRAME_HEIGHT, cam_height);
cam_cap.set(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 MyVideoStitcher::stitch( vector<VideoCapture> &captures, string &writer_file_name )
{
int video_num = captures.size();
vector<Mat> src(video_num);
Mat frame, dst, show_dst;
// Debug用信息
bool is_save_input_frames = false;
bool is_save_output_frames = true;
double fps = captures[0].get(CAP_PROP_FPS);
// skip some frames
for(int j = 0; j < video_num; j++)
for(int i = 0; i < start_frame_index_; i++)
captures[j].read(frame);
// 第一帧,做一些初始化,并且确定结果视频的分辨率
for(int j = 0; j < video_num; j++)
{
if( !captures[j].read(frame))
return -1;
frame.copyTo(src[j]);
if(is_debug_)
{
char img_save_name[100];
sprintf(img_save_name, "/%d.jpg", j+1);
imwrite(debug_dir_path_ + img_save_name, src[j]);
}
}
long prepare_start_clock = clock();
int prepare_status = Prepare(src);
// 先用ORB特征测试,错误的话再使用SURF,仍然错误则报错,输入视频不符合条件
if( prepare_status == STITCH_CONFIG_ERROR )
{
cout << "video stitch config error!" << endl;
return -1;
}
if( prepare_status != STITCH_SUCCESS)
{
features_type_ = "surf";
cout << "video stitch first try failed, second try ... " << endl;
if( Prepare(src) != STITCH_SUCCESS)
{
cout << "videos input are invalid. Initialization failed." << endl;
return -1;
}
}
long prepare_end_clock = clock();
cout << "\tprepare time: " << prepare_end_clock - prepare_start_clock << "ms" << endl;
StitchFrame(src, dst);
if(is_debug_) //保存第一帧拼接结果和mask
{
imwrite(debug_dir_path_ + "/res.jpg", dst);
vector<Mat> img_masks(video_num);
for(int i = 0; i < video_num; i++)
{
img_masks[i].create(src[i].rows, src[i].cols, CV_8UC3);
img_masks[i].setTo(Scalar::all(255));
}
Mat dst_mask;
StitchFrame(img_masks, dst_mask);
imwrite(debug_dir_path_ + "/mask.jpg", dst_mask);
}
// 创建结果视频
VideoWriter writer;
if(is_save_video_)
{
writer.open(writer_file_name, CAP_OPENCV_MJPEG, 20, Size(dst.cols, dst.rows));
writer.write(dst);
}
// 开始拼接
double stitch_time = 0;
FrameInfo frame_info;
frame_info.src.resize(video_num);
int frameidx = 1;
cout << "Stitching..." << endl;
string window_name = "视频拼接";
if(is_preview_)
namedWindow(window_name);
double show_scale = 1.0, scale_interval = 0.03;
int frame_show_interval = cvFloor(1000 / fps);
int failed_frame_count = 0;
char log_string[1000];
char log_file_name[200];
SYSTEMTIME sys_time = {0};
GetLocalTime(&sys_time);
sprintf(log_file_name, "%d%02d%02d-%02d%02d%02d.log",
sys_time.wYear, sys_time.wMonth, sys_time.wDay, sys_time.wHour, sys_time.wMinute, sys_time.wSecond);
ofstream log_file;
if(is_debug_)
log_file.open(debug_dir_path_ + log_file_name);
long long startTime = clock();
while(true)
{
long frame_time = 0;
// 采集
long cap_start_clock = clock();
int j;
for(j = 0; j < video_num; j++)
{
if( !captures[j].read(frame))
break;
frame.copyTo(frame_info.src[j]);
}
frame_info.frame_idx = frameidx;
frameidx++;
if(j != video_num || (end_frame_index_ >= 0 && frameidx >= end_frame_index_)) //有一个视频源结束,则停止拼接
break;
// 拼接
long stitch_start_clock = clock();
frame_info.stitch_status = StitchFrame(frame_info.src, frame_info.dst);
long stitch_clock = clock();
sprintf(log_string, "\tframe %d: stitch(%dms), capture(%dms)",
frame_info.frame_idx, stitch_clock - stitch_start_clock, stitch_start_clock - cap_start_clock);
printf("%s", log_string);
if(is_debug_)
log_file << log_string << endl;
stitch_time += stitch_clock - stitch_start_clock;
frame_time += stitch_clock - cap_start_clock;
// 拼接失败
if(frame_info.stitch_status != 0)
{
cout << "failed\n";
if(is_debug_)
log_file << "failed" << endl;
failed_frame_count++;
break;
}
// 保存视频
if(is_save_video_)
{
cout << ", write(";
if(is_save_output_frames)
{
char img_save_name[100];
sprintf(img_save_name, "/images/%d.jpg", frame_info.frame_idx);
imwrite(debug_dir_path_ + img_save_name, frame_info.dst);
}
long write_start_clock = clock();
writer.write(frame_info.dst);
long write_clock = clock();
cout << write_clock - write_start_clock << "ms)";
frame_time += write_clock - write_start_clock;
}
cout << endl;
// 显示---
if(is_preview_)
{
int key = waitKey(std::max(1, (int)(frame_show_interval - frame_time)));
if(key == 27) // ESC
break;
else if(key == 61 || key == 43) // +
show_scale += scale_interval;
else if(key == 45) // -
if(show_scale >= scale_interval)
show_scale -= scale_interval;
resize(frame_info.dst, show_dst, Size(show_scale * dst.cols, show_scale * dst.rows));
imshow(window_name, show_dst);
}
}
long long endTime = clock();
cout << "test " << endTime - startTime << endl;
cout << "\nStitch over" << endl;
cout << failed_frame_count << " frames failed." << endl;
cout << "\tfull view angle is " << cvRound(view_angle_) << "°" << endl;
if(is_debug_)
log_file << "\tfull view angle is " << cvRound(view_angle_) << "°" << endl;
writer.release();
cout << "\ton average: stitch time = " << stitch_time / (frameidx-1) << "ms" << endl;
cout << "\tcenter: (" << -dst_roi_.x << ", " << -dst_roi_.y << ")" << endl;
if(is_debug_)
{
log_file << "\ton average: stitch time = " << stitch_time / (frameidx-1) << "ms" << endl;
log_file << "\tcenter: (" << -dst_roi_.x << ", " << -dst_roi_.y << ")" << endl;
log_file.close();
}
return 0;
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/190798.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...