Mercurial > hg > nsaunier > traffic-intelligence
comparison python/cvutils.py @ 28:9ae709a2e8d0
rearranged code
| author | Nicolas Saunier <nico@confins.net> |
|---|---|
| date | Sat, 30 Jan 2010 21:42:53 -0500 |
| parents | |
| children | be3ae926e4e8 |
comparison
equal
deleted
inserted
replaced
| 27:44689029a86f | 28:9ae709a2e8d0 |
|---|---|
| 1 #! /usr/bin/env python | |
| 2 '''Image/Video utilities''' | |
| 3 | |
| 4 import Image, ImageDraw # PIL | |
| 5 import aggdraw # agg on top of PIL (antialiased drawing) | |
| 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): | |
| 34 pAugmented = insert(array(p.aslist()), [2],[1], axis=0) | |
| 35 tmp = dot(homography, pAugmented) | |
| 36 return Point(tmp[0]/tmp[2], tmp[1]/tmp[2]) | |
| 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 |
