calibrate.py added (broken)

work on video synth
This commit is contained in:
Alexander Mordvintsev 2011-06-13 21:45:18 +00:00
parent 6f26c55fe3
commit 619e503d53
2 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,63 @@
import numpy as np
import cv2, cv
import os
USAGE = '''
USAGE: calib.py [--save <filename>] [--debug <output path>] [<image mask>]
'''
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

View File

@ -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)