将从摄像头即时读入的人像放入背景视频中_with_OpenCV_in_Python
import cv2
import numpy as np
import time
cap = cv2.VideoCapture(0)
background_capture = cv2.VideoCapture(r'./a.avi')
counter = -1
while cap.isOpened():
counter += 1
start_time_extract_figure = time.time()
# your code
# extract your figure
_, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
mask = np.zeros(frame.shape[:2], np.uint8)
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)
rect = (50, 50, 450, 490)
start_time_t = time.time()
cv2.grabCut(frame, mask, rect, bgdModel, fgdModel, 1, cv2.GC_INIT_WITH_RECT)
during_time = time.time() - start_time_t
print('{}-th t_time: {}'.format(counter, during_time))
mask2 = np.where((mask == 2) | (mask == 0), (0,), (1,)).astype('uint8')
frame = frame * mask2[:, :, np.newaxis]
elapsed_time_extract_figure = time.time() - start_time_extract_figure
print('{}-th extract_figure_time: {}'.format(counter, elapsed_time_extract_figure))
# extract the background
start_time_combination = time.time()
# your code
ret, background = background_capture.read()
background = cv2.resize(background, (640, 480), interpolation=cv2.INTER_AREA)
# maybe the default size of embedded camera is 640x480
# combine the figure and background using mask instead of iteration
mask_1 = frame > 0
mask_2 = frame <= 0
combination = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) * mask_1 + background * mask_2
elapsed_time_combination = time.time() - start_time_combination
print('{}-th combination_time: {}'.format(counter, elapsed_time_combination))
cv2.imshow('combination', combination)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
发现影响速度的最主要因素是grabCut函数, 因此附官网grabCut详细说明https://docs.opencv.org/trunk/d8/d83/tutorial_py_grabcut.html供以参考.
- img - Input image
- mask - It is a mask image where we specify which areas are background, foreground or probable background/foreground etc. It is done by the following flags, cv2.GC_BGD, cv2.GC_FGD, cv2.GC_PR_BGD, cv2.GC_PR_FGD, or simply pass 0,1,2,3 to image.
- rect - It is the coordinates of a rectangle which includes the foreground object in the format (x,y,w,h)
- bdgModel, fgdModel - These are arrays used by the algorithm internally. You just create two np.float64 type zero arrays of size (1,65).
- iterCount - Number of iterations the algorithm should run.
- mode - It should be cv2.GC_INIT_WITH_RECT or cv2.GC_INIT_WITH_MASK or combined which decides whether we are drawing rectangle or final touchup strokes.
将从摄像头即时读入的人像放入背景视频中_with_OpenCV_in_Python的更多相关文章
- 将摄像头的读入的人像放入背景视频中_with_OpenCV_in_Python
import cv2 import numpy as np import time cap = cv2.VideoCapture(0) background_capture = cv2.VideoCa ...
- NX二次开发-UFUN将实体放入STL文件中函数UF_STD_put_solid_in_stl_file
NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <u ...
- C语言:将3*4矩阵中找出行最大,列最小的那个元素。-将低于平均值的人数作为函数返回值,将低于平均分的分数放入below数组中。
//将3*4矩阵中找出行最大,列最小的那个元素. #include <stdio.h> #define M 3 #define N 4 void fun(int (*a)[N]) { ,j ...
- 将DLL放入到资源中,运行时自动加载
今天在看到 一个小软件,考勤用的 AttendanceSheet_V_1_2,只有一个EXE文件,绿色的随便考到哪里都可以运行. 顺手反编译后发现,他将需要的DLL也放入到资源文件了,在启动的时候自动 ...
- 【转】【Android测试技巧】01. root后adb shell默认不是root用户时,如何将文件放入手机系统中
http://blog.csdn.net/wirelessqa/article/details/8624208 有些机器root后通过adb shell 后,默认不是root用户,需要输入 su才能切 ...
- JAVA ArrayList实现随机生成数字,并把偶数放入一个列表中
package Code429; import java.util.ArrayList;import java.util.Random; public class CodeArrayListPrint ...
- 两个list<Map<String,String>>放入一个list中
// public static void main(String[] args) {// List<Map<String, String>> list2 = new Arra ...
- 字体文件放入CDN服务器中,跨域问题(IIS版)
Font from origin 'http:/XXXX' has been blocked from loading by Cross-Origin Resource Sharing policy: ...
- php文件以二进制形式上传并放入到数据库中
conn.php: <?php $id=mysql_connect('localhost','root','root'); mysql_select_db("db_database12 ...
随机推荐
- 阿里云RDS物理备份恢复到本地
一:业务场景 验证阿里云备份文件可用性 二:恢复到本地过程中遇到的问题 1.修改密码报错 2.自定义函数不可用 三:恢复步骤 1.xtrabackup安装使用 请参考:https://www.cnbl ...
- 基于scrapy框架的分布式爬虫
分布式 概念:可以使用多台电脑组件一个分布式机群,让其执行同一组程序,对同一组网络资源进行联合爬取. 原生的scrapy是无法实现分布式 调度器无法被共享 管道无法被共享 基于 scrapy+redi ...
- dotnet cli 5.0 新特性——dotnet tool search
dotnet cli 5.0 新特性--dotnet tool search Intro .NET 5.0 SDK 的发布,给 dotnet cli 引入了一个新的特性,dotnet tool sea ...
- 阿里 Mock 工具正式开源,干掉市面上所有 Mock 工具!
最近栈长注意到阿里开源了自家的 Mock 工具:TestableMock,该工具号称最轻量.简单.舒适的 Mock 测试工具,功能十分强大,媲美 PowerMock,用法比 Mockito 还要简洁, ...
- 转 Jmeter测试实践:文件下载接口
Jmeter测试实践:文件下载接口 一 Jmeter步骤 1.打开jmeter4.0,新建测试计划,添加线程组.根据实际情况配置线程属性. 2.添加HTTP请求.根据接口文档进行配置. Basic ...
- spring源码分析之玩转ioc:bean初始化和依赖注入(一)
最近赶项目,天天加班到十一二点,终于把文档和代码都整完了,接上继续整. 上一篇聊了beanProcess的注册以及对bean的自定义修改和添加,也标志着创建bean的准备工作都做好了,接下来就是开大招 ...
- 虚拟化kvm的搭建
虚拟化, 是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机 ,在一台计算机上同时运行多个逻辑计算机,每台逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立的空间内运行而互不 ...
- Bitter.Core系列七:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 示例 更新删除插入
Bitter Orm 在操作数据库增删改的时候,支持模型驱动和直接执行裸SQL 操作,示例代码如下: 一:模型驱动(增删改) /// <summary> /// 插入,删除,更新示例(模型 ...
- The Garbage Collection Handbook
The Garbage Collection Handbook The Garbage Collection Handbook http://gchandbook.org/editions.html ...
- TypeScript基本类型
类型注解 作用:相当于强类型语言中的类型声明 语法:(变量/函数):type 数据类型 新建src/datatype.ts,里面定义各种类型的数据 原始类型: let bool: boolean = ...