annotate python/calibration-translation.py @ 160:b0719b3ad3db

created function to load point correspondences and updates scripts that use it
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 19 Sep 2011 16:43:28 -0400
parents 115f7f90286d
children 514f6b98cd8c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
1 #!/usr/bin/env python
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
2
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
3 import sys
159
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
4 import os
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
5
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
6 import matplotlib.mlab as pylab
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
7 import matplotlib.pyplot as plt
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
8 import numpy as np
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
9
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
10 import cv2
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
11 import utils
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
12 import cvutils
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
13
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
14 # development for the data collected and stabilized by Paul in Summer 2011
160
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 159
diff changeset
15 # todo write help, add options to control the parameters for matching (n points and distance)
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
16
158
2d7c6d767a39 corrected and improved calibration-translation.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 157
diff changeset
17 options = utils.parseCLIOptions('Program to re-calibrate an initial calibration based on point correspondences by adjusting the points to slightly different viewpoints, where all the points are still visible\n\nUsage: ', ['ref_video=', 'ref_points='], sys.argv, ['mask_img='])
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
18
160
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 159
diff changeset
19 referenceVideoFilename=options['--ref_video']
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 159
diff changeset
20 wldPts, imgPts = cvutils.loadPointCorrespondences(options['--ref_points'])
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
21
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
22 def translatePoints(points, t):
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
23 'points is Nx2, t is [x,y]'
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
24 translated = points.copy()
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
25 for i in xrange(2):
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
26 translated[i] += t[i]
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
27 return translated
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
28
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
29 filenames = [f for f in utils.listfiles('.','avi')] # directory to examine should be current directory
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
30
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
31 referenceVideoIndex = filenames.index(referenceVideoFilename)
159
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
32 indices = set(range(len(filenames)))
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
33 indices.discard(referenceVideoIndex)
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
34
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
35 images = {}
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
36 captures = {}
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
37
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
38 captures[referenceVideoFilename] = cv2.VideoCapture(referenceVideoFilename)
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
39 (ret, img) = captures[referenceVideoFilename].read()
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
40 images[referenceVideoFilename] = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
41
160
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 159
diff changeset
42 # load a mask image to compute the translation
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
43 if '--mask_img' in options.keys():
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
44 maskImg = cv2.imread('mask.png', cv2.CV_LOAD_IMAGE_GRAYSCALE) # todo add possibility to look in the whole image if not providing mask
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
45 else:
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
46 maskImg = np.ones(images[referenceVideoFilename].shape, dtype=np.uint8)
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
47
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
48 referenceFeatures = cv2.goodFeaturesToTrack(images[referenceVideoFilename], 1000, 0.02, 2, useHarrisDetector = True, mask=maskImg)
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
49 displayRef = cv2.cvtColor(images[referenceVideoFilename], cv2.COLOR_GRAY2RGB)
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
50 for j,p in enumerate(imgPts):
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
51 cv2.circle(displayRef, tuple(p), 3, (255,0,0))
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
52 cv2.putText(displayRef, str(j+1), tuple(p), cv2.FONT_HERSHEY_PLAIN, 1, (255,0,0))
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
53 cv2.imshow('Reference',displayRef)
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
54
160
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 159
diff changeset
55 # get suitable image references for each video
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 159
diff changeset
56 for f in filenames:
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
57 captures[f] = cv2.VideoCapture(f)
159
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
58 frameFilename = utils.removeExtension(f)+'-frame.png' # TODO if frame image already exists, no need to search for it again
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
59 if not os.path.exists(frameFilename):
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
60 key = -1
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
61 while key != cvutils.cvKeyNumbers['y']:
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
62 (ret, img) = captures[f].read()
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
63 cv2.imshow('Image',img)
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
64 print('Can one see the reference points in the image? (y/n)')
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
65 key = cv2.waitKey(0)
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
66
159
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
67 images[f] = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
68 cv2.imwrite(frameFilename, img)
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
69 else:
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
70 images[f] = cv2.imread(frameFilename, cv2.CV_LOAD_IMAGE_GRAYSCALE)
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
71 #features[f] = cv2.goodFeaturesToTrack(images[f], 1000, 0.02, 2, useHarrisDetector = True, mask=maskImg) # todo put parameters on the command line ?
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
72 # goodFeaturesToTrack(image, maxCorners, qualityLevel, minDistance[, corners[, mask[, blockSize[, useHarrisDetector[, k]]]]])
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
73 # display features
159
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
74 # if False:
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
75 # display = img.copy()#cv2.cvtColor(images[f], cv2.COLOR_GRAY2RGB) #.copy()
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
76 # for p in features[f]:
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
77 # cv2.circle(display, tuple(p[0]), 3, (255,0,0))
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
78 # cv2.imshow('Reference',display)
115f7f90286d updated calibration-translation and added function to convert point correspondences
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 158
diff changeset
79 # cv2.waitKey()
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
80
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
81 plt.close('all')
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
82
160
b0719b3ad3db created function to load point correspondences and updates scripts that use it
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 159
diff changeset
83 # validate or input point correspondences and compute homography
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
84 for i in indices:
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
85 t = cvutils.computeTranslation(images[filenames[referenceVideoIndex]], images[filenames[i]], referenceFeatures, 100, 10)
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
86 print filenames[i],t
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
87 key = -1
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
88 if t != None: # show translated points and ask if ok
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
89 displayImg = cv2.cvtColor(images[filenames[i]], cv2.COLOR_GRAY2RGB) #.copy()
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
90 for p in imgPts:
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
91 cv2.circle(displayImg, tuple(p+t[0]), 3, (255,0,0))
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
92 cv2.imshow('Image',displayImg)
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
93
158
2d7c6d767a39 corrected and improved calibration-translation.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 157
diff changeset
94 while not(key == cvutils.cvKeyNumbers['y'] or key == cvutils.cvKeyNumbers['n']):
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
95 print('Are the translated points rightly located (y/n)?')
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
96 key = cv2.waitKey(0)
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
97 if key == cvutils.cvKeyNumbers['y']: # compute homography with translated numbers
158
2d7c6d767a39 corrected and improved calibration-translation.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 157
diff changeset
98 newImgPts = np.array([p+t[0] for p in imgPts])
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
99 else:
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
100 print('No translation could be found automatically. You will have to manually input world reference points.')
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
101
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
102 if t==None or key != cvutils.cvKeyNumbers['y']:# if no translation could computed or it is not satisfactory
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
103 print('Select the corresponding points in the same order as in the reference image')
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
104 plt.figure(1)
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
105 plt.imshow(displayRef)
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
106 plt.figure(2)
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
107 plt.imshow(img)
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
108 plt.show()
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
109 newImgPts = np.array([list(p) for p in plt.ginput(n=wldPts.shape[0], timeout=-1)], dtype = np.float32)
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
110
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
111 homography, mask = cv2.findHomography(newImgPts, wldPts) # method=0, ransacReprojThreshold=3
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
112 print homography
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
113 np.savetxt(utils.removeExtension(filenames[i])+'-homography.txt',homography)
158
2d7c6d767a39 corrected and improved calibration-translation.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 157
diff changeset
114 np.savetxt(utils.removeExtension(filenames[i])+'-point-correspondences.txt', np.append(wldPts.T, newImgPts.T, axis=0))
157
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
115
3aab19947a34 added utility to recalibrate images with similar viewpoints
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
116 cv2.destroyAllWindows()