Mercurial Hosting > traffic-intelligence
comparison python/utils.py @ 14:e7bbe8465591
homography and other utils
author | Nicolas Saunier <nico@confins.net> |
---|---|
date | Sat, 14 Nov 2009 19:02:46 -0500 |
parents | ffddccfab7f9 |
children | 9d6831cfe675 |
comparison
equal
deleted
inserted
replaced
13:30559b2cf7a9 | 14:e7bbe8465591 |
---|---|
43 return filename[:i] | 43 return filename[:i] |
44 else: | 44 else: |
45 return filename | 45 return filename |
46 | 46 |
47 def listfiles(dirname, extension, remove = False): | 47 def listfiles(dirname, extension, remove = False): |
48 '''Returns the list of files with the extension in the directory dirname''' | 48 '''Returns the list of files with the extension in the directory dirname |
49 If remove is True, the filenames are stripped from the extension''' | |
49 from os import listdir | 50 from os import listdir |
50 tmp = [f for f in listdir(dirname) if f.endswith(extension)] | 51 tmp = [f for f in listdir(dirname) if f.endswith(extension)] |
51 tmp.sort() | 52 tmp.sort() |
52 if remove: | 53 if remove: |
53 return [removeExtension(f, extension) for f in tmp] | 54 return [removeExtension(f, extension) for f in tmp] |
54 else: | 55 else: |
55 return tmp | 56 return tmp |
56 | 57 |
58 def removeFile(filename): | |
59 '''Deletes the file while avoiding raising an error | |
60 if the file does not exist''' | |
61 if (os.path.exists(filename)): | |
62 os.remove(filename) | |
63 | |
64 def invertHomography(homography): | |
65 'Returns an inverted homography' | |
66 invH = inv(homography) | |
67 invH /= invH[2,2] | |
68 return invH | |
69 | |
70 def project(homography, p): | |
71 '''Returns the coordinates of the projection of the point p | |
72 through homography''' | |
73 from numpy.core._dotblas import dot | |
74 from numpy.core.multiarray import array | |
75 from numpy.lib.function_base import insert | |
76 if (homography!=None) and (len(homography)>0): | |
77 pAugmented = insert(array(p), [2],[1], axis=0) | |
78 projected = dot(homography, pAugmented) | |
79 projected[0] /= projected[2] | |
80 projected[1] /= projected[2] | |
81 else: | |
82 projected = p | |
83 return projected[:2] | |
57 | 84 |
58 | 85 |
59 | 86 |
60 if __name__ == "__main__": | 87 if __name__ == "__main__": |
61 import doctest | 88 import doctest |