From 619e503d53497f5f11bdbe8ba103837769b211b3 Mon Sep 17 00:00:00 2001 From: Alexander Mordvintsev Date: Mon, 13 Jun 2011 21:45:18 +0000 Subject: [PATCH] calibrate.py added (broken) work on video synth --- samples/python2/calibrate.py | 63 ++++++++++++++++++++++++++++++++++++++++++++ samples/python2/video.py | 16 +++++++++++ 2 files changed, 79 insertions(+) create mode 100644 samples/python2/calibrate.py diff --git a/samples/python2/calibrate.py b/samples/python2/calibrate.py new file mode 100644 index 0000000..e72170d --- /dev/null +++ b/samples/python2/calibrate.py @@ -0,0 +1,63 @@ +import numpy as np +import cv2, cv +import os + +USAGE = ''' +USAGE: calib.py [--save ] [--debug ] [] +''' + +class Bunch: + def __init__(self, **kwds): + self.__dict__.update(kwds) + + +def splitfn(fn): + path, fn = os.path.split(fn) + name, ext = os.path.splitext(fn) + return path, name, ext + + +if __name__ == '__main__': + import sys, getopt + from glob import glob + + args, img_mask = getopt.getopt(sys.argv[1:], '', ['save=', 'debug=']) + args = dict(args) + 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 + + pattern_size = (9, 6) + pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 ) + pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2) + + obj_points = [] + img_points = [] + h, w = 0, 0 + for fn in img_names: + print 'processing %s...' % fn, + img = cv2.imread(fn, 0) + h, w = img.shape[:2] + found, corners = cv2.findChessboardCorners(img, pattern_size) + if found: + term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 ) + cv2.cornerSubPix(img, corners, (11, 11), (-1, -1), term) + if debug_dir: + vis = cv2.cvtColor(img, cv.CV_GRAY2BGR) + cv2.drawChessboardCorners(vis, pattern_size, corners, found) + path, name, ext = splitfn(fn) + cv2.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis) + if not found: + print 'chessboard not found' + break + img_points.append(corners.reshape(-1, 2)) + obj_points.append(pattern_points) + + print 'ok' + + camera_matrix = np.zeros((3, 3)) + dist_coefs = np.zeros(4) + rms = cv2.calibrateCamera(obj_points, img_points, (w, h), camera_matrix, dist_coefs) + print rms \ No newline at end of file diff --git a/samples/python2/video.py b/samples/python2/video.py index b681f03..232f3ae 100644 --- a/samples/python2/video.py +++ b/samples/python2/video.py @@ -1,5 +1,9 @@ import numpy as np import cv2 +from time import clock + +def lookat(cam_pos, target_pos, up = ()): + dp = cam_pos - target_pos class VideoSynth(object): def __init__(self, size=None, noise=0.0, bg = None, **params): @@ -17,6 +21,15 @@ class VideoSynth(object): 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): + pass + + def read(self, dst=None): w, h = self.frame_size @@ -24,6 +37,9 @@ class VideoSynth(object): buf = np.zeros((h, w, 3), np.uint8) else: buf = self.bg.copy() + + self.draw_layers(buf) + if self.noise > 0.0: noise = np.zeros((h, w, 3), np.int8) cv2.randn(noise, np.zeros(3), np.ones(3)*255*self.noise) -- 2.7.4