Mercurial > hg > nsaunier > traffic-intelligence
annotate python/cvutils.py @ 84:731df2fa0010
correction to compile with latest opencv
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 29 Mar 2011 01:47:00 -0400 |
| parents | 40e8e3bb3702 |
| children | b85912ab4064 |
| 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 | |
| 27 def project(homography, p): | |
| 28 '''Returns the coordinates of the projection of the point p | |
| 29 through homography''' | |
| 30 from numpy.core._dotblas import dot | |
| 31 from numpy.core.multiarray import array | |
| 32 from numpy.lib.function_base import insert | |
| 33 if (homography!=None) and (len(homography)>0): | |
|
63
40e8e3bb3702
corrected projection bugs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
60
diff
changeset
|
34 pAugmented = insert(array(p), [2],[1], axis=0) |
| 28 | 35 tmp = dot(homography, pAugmented) |
|
63
40e8e3bb3702
corrected projection bugs
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
60
diff
changeset
|
36 return [tmp[0]/tmp[2], tmp[1]/tmp[2]] |
| 28 | 37 else: |
| 38 return p | |
| 39 | |
| 40 def projectTrajectory(homography, trajectory): | |
| 41 '''Projects a series of points in the format | |
| 42 [[x1, x2, ...], | |
| 43 [y1, y2, ...]] | |
| 44 | |
| 45 Warning: not optimized, calls project()''' | |
| 46 projected = [[],[]] | |
| 47 for x, y in zip(trajectory[0], trajectory[1]): | |
| 48 p = [x,y] | |
| 49 pp = project(homography, p) | |
| 50 projected[0].append(pp[0]) | |
| 51 projected[1].append(pp[1]) | |
| 52 return projected | |
| 53 | |
|
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
|
54 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
|
55 '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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 |
|
60
1d36a676c745
minor class name change
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
61 class FourWayIntersection: |
|
44
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
62 '''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
|
63 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
|
64 self.dimension = dimension |
|
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
65 self.coordX = coordX |
|
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
66 self.coordY = coordY |
|
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
67 |
|
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
68 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
|
69 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
|
70 |
|
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
71 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
|
72 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
|
73 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
|
74 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
|
75 |
|
be3ae926e4e8
added simple intersection description, function to load collision points
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
28
diff
changeset
|
76 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
|
77 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
|
78 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
|
79 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
|
80 axis('equal') |
