Mercurial > hg > nsaunier > traffic-intelligence
annotate python/cvutils.py @ 98:b85912ab4064
refactored projection functions
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 12 Jul 2011 16:43:33 -0400 |
| parents | 40e8e3bb3702 |
| children | e7dc5a780f09 |
| rev | line source |
|---|---|
| 28 | 1 #! /usr/bin/env python |
| 2 '''Image/Video utilities''' | |
| 3 | |
| 4 import Image, ImageDraw # PIL | |
|
44
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
5 #import aggdraw # agg on top of PIL (antialiased drawing) |
| 28 | 6 from moving import Point |
| 7 #import utils | |
| 8 | |
| 9 __metaclass__ = type | |
| 10 | |
| 11 def drawLines(filename, origins, destinations, w = 1, resultFilename='image.png'): | |
| 12 '''Draws lines over the image ''' | |
| 13 | |
| 14 img = Image.open(filename) | |
| 15 | |
| 16 draw = ImageDraw.Draw(img) | |
| 17 #draw = aggdraw.Draw(img) | |
| 18 #pen = aggdraw.Pen("red", width) | |
| 19 for p1, p2 in zip(origins, destinations): | |
| 20 draw.line([p1.x, p1.y, p2.x, p2.y], width = w, fill = (256,0,0)) | |
| 21 #draw.line([p1.x, p1.y, p2.x, p2.y], pen) | |
| 22 del draw | |
| 23 | |
| 24 #out = utils.openCheck(resultFilename) | |
| 25 img.save(resultFilename) | |
| 26 | |
|
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
27 def projectArray(homography, points): |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
28 '''Returns the coordinates of the projected points (format 2xN points) |
| 28 | 29 through homography''' |
| 30 from numpy.core._dotblas import dot | |
| 31 from numpy.core.multiarray import array | |
|
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
32 from numpy.lib.function_base import append |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
33 |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
34 if points.shape[0] != 2: |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
35 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
|
36 |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
37 if (homography!=None) and homography.size>0: |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
38 augmentedPoints = append(points,[[1]*points.shape[1]], 0) |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
39 prod = dot(homography, augmentedPoints) |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
40 return prod[0:2]/prod[2] |
| 28 | 41 else: |
| 42 return p | |
| 43 | |
|
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
44 def project(homography, p): |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
45 '''Returns the coordinates of the projection of the point p |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
46 through homography''' |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
47 from numpy.core.multiarray import array |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
48 return projectArray(homography, array([[p[0]],p[1]])) |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
49 |
| 28 | 50 def projectTrajectory(homography, trajectory): |
| 51 '''Projects a series of points in the format | |
| 52 [[x1, x2, ...], | |
|
98
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
53 [y1, y2, ...]]''' |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
54 from numpy.core.multiarray import array |
|
b85912ab4064
refactored projection functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
63
diff
changeset
|
55 return projectArray(homography, array(trajectory)) |
| 28 | 56 |
|
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
|
57 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
|
58 '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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 |
|
60
1d36a676c745
minor class name change
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
64 class FourWayIntersection: |
|
44
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
65 '''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
|
66 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
|
67 self.dimension = dimension |
|
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
68 self.coordX = coordX |
|
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
69 self.coordY = coordY |
|
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
70 |
|
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
71 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
|
72 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
|
73 |
|
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 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
|
78 |
|
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
79 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
|
80 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
|
81 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
|
82 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
|
83 axis('equal') |
