use cv2 function

added color_histogram.py sample
work on VideoSynth (chessboard)
This commit is contained in:
Alexander Mordvintsev
2011-06-15 13:58:40 +00:00
parent 35aa133d9a
commit 1a208fe132
7 changed files with 138 additions and 68 deletions

View File

@@ -1,11 +1,10 @@
import numpy as np
import cv2
from time import clock
from numpy import pi, sin, cos
import common
def lookat(cam_pos, target_pos, up = ()):
dp = cam_pos - target_pos
class VideoSynth(object):
class VideoSynthBase(object):
def __init__(self, size=None, noise=0.0, bg = None, **params):
self.bg = None
self.frame_size = (640, 480)
@@ -17,19 +16,13 @@ class VideoSynth(object):
if size is not None:
w, h = map(int, size.split('x'))
self.frame_size = (w, h)
self.bg = cv2.resize(bg, self.frame_size)
self.bg = cv2.resize(self.bg, self.frame_size)
self.noise = float(noise)
w, h = self.frame_size
self.K = np.float64([[1.0/w, 0.0, 0.5*(w-1)],
[ 0.0, 1.0/w, 0.5*(h-1)],
[ 0.0, 0.0, 1.0]])
def draw_layers(self, dst):
def render(self, dst):
pass
def read(self, dst=None):
w, h = self.frame_size
@@ -38,7 +31,7 @@ class VideoSynth(object):
else:
buf = self.bg.copy()
self.draw_layers(buf)
self.render(buf)
if self.noise > 0.0:
noise = np.zeros((h, w, 3), np.int8)
@@ -46,6 +39,54 @@ class VideoSynth(object):
buf = cv2.add(buf, noise, dtype=cv2.CV_8UC3)
return True, buf
class Chess(VideoSynthBase):
def __init__(self, **kw):
super(Chess, self).__init__(**kw)
w, h = self.frame_size
self.grid_size = sx, sy = 10, 7
white_quads = []
black_quads = []
for i, j in np.ndindex(sy, sx):
q = [[j, i, 0], [j+1, i, 0], [j+1, i+1, 0], [j, i+1, 0]]
[white_quads, black_quads][(i + j) % 2].append(q)
self.white_quads = np.float32(white_quads)
self.black_quads = np.float32(black_quads)
fx = 0.9
self.K = np.float64([[fx*w, 0, 0.5*(w-1)],
[0, fx*w, 0.5*(h-1)],
[0.0,0.0, 1.0]])
self.dist_coef = np.float64([-0.2, 0.1, 0, 0])
def draw_quads(self, img, quads, color = (0, 255, 0)):
img_quads = cv2.projectPoints(quads.reshape(-1, 3), self.rvec, self.tvec, self.K, self.dist_coef) [0]
img_quads.shape = quads.shape[:2] + (2,)
for q in img_quads:
cv2.fillConvexPoly(img, np.int32(q*4), color, cv2.CV_AA, shift=2)
def render(self, dst):
t = clock()
sx, sy = self.grid_size
center = np.array([0.5*sx, 0.5*sy, 0.0])
phi = pi/3 + sin(t*3)*pi/8
c, s = cos(phi), sin(phi)
ofs = np.array([sin(1.2*t), cos(1.8*t), 0]) * sx * 0.2
eye_pos = center + np.array([cos(t)*c, sin(t)*c, s]) * 15.0 + ofs
target_pos = center + ofs
R, self.tvec = common.lookat(eye_pos, target_pos)
self.rvec = common.mtx2rvec(R)
self.draw_quads(dst, self.white_quads, (245, 245, 245))
self.draw_quads(dst, self.black_quads, (10, 10, 10))
classes = dict(chess=Chess)
def create_capture(source):
'''
@@ -59,13 +100,17 @@ def create_capture(source):
if source.startswith('synth'):
ss = filter(None, source.split(':'))
params = dict( s.split('=') for s in ss[1:] )
return VideoSynth(**params)
try: Class = classes[params['class']]
except: Class = VideoSynthBase
return Class(**params)
return cv2.VideoCapture(source)
presets = dict(
empty = 'synth:',
lena = 'synth:bg=../cpp/lena.jpg:noise=0.1'
lena = 'synth:bg=../cpp/lena.jpg:noise=0.1',
chess = 'synth:class=chess:bg=../cpp/lena.jpg:noise=0.1:size=640x480'
)
if __name__ == '__main__':
@@ -80,7 +125,7 @@ if __name__ == '__main__':
args = dict(args)
shotdir = args.get('--shotdir', '.')
if len(sources) == 0:
sources = [ presets['lena'] ]
sources = [ presets['chess'] ]
print 'Press SPACE to save current frame'