Mercurial > hg > nsaunier > traffic-intelligence
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 159:115f7f90286d | 160:b0719b3ad3db |
|---|---|
| 10 import cv2 | 10 import cv2 |
| 11 import utils | 11 import utils |
| 12 import cvutils | 12 import cvutils |
| 13 | 13 |
| 14 # development for the data collected and stabilized by Paul in Summer 2011 | 14 # development for the data collected and stabilized by Paul in Summer 2011 |
| 15 # todo test other features | 15 # todo write help, add options to control the parameters for matching (n points and distance) |
| 16 | 16 |
| 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=']) | 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=']) |
| 18 #, 'ref_homography=' | |
| 19 | 18 |
| 20 referenceVideoFilename=options['--ref_video']#'1440-1459_Mercalli.avi' | 19 referenceVideoFilename=options['--ref_video'] |
| 21 #referenceHomographyFilename=options['--ref_homography']#'1440-1459_Mercalli-homography.txt' | 20 wldPts, imgPts = cvutils.loadPointCorrespondences(options['--ref_points']) |
| 22 points = np.loadtxt(options['--ref_points'], dtype=np.float32) # '1440-1459_Mercalli-point-correspondences.txt' | |
| 23 wldPts = points[:2,:].T | |
| 24 imgPts = points[2:,:].T | |
| 25 | 21 |
| 26 def translatePoints(points, t): | 22 def translatePoints(points, t): |
| 27 'points is Nx2, t is [x,y]' | 23 'points is Nx2, t is [x,y]' |
| 28 translated = points.copy() | 24 translated = points.copy() |
| 29 for i in xrange(2): | 25 for i in xrange(2): |
| 30 translated[i] += t[i] | 26 translated[i] += t[i] |
| 31 return translated | 27 return translated |
| 32 | 28 |
| 33 filenames = [f for f in utils.listfiles('.','avi')] # directory to examine should be current directory | 29 filenames = [f for f in utils.listfiles('.','avi')] # directory to examine should be current directory |
| 34 | 30 |
| 35 #referenceHomography = np.loadtxt(referenceHomographyFilename) | |
| 36 referenceVideoIndex = filenames.index(referenceVideoFilename) | 31 referenceVideoIndex = filenames.index(referenceVideoFilename) |
| 37 indices = set(range(len(filenames))) | 32 indices = set(range(len(filenames))) |
| 38 indices.discard(referenceVideoIndex) | 33 indices.discard(referenceVideoIndex) |
| 39 | 34 |
| 40 images = {} | 35 images = {} |
| 41 #features = {} | |
| 42 captures = {} | 36 captures = {} |
| 43 | 37 |
| 44 captures[referenceVideoFilename] = cv2.VideoCapture(referenceVideoFilename) | 38 captures[referenceVideoFilename] = cv2.VideoCapture(referenceVideoFilename) |
| 45 (ret, img) = captures[referenceVideoFilename].read() | 39 (ret, img) = captures[referenceVideoFilename].read() |
| 46 images[referenceVideoFilename] = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | 40 images[referenceVideoFilename] = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) |
| 47 | 41 |
| 42 # load a mask image to compute the translation | |
| 48 if '--mask_img' in options.keys(): | 43 if '--mask_img' in options.keys(): |
| 49 maskImg = cv2.imread('mask.png', cv2.CV_LOAD_IMAGE_GRAYSCALE) # todo add possibility to look in the whole image if not providing mask | 44 maskImg = cv2.imread('mask.png', cv2.CV_LOAD_IMAGE_GRAYSCALE) # todo add possibility to look in the whole image if not providing mask |
| 50 else: | 45 else: |
| 51 maskImg = np.ones(images[referenceVideoFilename].shape, dtype=np.uint8) | 46 maskImg = np.ones(images[referenceVideoFilename].shape, dtype=np.uint8) |
| 52 | 47 |
| 55 for j,p in enumerate(imgPts): | 50 for j,p in enumerate(imgPts): |
| 56 cv2.circle(displayRef, tuple(p), 3, (255,0,0)) | 51 cv2.circle(displayRef, tuple(p), 3, (255,0,0)) |
| 57 cv2.putText(displayRef, str(j+1), tuple(p), cv2.FONT_HERSHEY_PLAIN, 1, (255,0,0)) | 52 cv2.putText(displayRef, str(j+1), tuple(p), cv2.FONT_HERSHEY_PLAIN, 1, (255,0,0)) |
| 58 cv2.imshow('Reference',displayRef) | 53 cv2.imshow('Reference',displayRef) |
| 59 | 54 |
| 60 for f in filenames: # get suitable image references for each video | 55 # get suitable image references for each video |
| 56 for f in filenames: | |
| 61 captures[f] = cv2.VideoCapture(f) | 57 captures[f] = cv2.VideoCapture(f) |
| 62 frameFilename = utils.removeExtension(f)+'-frame.png' # TODO if frame image already exists, no need to search for it again | 58 frameFilename = utils.removeExtension(f)+'-frame.png' # TODO if frame image already exists, no need to search for it again |
| 63 if not os.path.exists(frameFilename): | 59 if not os.path.exists(frameFilename): |
| 64 key = -1 | 60 key = -1 |
| 65 while key != cvutils.cvKeyNumbers['y']: | 61 while key != cvutils.cvKeyNumbers['y']: |
| 82 # cv2.imshow('Reference',display) | 78 # cv2.imshow('Reference',display) |
| 83 # cv2.waitKey() | 79 # cv2.waitKey() |
| 84 | 80 |
| 85 plt.close('all') | 81 plt.close('all') |
| 86 | 82 |
| 83 # validate or input point correspondences and compute homography | |
| 87 for i in indices: | 84 for i in indices: |
| 88 t = cvutils.computeTranslation(images[filenames[referenceVideoIndex]], images[filenames[i]], referenceFeatures, 100, 10) | 85 t = cvutils.computeTranslation(images[filenames[referenceVideoIndex]], images[filenames[i]], referenceFeatures, 100, 10) |
| 89 print filenames[i],t | 86 print filenames[i],t |
| 90 key = -1 | 87 key = -1 |
| 91 if t != None: # show translated points and ask if ok | 88 if t != None: # show translated points and ask if ok |
| 101 newImgPts = np.array([p+t[0] for p in imgPts]) | 98 newImgPts = np.array([p+t[0] for p in imgPts]) |
| 102 else: | 99 else: |
| 103 print('No translation could be found automatically. You will have to manually input world reference points.') | 100 print('No translation could be found automatically. You will have to manually input world reference points.') |
| 104 | 101 |
| 105 if t==None or key != cvutils.cvKeyNumbers['y']:# if no translation could computed or it is not satisfactory | 102 if t==None or key != cvutils.cvKeyNumbers['y']:# if no translation could computed or it is not satisfactory |
| 106 # image should be right to get points | |
| 107 # todo save image | |
| 108 print('Select the corresponding points in the same order as in the reference image') | 103 print('Select the corresponding points in the same order as in the reference image') |
| 109 plt.figure(1) | 104 plt.figure(1) |
| 110 plt.imshow(displayRef) | 105 plt.imshow(displayRef) |
| 111 plt.figure(2) | 106 plt.figure(2) |
| 112 plt.imshow(img) | 107 plt.imshow(img) |
