将摄像头的读入的人像放入背景视频中_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 ...
随机推荐
- 201521123069 《Java程序设计》 第2周学习总结
1. 本章学习总结 (1)String类.StringBuilder类(频繁进行字符串的修改应选用StringBuilder,不会生成大量的字符串对象).Math类的用法.字符串池的概念 (2)Sca ...
- 201521123115《java程序设计》第十一周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) 1. ...
- 跨站数据请求哪家强——青出于蓝的jsonp
/* * 跨站数据请求哪家强--青出于蓝的jsonp数据格式 * @author gj * @date 2017-09-15 */ 先哲有云"青,取之于蓝,而青于蓝:冰,水为之,而寒于水&q ...
- 搭建连接MySql的三层架构的ASP.NetCore2.0的WebApi
里我们用三层架构搭建一个连接MySql的ASP.netCore模板的WebApi项目 首先添加WebApi项目(ASP.NetCore版本) 右键解决方案>新建项目> 选择Web>A ...
- Struts2开山篇【引入Struts、自定义MyStruts框架】
前言 这是Strtus的开山篇,主要是引入struts框架-为什么要引入struts,引入struts的好处是什么-. 为什么要引入struts? 首先,在讲解struts之前,我们来看看我们以前写的 ...
- Java Sftp上传下载文件
需要使用jar包 jsch-0.1.50.jar sftp上传下载实现类 package com.bstek.transit.sftp; import java.io.File; import ja ...
- Linux中组 与 用户的管理
在linux中建立组的指令是 groupadd 组名 相应的,删除组的指令: groupdel 组名 查看自己用户的组: groups 一个用户可以在多个组里面,用这个命令可以将用户添加到组: add ...
- 异常处理第一讲(SEH),筛选器异常,以及__asm的扩展,寄存器注入简介
异常处理第一讲(SSH),筛选器异常,以及__asm的扩展 博客园IBinary原创 博客连接:http://www.cnblogs.com/iBinary/ 转载请注明出处,谢谢 一丶__Asm的 ...
- javascript 单元测试初入门
1.使用mocha工具实现单元测试 ①首先准备node环境 ②安装mocha:npm install mocha 也可以进行全局安装 npm install global mocha ③安装断言库:n ...
- GCD之全局、主线程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 -(NSString *)fetchData { [NSThread sleepFo ...