Mercurial Hosting > traffic-intelligence
annotate python/cvutils.py @ 110:fae55a4c7a5a
added functions to modify and copy subsets of trajectory files
author | Nicolas Saunier <nico@confins.net> |
---|---|
date | Fri, 15 Jul 2011 15:49:32 -0400 |
parents | abfc54c097d4 |
children | 67555e968b5e |
rev | line source |
---|---|
28 | 1 #! /usr/bin/env python |
2 '''Image/Video utilities''' | |
3 | |
4 import Image, ImageDraw # PIL | |
99
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
5 import cv |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
6 from sys import stdout |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
7 |
44
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
8 #import aggdraw # agg on top of PIL (antialiased drawing) |
28 | 9 #import utils |
10 | |
11 __metaclass__ = type | |
12 | |
13 def drawLines(filename, origins, destinations, w = 1, resultFilename='image.png'): | |
14 '''Draws lines over the image ''' | |
15 | |
16 img = Image.open(filename) | |
17 | |
18 draw = ImageDraw.Draw(img) | |
19 #draw = aggdraw.Draw(img) | |
20 #pen = aggdraw.Pen("red", width) | |
21 for p1, p2 in zip(origins, destinations): | |
22 draw.line([p1.x, p1.y, p2.x, p2.y], width = w, fill = (256,0,0)) | |
23 #draw.line([p1.x, p1.y, p2.x, p2.y], pen) | |
24 del draw | |
25 | |
26 #out = utils.openCheck(resultFilename) | |
27 img.save(resultFilename) | |
28 | |
99
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
29 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
|
30 '''Returns the homography matrix mapping from srcPoints to dstPoints (dimension Nx2)''' |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
31 cvSrcPoints = arrayToCvMat(srcPoints); |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
32 cvDstPoints = arrayToCvMat(dstPoints); |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
33 H = cv.CreateMat(3, 3, cv.CV_64FC1) |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
34 cv.FindHomography(cvSrcPoints, cvDstPoints, H, method, ransacReprojThreshold) |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
35 return H |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
36 |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
37 def cvMatToArray(cvmat): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
38 '''Converts an OpenCV CvMat to numpy array.''' |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
39 from numpy.core.multiarray import zeros |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
40 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
|
41 for i in xrange(cvmat.rows): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
42 for j in xrange(cvmat.cols): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
43 a[i,j] = cvmat[i,j] |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
44 return a |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
45 |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
46 def arrayToCvMat(a, t = cv.CV_64FC1): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
47 '''Converts a numpy array to an OpenCV CvMat, with default type CV_64FC1.''' |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
48 cvmat = cv.CreateMat(a.shape[0], a.shape[1], t) |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
49 for i in range(cvmat.rows): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
50 for j in range(cvmat.cols): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
51 cvmat[i,j] = a[i,j] |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
52 return cvmat |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
53 |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
54 def printCvMat(cvmat, out = stdout): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
55 '''Prints the cvmat to out''' |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
56 for i in xrange(cvmat.rows): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
57 for j in xrange(cvmat.cols): |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
58 out.write('{0} '.format(cvmat[i,j])) |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
59 out.write('\n') |
e7dc5a780f09
added conversion functions and homography computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
98
diff
changeset
|
60 |
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
61 def projectArray(homography, points): |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
62 '''Returns the coordinates of the projected points (format 2xN points) |
28 | 63 through homography''' |
64 from numpy.core._dotblas import dot | |
65 from numpy.core.multiarray import array | |
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
66 from numpy.lib.function_base import append |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
67 |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
68 if points.shape[0] != 2: |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
69 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
|
70 |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
71 if (homography!=None) and homography.size>0: |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
72 augmentedPoints = append(points,[[1]*points.shape[1]], 0) |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
73 prod = dot(homography, augmentedPoints) |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
74 return prod[0:2]/prod[2] |
28 | 75 else: |
76 return p | |
77 | |
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
78 def project(homography, p): |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
79 '''Returns the coordinates of the projection of the point p |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
80 through homography''' |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
81 from numpy.core.multiarray import array |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
82 return projectArray(homography, array([[p[0]],p[1]])) |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
83 |
28 | 84 def projectTrajectory(homography, trajectory): |
85 '''Projects a series of points in the format | |
86 [[x1, x2, ...], | |
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
87 [y1, y2, ...]]''' |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
88 from numpy.core.multiarray import array |
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
89 return projectArray(homography, array(trajectory)) |
28 | 90 |
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
|
91 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
|
92 '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
|
93 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
|
94 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
|
95 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
|
96 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
|
97 |
100
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
98 def computeTranslation(img1, img2, img1Points, maxTranslation, minNMatches, windowSize = (5,5), level = 5, criteria = (cv.CV_TERMCRIT_EPS, 0, 0.01)): |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
99 '''Computes the translation between of img2 with respect to img1 |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
100 (loaded using OpenCV) |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
101 img1Points are used to compute the translation |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
102 |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
103 TODO add diagnostic if data is all over the place, and it most likely is not a translation (eg zoom)''' |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
104 from numpy.core.multiarray import zeros |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
105 from numpy.lib.function_base import median |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
106 |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
107 (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) |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
108 |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
109 deltaX = [] |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
110 deltaY = [] |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
111 for (k, (p1,p2)) in enumerate(zip(img1Points, img2Points)): |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
112 if status[k] == 1: |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
113 dx = p2[0]-p1[0] |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
114 dy = p2[1]-p1[1] |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
115 d = dx**2 + dy**2 |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
116 if d < maxTranslation: |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
117 deltaX.append(dx) |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
118 deltaY.append(dy) |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
119 if len(deltaX) >= 10: |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
120 return [median(deltaX), median(deltaY)] |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
121 else: |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
122 return None |
2a3cafcf5faf
added function to compute the translation between two images
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
99
diff
changeset
|
123 |
60
1d36a676c745
minor class name change
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
124 class FourWayIntersection: |
44
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
125 '''Simple class for simple intersection outline''' |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
126 def __init__(self, dimension, coordX, coordY): |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
127 self.dimension = dimension |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
128 self.coordX = coordX |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
129 self.coordY = coordY |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
130 |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
131 def plot(self, options = 'k'): |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
132 from matplotlib.pyplot import plot, axis |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
133 |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
134 minX = min(self.dimension[0]) |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
135 maxX = max(self.dimension[0]) |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
136 minY = min(self.dimension[1]) |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
137 maxY = max(self.dimension[1]) |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
138 |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
139 plot([minX, self.coordX[0], self.coordX[0]], [self.coordY[0], self.coordY[0], minY],options) |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
140 plot([self.coordX[1], self.coordX[1], maxX], [minY, self.coordY[0], self.coordY[0]],options) |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
141 plot([minX, self.coordX[0], self.coordX[0]], [self.coordY[1], self.coordY[1], maxY],options) |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
142 plot([self.coordX[1], self.coordX[1], maxX], [maxY, self.coordY[1], self.coordY[1]],options) |
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
143 axis('equal') |