1. import cv2
  2. import numpy as np
  3. import time
  4. cap = cv2.VideoCapture(0)
  5. background_capture = cv2.VideoCapture(r'./a.avi')
  6. counter = -1
  7. while cap.isOpened():
  8. counter += 1
  9. start_time_extract_figure = time.time()
  10. # your code
  11. # extract your figure
  12. _, frame = cap.read()
  13. frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  14. mask = np.zeros(frame.shape[:2], np.uint8)
  15. bgdModel = np.zeros((1, 65), np.float64)
  16. fgdModel = np.zeros((1, 65), np.float64)
  17. rect = (50, 50, 450, 490)
  18. start_time_t = time.time()
  19. cv2.grabCut(frame, mask, rect, bgdModel, fgdModel, 1, cv2.GC_INIT_WITH_RECT)
  20. during_time = time.time() - start_time_t
  21. print('{}-th t_time: {}'.format(counter, during_time))
  22. mask2 = np.where((mask == 2) | (mask == 0), (0,), (1,)).astype('uint8')
  23. frame = frame * mask2[:, :, np.newaxis]
  24. elapsed_time_extract_figure = time.time() - start_time_extract_figure
  25. print('{}-th extract_figure_time: {}'.format(counter, elapsed_time_extract_figure))
  26. # extract the background
  27. start_time_combination = time.time()
  28. # your code
  29. ret, background = background_capture.read()
  30. background = cv2.resize(background, (640, 480), interpolation=cv2.INTER_AREA)
  31. # maybe the default size of embedded camera is 640x480
  32. # combine the figure and background using mask instead of iteration
  33. mask_1 = frame > 0
  34. mask_2 = frame <= 0
  35. combination = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) * mask_1 + background * mask_2
  36. elapsed_time_combination = time.time() - start_time_combination
  37. print('{}-th combination_time: {}'.format(counter, elapsed_time_combination))
  38. cv2.imshow('combination', combination)
  39. k = cv2.waitKey(30) & 0xff
  40. if k == 27:
  41. break
  42. cap.release()
  43. 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的更多相关文章

  1. 将摄像头的读入的人像放入背景视频中_with_OpenCV_in_Python

    import cv2 import numpy as np import time cap = cv2.VideoCapture(0) background_capture = cv2.VideoCa ...

  2. 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 ...

  3. C语言:将3*4矩阵中找出行最大,列最小的那个元素。-将低于平均值的人数作为函数返回值,将低于平均分的分数放入below数组中。

    //将3*4矩阵中找出行最大,列最小的那个元素. #include <stdio.h> #define M 3 #define N 4 void fun(int (*a)[N]) { ,j ...

  4. 将DLL放入到资源中,运行时自动加载

    今天在看到 一个小软件,考勤用的 AttendanceSheet_V_1_2,只有一个EXE文件,绿色的随便考到哪里都可以运行. 顺手反编译后发现,他将需要的DLL也放入到资源文件了,在启动的时候自动 ...

  5. 【转】【Android测试技巧】01. root后adb shell默认不是root用户时,如何将文件放入手机系统中

    http://blog.csdn.net/wirelessqa/article/details/8624208 有些机器root后通过adb shell 后,默认不是root用户,需要输入 su才能切 ...

  6. JAVA ArrayList实现随机生成数字,并把偶数放入一个列表中

    package Code429; import java.util.ArrayList;import java.util.Random; public class CodeArrayListPrint ...

  7. 两个list<Map<String,String>>放入一个list中

    // public static void main(String[] args) {// List<Map<String, String>> list2 = new Arra ...

  8. 字体文件放入CDN服务器中,跨域问题(IIS版)

    Font from origin 'http:/XXXX' has been blocked from loading by Cross-Origin Resource Sharing policy: ...

  9. php文件以二进制形式上传并放入到数据库中

    conn.php: <?php $id=mysql_connect('localhost','root','root'); mysql_select_db("db_database12 ...

随机推荐

  1. ESPNet/ESPNetV2:空洞卷积金字塔 | 轻量级网络

    ESPNet系列的核心在于空洞卷积金字塔,每层具有不同的dilation rate,在参数量不增加的情况下,能够融合多尺度特征,相对于深度可分离卷积,深度可分离空洞卷积金字塔性价比更高.另外,HFF的 ...

  2. SELECT ... FOR UPDATE or SELECT ... FOR SHARE Locking Reads session

    小结: 1.注意使用限制 Locking reads are only possible when autocommit is disabled (either by beginning transa ...

  3. Most basic operations in Go are not synchronized. In other words, they are not concurrency-safe.

    Most basic operations in Go are not synchronized. In other words, they are not concurrency-safe. htt ...

  4. SumatraPDF设置护眼背景

    高级选项中: 1 FixedPageUI [ 2 TextColor = #000000 3 BackgroundColor = #C7EDCC 4 SelectionColor = #f5fc0c ...

  5. C语言中使用类似awk的功能

    awk实在是太强大了, 非常复杂的任务,几行代码就可以解决, awk经常需要用到, 但是在c语言里面, 调用system不太优雅, 能不能直接在c语言中直接调用呢,简单实现了一些功能, 但大多数情况, ...

  6. 洛谷P1858

    \({\mathcal{For}}\) \({\mathcal{we}\ }\)\({\mathcal{live}\ }\)\({\mathcal{by}\ }\)\({\mathcal{faith} ...

  7. 改造xxl-job的客户端日志文件生成体系

    为什么要改造XXL-JOB原有的日志文件生成体系   xxl-job原本自己的客户端日志文件生成策略是:一个日志记录就生成一个文件,也就是当数据库存在一条日志logId,对应的客户端就会生成一个文件, ...

  8. Spark调优 | Spark Streaming 调优

    Spark调优 | Spark Streaming 调优 1.数据序列化 2.广播大变量 3.数据处理和接收时的并行度 4.设置合理的批处理间隔 5.内存优化 5.1 内存管理 5.2优化策略 5.3 ...

  9. Spring,Spring MVC,MyBatis,Hibernate总结

    将之前学习的框架知识进行了UML图总结,若有错误或不当之处,劳烦朋友们指正,会及时作出修改和补充: [toc] Spring Spring MVC MyBatis,Hibernate

  10. HashMap源码(JDK1.8)-手动注释

    HashMap简介 HashMap是一种K-V映射的一种数据结构,通过K(key)值能实现在O(1)的时间复杂度下找到对应的V(value).JDK1.8之前,HashMap的底层数据结构是数组+链表 ...