comparison python/calibration-translation.py @ 157:3aab19947a34

added utility to recalibrate images with similar viewpoints
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 08 Sep 2011 19:25:02 -0400
parents
children 2d7c6d767a39
comparison
equal deleted inserted replaced
156:2eef5620c0b3 157:3aab19947a34
1 #!/usr/bin/env python
2
3 import sys
4
5 import matplotlib.mlab as pylab
6 import matplotlib.pyplot as plt
7 import numpy as np
8
9 import cv2
10 import utils
11 import cvutils
12
13 # development for the data collected and stabilized by Paul in Summer 2011
14 # todo test other features
15
16 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_homography=', 'ref_points='], sys.argv, ['mask_img='])
17
18 referenceVideoFilename=options['--ref_video']#'1440-1459_Mercalli.avi'
19 referenceHomographyFilename=options['--ref_homography']#'1440-1459_Mercalli-homography.txt'
20 points = np.loadtxt(options['--ref_points'], dtype=np.float32) # '1440-1459_Mercalli-point-correspondences.txt'
21 wldPts = points[:2,:].T
22 imgPts = points[2:,:].T
23
24 def translatePoints(points, t):
25 'points is Nx2, t is [x,y]'
26 translated = points.copy()
27 for i in xrange(2):
28 translated[i] += t[i]
29 return translated
30
31 filenames = [f for f in utils.listfiles('.','avi')] # directory to examine should be current directory
32
33 referenceHomography = np.loadtxt(referenceHomographyFilename)
34 referenceVideoIndex = filenames.index(referenceVideoFilename)
35 indices = set(range(len(filenames)))
36 indices.discard(referenceVideoIndex)
37
38 images = {}
39 #features = {}
40 captures = {}
41
42 captures[referenceVideoFilename] = cv2.VideoCapture(referenceVideoFilename)
43 (ret, img) = captures[referenceVideoFilename].read()
44 images[referenceVideoFilename] = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
45
46 if '--mask_img' in options.keys():
47 maskImg = cv2.imread('mask.png', cv2.CV_LOAD_IMAGE_GRAYSCALE) # todo add possibility to look in the whole image if not providing mask
48 else:
49 maskImg = np.ones(images[referenceVideoFilename].shape, dtype=np.uint8)
50
51 referenceFeatures = cv2.goodFeaturesToTrack(images[referenceVideoFilename], 1000, 0.02, 2, useHarrisDetector = True, mask=maskImg)
52 displayRef = cv2.cvtColor(images[referenceVideoFilename], cv2.COLOR_GRAY2RGB)
53 for j,p in enumerate(imgPts):
54 cv2.circle(displayRef, tuple(p), 3, (255,0,0))
55 cv2.putText(displayRef, str(j+1), tuple(p), cv2.FONT_HERSHEY_PLAIN, 1, (255,0,0))
56 cv2.imshow('Reference',displayRef)
57
58 key = -1
59 for f in filenames: # get suitable image references for each video
60 captures[f] = cv2.VideoCapture(f)
61 while key != cvutils.cvKeyNumbers['y']:
62 (ret, img) = captures[f].read()
63 cv2.imshow('Image',img)
64 print('Can one see the reference points in the image? (y/n)')
65 key = cv2.waitKey(0)
66
67 images[f] = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
68 cv2.imwrite(utils.removeExtension(filenames[i])+'-frame.png')
69 #images[f] = cv2.imread(f, cv2.CV_LOAD_IMAGE_GRAYSCALE)
70 #features[f] = cv2.goodFeaturesToTrack(images[f], 1000, 0.02, 2, useHarrisDetector = True, mask=maskImg) # todo put parameters on the command line ?
71 # goodFeaturesToTrack(image, maxCorners, qualityLevel, minDistance[, corners[, mask[, blockSize[, useHarrisDetector[, k]]]]])
72 # display features
73 if False:
74 display = img.copy()#cv2.cvtColor(images[f], cv2.COLOR_GRAY2RGB) #.copy()
75 for p in features[f]:
76 cv2.circle(display, tuple(p[0]), 3, (255,0,0))
77 cv2.imshow('Reference',display)
78 cv2.waitKey()
79
80 plt.close('all')
81
82 for i in indices:
83 t = cvutils.computeTranslation(images[filenames[referenceVideoIndex]], images[filenames[i]], referenceFeatures, 100, 10)
84 print filenames[i],t
85 key = -1
86 if t != None: # show translated points and ask if ok
87 displayImg = cv2.cvtColor(images[filenames[i]], cv2.COLOR_GRAY2RGB) #.copy()
88 for p in imgPts:
89 cv2.circle(displayImg, tuple(p+t[0]), 3, (255,0,0))
90 cv2.imshow('Image',displayImg)
91
92 while key != cvutils.cvKeyNumbers['y'] and key != cvutils.cvKeyNumbers['n']:
93 print('Are the translated points rightly located (y/n)?')
94 key = cv2.waitKey(0)
95 if key == cvutils.cvKeyNumbers['y']: # compute homography with translated numbers
96 newImgPts = [p+t[0] for p in imgPts]
97 else:
98 print('No translation could be found automatically. You will have to manually input world reference points.')
99
100 if t==None or key != cvutils.cvKeyNumbers['y']:# if no translation could computed or it is not satisfactory
101 # image should be right to get points
102 # todo save image
103 print('Select the corresponding points in the same order as in the reference image')
104 plt.figure(1)
105 plt.imshow(displayRef)
106 plt.figure(2)
107 plt.imshow(img)
108 plt.show()
109 newImgPts = np.array([list(p) for p in plt.ginput(n=wldPts.shape[0], timeout=-1)], dtype = np.float32)
110
111 homography, mask = cv2.findHomography(newImgPts, wldPts) # method=0, ransacReprojThreshold=3
112 print homography
113 np.savetxt(utils.removeExtension(filenames[i])+'-homography.txt',homography)
114 np.savetxt(utils.removeExtension(filenames[i])+'-point-correspondences.txt', append(wldPts.T, newImgPts.T, axis=0))
115
116 cv2.destroyAllWindows()