From 194506397eb30cc79edc94a15085d35d312233f1 Mon Sep 17 00:00:00 2001 From: Alexander Mordvintsev Date: Tue, 14 Jun 2011 15:02:57 +0000 Subject: [PATCH] python helper routines (common.py) added --- samples/python2/calibrate.py | 3 +- samples/python2/common.py | 72 ++++++++++++++++++++++++++++++++++++ samples/python2/video.py | 1 + 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 samples/python2/common.py diff --git a/samples/python2/calibrate.py b/samples/python2/calibrate.py index f334ba261a..cc35535450 100644 --- a/samples/python2/calibrate.py +++ b/samples/python2/calibrate.py @@ -26,8 +26,7 @@ if __name__ == '__main__': try: img_mask = img_mask[0] except: img_mask = '../cpp/left*.jpg' img_names = glob(img_mask) - try: debug_dir = args['--debug'] - except: debug_dir = None + debug_dir = args.get('--debug') pattern_size = (9, 6) pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 ) diff --git a/samples/python2/common.py b/samples/python2/common.py new file mode 100644 index 0000000000..27e1b340e7 --- /dev/null +++ b/samples/python2/common.py @@ -0,0 +1,72 @@ +import numpy as np +import cv2 + +def to_list(a): + return [tuple(p) for p in a] + +def anorm2(a): + return (a*a).sum(-1) +def anorm(a): + return np.sqrt( anorm2(a) ) + +def homotrans(H, x, y): + xs = H[0, 0]*x + H[0, 1]*y + H[0, 2] + ys = H[1, 0]*x + H[1, 1]*y + H[1, 2] + s = H[2, 0]*x + H[2, 1]*y + H[2, 2] + return xs/s, ys/s + +def to_rect(a): + a = np.ravel(a) + if len(a) == 2: + a = (0, 0, a[0], a[1]) + return np.array(a, np.float64).reshape(2, 2) + +def rect2rect_mtx(src, dst): + src, dst = to_rect(src), to_rect(dst) + cx, cy = (dst[1] - dst[0]) / (src[1] - src[0]) + tx, ty = dst[0] - src[0] * (cx, cy) + M = np.float64([[ cx, 0, tx], + [ 0, cy, ty], + [ 0, 0, 1]]) + return M + + +def lookat(eye, target, up = (0, 0, 1)): + fwd = np.asarray(target, np.float64) - eye + fwd /= anorm(fwd) + right = np.cross(fwd, up) + right /= anorm(right) + down = np.cross(fwd, right) + Rt = np.zeros((3, 4)) + Rt[:,:3] = [right, down, fwd] + Rt[:,3] = -np.dot(Rt[:,:3], eye) + return Rt + +def mtx2rvec(R): + pass + + +if __name__ == '__main__': + import cv2 + from time import clock + + ''' + w, h = 640, 480 + while True: + img = np.zeros((h, w, 3), np.uint8) + t = clock() + eye = [5*cos(t), 5*sin(t), 3] + Rt = lookat(eye, [0, 0, 0]) + ''' + + + + eye = [1, -4, 3] + target = [0, 0, 0] + Rt = lookat(eye, [0, 0, 0]) + print Rt + p = [0, 0, 0] + print cv2.transform(np.float64([[p]]), Rt) + + print cv2.SVDecomp(Rt[:,:3]) + diff --git a/samples/python2/video.py b/samples/python2/video.py index 232f3aee74..a87d9771f9 100644 --- a/samples/python2/video.py +++ b/samples/python2/video.py @@ -64,6 +64,7 @@ def create_capture(source): presets = dict( + empty = 'synth:', lena = 'synth:bg=../cpp/lena.jpg:noise=0.1' )