# HG changeset patch # User Nicolas Saunier # Date 1428577885 -7200 # Node ID 4e7925cb4f8f0ef87ed308593dc484e9f5582026 # Parent 852f5de42d013ab746c0cc28bf660e246a0e61ca modified tsai camera homography computation to avoid using os dependent temporary files diff -r 852f5de42d01 -r 4e7925cb4f8f python/cvutils.py --- a/python/cvutils.py Wed Apr 08 16:07:15 2015 +0200 +++ b/python/cvutils.py Thu Apr 09 13:11:25 2015 +0200 @@ -277,12 +277,11 @@ else: print 'Cannot load file ' + videoFilename - def computeHomographyFromPDTV(cameraFilename): - '''Returns the homography matrix at ground level from PDTV format + def computeHomographyFromPDTV(camera): + '''Returns the homography matrix at ground level from PDTV camera https://bitbucket.org/hakanardo/pdtv''' - import pdtv from numpy import array - camera = pdtv.load(cameraFilename) + # camera = pdtv.load(cameraFilename) srcPoints = [[x,y] for x, y in zip([1.,2.,2.,1.],[1.,1.,2.,2.])] # need floats!! dstPoints = [] for srcPoint in srcPoints: @@ -423,6 +422,7 @@ raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1])) if (homography is not None) and homography.size>0: + #alternatively, on could use cv2.convertpointstohomogeneous and other conversion to/from homogeneous coordinates augmentedPoints = append(points,[[1]*points.shape[1]], 0) prod = dot(homography, augmentedPoints) return prod[0:2]/prod[2] diff -r 852f5de42d01 -r 4e7925cb4f8f scripts/compute-homography.py --- a/scripts/compute-homography.py Wed Apr 08 16:07:15 2015 +0200 +++ b/scripts/compute-homography.py Thu Apr 09 13:11:25 2015 +0200 @@ -81,15 +81,16 @@ worldPts, videoPts = cvutils.loadPointCorrespondences(args.pointCorrespondencesFilename) homography, mask = cv2.findHomography(videoPts, worldPts) # method=0, ransacReprojThreshold=3 elif args.tsaiCameraFilename is not None: # hack using PDTV + from StringIO import StringIO + from pdtv import TsaiCamera + import yaml f = storage.openCheck(args.tsaiCameraFilename, quitting = True) content = storage.getLines(f) - outFilename = '/tmp/camera.yaml' - out = storage.openCheck(outFilename, 'w') - out.write('data_class: TsaiCamera\n') - for l in content: - out.write(l.replace(' f:', 'f:').replace(' k:', 'k:').replace(',','.')+'\n') - out.close() - homography = cvutils.computeHomographyFromPDTV(outFilename) + #out.write('data_class: TsaiCamera\n') + yamlContent = ''.join([l.replace(' f:', 'f:').replace(' k:', 'k:').replace(',','.')+'\n' for l in content]) + cameraData = yaml.load(StringIO(yamlContent)) + camera = TsaiCamera(Cx=cameraData['Cx'], Cy=cameraData['Cy'], Sx=cameraData['Sx'], Tx=cameraData['Tx'], Ty=cameraData['Ty'], Tz=cameraData['Tz'], dx=cameraData['dx'], dy=cameraData['dy'], f=cameraData['f'], k=cameraData['k'], r1=cameraData['r1'], r2=cameraData['r2'], r3=cameraData['r3'], r4=cameraData['r4'], r5=cameraData['r5'], r6=cameraData['r6'], r7=cameraData['r7'], r8=cameraData['r8'], r9=cameraData['r9']) + homography = cvutils.computeHomographyFromPDTV(camera) elif args.videoFrameFilename is not None and args.worldFilename is not None: worldImg = plt.imread(args.worldFilename) videoImg = plt.imread(args.videoFrameFilename)