annotate python/cvutils.py @ 151:4af774bb186d

wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 06 Sep 2011 17:55:06 -0400
parents 404f3cade05f
children 74b1fc68d4df
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
1 #! /usr/bin/env python
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
2 '''Image/Video utilities'''
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
3
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
4 import Image, ImageDraw # PIL
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
5 try:
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
6 import cv2
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
7 opencvExists = True
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
8 except ImportError:
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
9 print('OpenCV library could not be loaded')
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
10 opencvExists = False
99
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
11 from sys import stdout
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
12
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
13 import utils
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
14
44
be3ae926e4e8 added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 28
diff changeset
15 #import aggdraw # agg on top of PIL (antialiased drawing)
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
16 #import utils
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
17
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
18 __metaclass__ = type
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
19
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
20 cvRed = (0,0,255)
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
21 cvGreen = (0,255,0)
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
22 cvBlue = (255,0,0)
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
23 cvColors = utils.PlottingPropertyValues([cvRed,
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
24 cvGreen,
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
25 cvBlue])
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
26
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
27 def drawLines(filename, origins, destinations, w = 1, resultFilename='image.png'):
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
28 '''Draws lines over the image '''
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
29
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
30 img = Image.open(filename)
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
31
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
32 draw = ImageDraw.Draw(img)
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
33 #draw = aggdraw.Draw(img)
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
34 #pen = aggdraw.Pen("red", width)
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
35 for p1, p2 in zip(origins, destinations):
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
36 draw.line([p1.x, p1.y, p2.x, p2.y], width = w, fill = (256,0,0))
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
37 #draw.line([p1.x, p1.y, p2.x, p2.y], pen)
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
38 del draw
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
39
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
40 #out = utils.openCheck(resultFilename)
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
41 img.save(resultFilename)
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
42
99
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
43 def computeHomography(srcPoints, dstPoints, method=0, ransacReprojThreshold=0.0):
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
44 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)'''
151
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
45 #cvSrcPoints = arrayToCvMat(srcPoints);
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
46 #cvDstPoints = arrayToCvMat(dstPoints);
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
47 #H = cv.CreateMat(3, 3, cv.CV_64FC1)
4af774bb186d wrote a simple script to compute homography from point correspondences and display the reprojection for visual verification
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 150
diff changeset
48 H, mask = cv2.findHomography(srcPoints, dstPoints, method, ransacReprojThreshold)
99
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
49 return H
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
50
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
51 def cvMatToArray(cvmat):
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
52 '''Converts an OpenCV CvMat to numpy array.'''
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
53 from numpy.core.multiarray import zeros
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
54 a = zeros((cvmat.rows, cvmat.cols))#array([[0.0]*cvmat.width]*cvmat.height)
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
55 for i in xrange(cvmat.rows):
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
56 for j in xrange(cvmat.cols):
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
57 a[i,j] = cvmat[i,j]
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
58 return a
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
59
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
60 if opencvExists:
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
61 def arrayToCvMat(a, t = cv.CV_64FC1):
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
62 '''Converts a numpy array to an OpenCV CvMat, with default type CV_64FC1.'''
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
63 cvmat = cv.CreateMat(a.shape[0], a.shape[1], t)
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
64 for i in range(cvmat.rows):
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
65 for j in range(cvmat.cols):
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
66 cvmat[i,j] = a[i,j]
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
67 return cvmat
99
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
68
149
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
69 def playVideo(filename):
150
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
70 '''Plays the video'''
149
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
71 capture = cv2.VideoCapture(filename)
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
72 if capture.isOpened():
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
73 key = -1
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
74 while key!= 1048689: # 'q'
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
75 ret, img = capture.read()
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
76 if ret:
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
77 cv2.imshow('frame', img)
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
78 key = cv2.waitKey(5)
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
79
150
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
80 def getImagesFromVideo(filename, nImages = 1):
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
81 '''Returns nImages images from the video sequence'''
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
82 images = []
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
83 capture = cv2.VideoCapture(filename)
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
84 if capture.isOpened():
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
85 ret = False
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
86 while len(images)<nImages:
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
87 while not ret:
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
88 ret, img = capture.read()
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
89 if img.size>0:
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
90 images.append(img)
404f3cade05f added python function to get image frames from video filenames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 149
diff changeset
91 return images
149
0f552c8b1650 added python function to play video
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 116
diff changeset
92
99
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
93 def printCvMat(cvmat, out = stdout):
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
94 '''Prints the cvmat to out'''
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
95 for i in xrange(cvmat.rows):
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
96 for j in xrange(cvmat.cols):
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
97 out.write('{0} '.format(cvmat[i,j]))
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
98 out.write('\n')
e7dc5a780f09 added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 98
diff changeset
99
98
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
100 def projectArray(homography, points):
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
101 '''Returns the coordinates of the projected points (format 2xN points)
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
102 through homography'''
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
103 from numpy.core._dotblas import dot
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
104 from numpy.core.multiarray import array
98
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
105 from numpy.lib.function_base import append
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
106
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
107 if points.shape[0] != 2:
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
108 raise Exception('points of dimension {0} {1}'.format(points.shape[0], points.shape[1]))
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
109
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
110 if (homography!=None) and homography.size>0:
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
111 augmentedPoints = append(points,[[1]*points.shape[1]], 0)
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
112 prod = dot(homography, augmentedPoints)
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
113 return prod[0:2]/prod[2]
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
114 else:
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
115 return p
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
116
98
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
117 def project(homography, p):
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
118 '''Returns the coordinates of the projection of the point p
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
119 through homography'''
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
120 from numpy.core.multiarray import array
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
121 return projectArray(homography, array([[p[0]],p[1]]))
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
122
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
123 def projectTrajectory(homography, trajectory):
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
124 '''Projects a series of points in the format
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
125 [[x1, x2, ...],
98
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
126 [y1, y2, ...]]'''
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
127 from numpy.core.multiarray import array
b85912ab4064 refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 63
diff changeset
128 return projectArray(homography, array(trajectory))
28
9ae709a2e8d0 rearranged code
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
129
48
8aed225f71d8 rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 44
diff changeset
130 def invertHomography(homography):
8aed225f71d8 rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 44
diff changeset
131 'Returns an inverted homography'
8aed225f71d8 rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 44
diff changeset
132 from numpy.linalg.linalg import inv
8aed225f71d8 rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 44
diff changeset
133 invH = inv(homography)
8aed225f71d8 rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 44
diff changeset
134 invH /= invH[2,2]
8aed225f71d8 rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 44
diff changeset
135 return invH
8aed225f71d8 rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 44
diff changeset
136
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
137 if opencvExists:
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
138 def computeTranslation(img1, img2, img1Points, maxTranslation, minNMatches, windowSize = (5,5), level = 5, criteria = (cv.CV_TERMCRIT_EPS, 0, 0.01)):
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
139 '''Computes the translation between of img2 with respect to img1
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
140 (loaded using OpenCV)
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
141 img1Points are used to compute the translation
100
2a3cafcf5faf added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 99
diff changeset
142
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
143 TODO add diagnostic if data is all over the place, and it most likely is not a translation (eg zoom)'''
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
144 from numpy.core.multiarray import zeros
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
145 from numpy.lib.function_base import median
100
2a3cafcf5faf added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 99
diff changeset
146
112
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
147 (img2Points, status, track_error) = cv.CalcOpticalFlowPyrLK(img1, img2, zeros((img1.rows,img1.cols+8)), zeros((img1.rows,img1.cols+8)), img1Points, windowSize, level, criteria, 0)
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
148
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
149 deltaX = []
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
150 deltaY = []
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
151 for (k, (p1,p2)) in enumerate(zip(img1Points, img2Points)):
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
152 if status[k] == 1:
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
153 dx = p2[0]-p1[0]
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
154 dy = p2[1]-p1[1]
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
155 d = dx**2 + dy**2
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
156 if d < maxTranslation:
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
157 deltaX.append(dx)
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
158 deltaY.append(dy)
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
159 if len(deltaX) >= 10:
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
160 return [median(deltaX), median(deltaY)]
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
161 else:
67555e968b5e added tests if OpenCV python libraries are not available
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 102
diff changeset
162 return None