comparison python/compute-homography.py @ 238:be3761a09b20

added functions to input point correspondences
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 09 Jul 2012 00:46:04 -0400
parents 6774bdce03f1
children 9d88a4d97473
comparison
equal deleted inserted replaced
237:6774bdce03f1 238:be3761a09b20
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 2
3 import sys,getopt 3 import sys,getopt
4 4
5 import matplotlib.pyplot as plt
5 import numpy as np 6 import numpy as np
6 import cv2 7 import cv2
7 8
8 import cvutils 9 import cvutils
9 import utils 10 import utils
10 11
11 options, args = getopt.getopt(sys.argv[1:], 'h',['help','video_frame=']) 12 options, args = getopt.getopt(sys.argv[1:], 'hp:i:w:n:u:',['help'])
12 options = dict(options) 13 options = dict(options)
13 14
14 # TODO process camera intrinsic and extrinsic parameters to obtain image to world homography, taking example from Work/src/python/generate-homography.py script 15 # TODO process camera intrinsic and extrinsic parameters to obtain image to world homography, taking example from Work/src/python/generate-homography.py script
15 # cameraMat = load(videoFilenamePrefix+'-camera.txt'); 16 # cameraMat = load(videoFilenamePrefix+'-camera.txt');
16 # T1 = cameraMat[3:6,:].copy(); 17 # T1 = cameraMat[3:6,:].copy();
49 50
50 # H = cvCreateMat(3, 3, CV_64FC1); 51 # H = cvCreateMat(3, 3, CV_64FC1);
51 52
52 # cvFindHomography(imagePoints, worldPoints, H); 53 # cvFindHomography(imagePoints, worldPoints, H);
53 54
55 if '--help' in options.keys() or '-h' in options.keys() or len(options) == 0:
56 print('Usage: {0} --help|-h [-p point-correspondences.txt] [ -i video-frame] [ -w world-frame] [n number-points] [-u unit-per-pixel=1]'.format(sys.argv[0]))
57 print('''The input data can be provided either as point correspondences already saved
58 in a text file or inputed by clicking a certain number of points (>=4)
59 in a video frame and a world image.
54 60
55 if '--help' in options.keys() or '-h' in options.keys() or len(args) == 0: 61 The point correspondence file contains at least 4 non-colinear point coordinates
56 print('Usage: {0} --help|-h [--video_frame <video frame filename>] [<point_correspondences.txt>]'.format(sys.argv[0])) 62 with the following format:
57 print('''The positional argument should be the name
58 of a file containing at least 4 non-colinear point coordinates (point correspondences:
59 - the first two lines are the x and y coordinates in the projected space (usually world space) 63 - the first two lines are the x and y coordinates in the projected space (usually world space)
60 - the last two lines are the x and y coordinates in the origin space (usually image space) 64 - the last two lines are the x and y coordinates in the origin space (usually image space)
61 65
62 if providing a video frame, the image points and back projected world points will be plotted''') 66 If providing video and world images, with a number of points to input
67 and a ration to convert pixels to world distance unit (eg meters per pixel),
68 the images will be shown in turn and the user should click
69 in the same order the corresponding points in world and image spaces. ''')
63 sys.exit() 70 sys.exit()
64 71
65 dstPts, srcPts = cvutils.loadPointCorrespondences(args[0]) 72 homography = np.array([])
66 homography, mask = cv2.findHomography(srcPts, dstPts) # method=0, ransacReprojThreshold=3 73 if '-p' in options.keys():
67 np.savetxt(utils.removeExtension(sys.argv[1])+'-homography.txt',homography) 74 worldPts, videoPts = cvutils.loadPointCorrespondences(args[0])
75 homography, mask = cv2.findHomography(videoPts, worldPts) # method=0, ransacReprojThreshold=3
76 elif '-i' in options.keys() and '-w' in options.keys():
77 nPoints = 4
78 if '-n' in options.keys():
79 nPoints = int(options['-n'])
80 unitsPerPixel = 1
81 if '-u' in options.keys():
82 unitsPerPixel = float(options['-u'])
83 worldImg = plt.imread(options['-w'])
84 videoImg = plt.imread(options['-i'])
85 print('Click on {0} points in the video frame'.format(nPoints))
86 plt.figure()
87 plt.imshow(videoImg)
88 videoPts = np.array(plt.ginput(nPoints))
89 print('Click on {0} points in the world image'.format(nPoints))
90 plt.figure()
91 plt.imshow(worldImg)
92 worldPts = unitsPerPixel*np.array(plt.ginput(nPoints))
93 plt.close('all')
94 homography, mask = cv2.findHomography(videoPts, worldPts)
95 # save the points in file
96 f = open('point-correspondences.txt', 'a')
97 np.savetxt(f, worldPts.T)
98 np.savetxt(f, videoPts.T)
99 f.close()
68 100
69 if '--video_frame' in options.keys() and homography.size>0: 101 if homography.size>0:
70 img = cv2.imread(options['--video_frame']) 102 np.savetxt('homography.txt',homography)
71 for p in srcPts: 103
72 cv2.circle(img,tuple(p),2,cvutils.cvRed) 104 if '-i' in options.keys() and homography.size>0:
105 videoImg = cv2.imread(options['-i'])
106 worldImg = cv2.imread(options['-w'])
73 invHomography = np.linalg.inv(homography) 107 invHomography = np.linalg.inv(homography)
74 projectedDstPts = cvutils.projectArray(invHomography, dstPts.T).T 108 projectedWorldPts = cvutils.projectArray(invHomography, worldPts.T).T
75 for i,p in enumerate(projectedDstPts): 109 if '-u' in options.keys():
76 cv2.circle(img,tuple(np.int32(np.round(p))),2,cvutils.cvBlue) 110 unitsPerPixel = float(options['-u'])
77 print('img: {0} / projected: {1}'.format(srcPts[i], p)) 111 projectedVideoPts = cvutils.projectArray(invHomography, videoPts.T).T
78 cv2.imshow('video frame',img) 112 for i in range(worldPts.shape[0]):
113 cv2.circle(videoImg,tuple(np.int32(np.round(videoPts[i]))),2,cvutils.cvRed)
114 cv2.circle(videoImg,tuple(np.int32(np.round(projectedWorldPts[i]))),2,cvutils.cvBlue)
115 if '-u' in options.keys():
116 cv2.circle(worldImg,tuple(np.int32(np.round(worldPts[i]/unitsPerPixel))),2,cvutils.cvRed)
117 cv2.circle(worldImg,tuple(np.int32(np.round(projectedVideoPts[i]/unitsPerPixel))),2,cvutils.cvRed)
118 #print('img: {0} / projected: {1}'.format(videoPts[i], p))
119 cv2.imshow('video frame',videoImg)
120 if '-u' in options.keys():
121 cv2.imshow('world image',worldImg)
79 cv2.waitKey() 122 cv2.waitKey()