zoukankan      html  css  js  c++  java
  • video.py OpenCv例程阅读

      1 #!/usr/bin/env python
      2 
      3 '''
      4 Video capture sample.
      5 
      6 Sample shows how VideoCapture class can be used to acquire video
      7 frames from a camera of a movie file. Also the sample provides
      8 an example of procedural video generation by an object, mimicking
      9 the VideoCapture interface (see Chess class).
     10 
     11 'create_capture' is a convinience function for capture creation,
     12 falling back to procedural video in case of error.
     13 
     14 Usage:
     15     video.py [--shotdir <shot path>] [source0] [source1] ...'
     16 
     17     sourceN is an
     18      - integer number for camera capture
     19      - name of video file
     20      - synth:<params> for procedural video
     21 
     22 Synth examples:
     23     synth:bg=../cpp/lena.jpg:noise=0.1
     24     synth:class=chess:bg=../cpp/lena.jpg:noise=0.1:size=640x480
     25 
     26 Keys:
     27     ESC    - exit
     28     SPACE  - save current frame to <shot path> directory
     29 
     30 '''
     31 
     32 import numpy as np
     33 import cv2
     34 from time import clock
     35 from numpy import pi, sin, cos
     36 import common
     37 
     38 class VideoSynthBase(object):
     39     def __init__(self, size=None, noise=0.0, bg = None, **params):
     40         self.bg = None
     41         self.frame_size = (640, 480)
     42         if bg is not None:
     43             self.bg = cv2.imread(bg, 1)
     44             h, w = self.bg.shape[:2]
     45             self.frame_size = (w, h)
     46 
     47         if size is not None:
     48             w, h = map(int, size.split('x'))
     49             self.frame_size = (w, h)
     50             self.bg = cv2.resize(self.bg, self.frame_size)
     51 
     52         self.noise = float(noise)
     53 
     54     def render(self, dst):
     55         pass
     56 
     57     def read(self, dst=None):
     58         w, h = self.frame_size
     59 
     60         if self.bg is None:
     61             buf = np.zeros((h, w, 3), np.uint8)
     62         else:
     63             buf = self.bg.copy()
     64 
     65         self.render(buf)
     66 
     67         if self.noise > 0.0:
     68             noise = np.zeros((h, w, 3), np.int8)
     69             cv2.randn(noise, np.zeros(3), np.ones(3)*255*self.noise)
     70             buf = cv2.add(buf, noise, dtype=cv2.CV_8UC3)
     71         return True, buf
     72 
     73     def isOpened(self):
     74         return True
     75 
     76 class Chess(VideoSynthBase):
     77     def __init__(self, **kw):
     78         super(Chess, self).__init__(**kw)
     79 
     80         w, h = self.frame_size
     81 
     82         self.grid_size = sx, sy = 10, 7
     83         white_quads = []
     84         black_quads = []
     85         for i, j in np.ndindex(sy, sx):
     86             q = [[j, i, 0], [j+1, i, 0], [j+1, i+1, 0], [j, i+1, 0]]
     87             [white_quads, black_quads][(i + j) % 2].append(q)
     88         self.white_quads = np.float32(white_quads)
     89         self.black_quads = np.float32(black_quads)
     90 
     91         fx = 0.9
     92         self.K = np.float64([[fx*w, 0, 0.5*(w-1)],
     93                         [0, fx*w, 0.5*(h-1)],
     94                         [0.0,0.0,      1.0]])
     95 
     96         self.dist_coef = np.float64([-0.2, 0.1, 0, 0])
     97         self.t = 0
     98 
     99     def draw_quads(self, img, quads, color = (0, 255, 0)):
    100         img_quads = cv2.projectPoints(quads.reshape(-1, 3), self.rvec, self.tvec, self.K, self.dist_coef) [0]
    101         img_quads.shape = quads.shape[:2] + (2,)
    102         for q in img_quads:
    103             cv2.fillConvexPoly(img, np.int32(q*4), color, cv2.CV_AA, shift=2)
    104 
    105     def render(self, dst):
    106         t = self.t
    107         self.t += 1.0/30.0
    108 
    109         sx, sy = self.grid_size
    110         center = np.array([0.5*sx, 0.5*sy, 0.0])
    111         phi = pi/3 + sin(t*3)*pi/8
    112         c, s = cos(phi), sin(phi)
    113         ofs = np.array([sin(1.2*t), cos(1.8*t), 0]) * sx * 0.2
    114         eye_pos = center + np.array([cos(t)*c, sin(t)*c, s]) * 15.0 + ofs
    115         target_pos = center + ofs
    116 
    117         R, self.tvec = common.lookat(eye_pos, target_pos)
    118         self.rvec = common.mtx2rvec(R)
    119 
    120         self.draw_quads(dst, self.white_quads, (245, 245, 245))
    121         self.draw_quads(dst, self.black_quads, (10, 10, 10))
    122 
    123 
    124 classes = dict(chess=Chess)
    125 
    126 presets = dict(
    127     empty = 'synth:',
    128     lena = 'synth:bg=../cpp/lena.jpg:noise=0.1',
    129     chess = 'synth:class=chess:bg=../cpp/lena.jpg:noise=0.1:size=640x480'
    130 )
    131 
    132 
    133 def create_capture(source = 0, fallback = presets['chess']):
    134     '''source: <int> or '<int>|<filename>|synth [:<param_name>=<value> [:...]]'
    135     '''
    136     source = str(source).strip()
    137     chunks = source.split(':')
    138     # hanlde drive letter ('c:', ...)
    139     if len(chunks) > 1 and len(chunks[0]) == 1 and chunks[0].isalpha():
    140         chunks[1] = chunks[0] + ':' + chunks[1]
    141         del chunks[0]
    142 
    143     source = chunks[0]
    144     try: source = int(source)
    145     except ValueError: pass
    146     params = dict( s.split('=') for s in chunks[1:] )
    147 
    148     cap = None
    149     if source == 'synth':
    150         Class = classes.get(params.get('class', None), VideoSynthBase)
    151         try: cap = Class(**params)
    152         except: pass
    153     else:
    154         cap = cv2.VideoCapture(source)
    155         if 'size' in params:
    156             w, h = map(int, params['size'].split('x'))
    157             cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, w)
    158             cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, h)
    159     if cap is None or not cap.isOpened():
    160         print 'Warning: unable to open video source: ', source
    161         if fallback is not None:
    162             return create_capture(fallback, None)
    163     return cap
    164 
    165 if __name__ == '__main__':
    166     import sys
    167     import getopt
    168 
    169     print __doc__
    170 
    171     args, sources = getopt.getopt(sys.argv[1:], '', 'shotdir=')
    172     args = dict(args)
    173     shotdir = args.get('--shotdir', '.')
    174     if len(sources) == 0:
    175         sources = [ 0 ]
    176 
    177     caps = map(create_capture, sources)
    178     shot_idx = 0
    179     while True:
    180         imgs = []
    181         for i, cap in enumerate(caps):
    182             ret, img = cap.read()
    183             imgs.append(img)
    184             cv2.imshow('capture %d' % i, img)
    185         ch = 0xFF & cv2.waitKey(1)
    186         if ch == 27:
    187             break
    188         if ch == ord(' '):
    189             for i, img in enumerate(imgs):
    190                 fn = '%s/shot_%d_%03d.bmp' % (shotdir, i, shot_idx)
    191                 cv2.imwrite(fn, img)
    192                 print fn, 'saved'
    193             shot_idx += 1
    194     cv2.destroyAllWindows()
    View Code

    第133行:create_capture(source = 0, fallback = presets['chess'])        有两个参数 source 用于指示在哪里获取视频源。fallback ------------

        

      声明:s为字符串,rm为要删除的字符序列

      s.strip(rm)        删除s字符串中开头、结尾处,位于 rm删除序列的字符

      s.lstrip(rm)       删除s字符串中开头处,位于 rm删除序列的字符

      s.rstrip(rm)      删除s字符串中结尾处,位于 rm删除序列的字符

      当rm为空时,默认删除空白符(包括' ', ' ',  ' ',  ' ')

    总结 开启数据采集设备并返回 控制句柄。

     

  • 相关阅读:
    【C#/WPF】限制GridSplitter分隔栏的滑动范围
    【C#】访问泛型中的List列表数据
    【C#学习笔记】反射的简单用法
    【C#】获取泛型<T>的真实类型
    【Unity】关于发射子弹、导弹追踪的逻辑
    【转】【Unity】四元数(Quaternion)和旋转
    【Unity】UGUI的Text各种小问题
    【火狐FireFox】同步失败后,书签被覆盖,如何恢复书签
    【转】【Unity】实现全局管理类的几种方式
    【Unity】动态调用其他脚本的函数
  • 原文地址:https://www.cnblogs.com/A-FM/p/6640969.html
Copyright © 2011-2022 走看看