APAP代码(1)「建议收藏」

APAP代码(1)「建议收藏」staticintVideoStitch(intargc,char*argv[]){ for(inti=0;i<argc;i++) printf(“%s\n”,argv[i]); intretval=parseCmdArgs(argc,argv); /*if(retval) returnretval;*/ is_camera=false; for(inti=0;i<video_names.size();i++) .

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新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;
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/190798.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 自考法律

    自考法律

    2021年10月2日
    38
  • container html css,splitcontainer「建议收藏」

    container html css,splitcontainer「建议收藏」怎么使用SplitContainer控件实现上下分隔您好,我来为您解C#如何使用SplitContainer控件实现上下分隔Orientation属性设置为Horizontal希望我的回答对你有帮助。c#splitContainer能把窗体分割成三部分或者更多…splitContainer能把窗体分割成三部分或者更多的部分吗?怎么操作?splitContainer控件单独使用只支…

    2022年7月18日
    14
  • js生成二维码原理_二维码生成器原理

    js生成二维码原理_二维码生成器原理1.引用相关的js文件:        jquery.qrcode.js,qrcode.js    2.js代码如下:           3.html页面主要部分:       以上就可以生成自己的二维码了,手机扫描就可以跳转到指定的页面或者显示内容。   相关的代码在这:http://download.csdn.net/detail/go_walkin

    2022年10月17日
    2
  • win7 64位官方旗舰版上搭建ruby on rails的步骤

    win7 64位官方旗舰版上搭建ruby on rails的步骤今天在win7上安装ruby成功,步骤记录下来了,分享给朋友们。亲测,可行。———-第一步:安装ruby————1.安装rubyinstaller-2.2.4-x64.exe,记得勾选addpath…选项,安装完之后ruby-v查看版本号,比如ruby2.2.4p230(2015-12-16revision53155)[x64-mi

    2022年6月5日
    29
  • spring-boot2.0 Mybatis多数据源配置

    spring-boot2.0 Mybatis多数据源配置

    2021年6月6日
    96
  • 关于迁移学习「建议收藏」

    作者:刘诗昆链接:https://www.zhihu.com/question/41979241/answer/123545914来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。2018.1.二次更新/重点介绍迁移学习和其相关方向,完整删除之前的答案,并重新规整了迁移学习内容和代表性文章。什么是/为什么要迁移学习?迁移学习(T

    2022年4月17日
    62

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号