comparison python/utils.py @ 48:8aed225f71d8

rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 23 Sep 2010 17:24:31 -0400
parents b5d007612e16
children 75cf537b8d88
comparison
equal deleted inserted replaced
47:e27598865af3 48:8aed225f71d8
7 import moving 7 import moving
8 8
9 __metaclass__ = type 9 __metaclass__ = type
10 10
11 commentChar = '#' 11 commentChar = '#'
12
13 delimiterChar = '%';
12 14
13 ######################### 15 #########################
14 # simple statistics 16 # simple statistics
15 ######################### 17 #########################
16 18
144 if quit: 146 if quit:
145 from sys import exit 147 from sys import exit
146 exit() 148 exit()
147 return None 149 return None
148 150
149 def readline(f): 151 def readline(f, commentCharacter = commentChar):
150 '''Modified readline function to skip comments.''' 152 '''Modified readline function to skip comments.'''
151 s = f.readline() 153 s = f.readline()
152 while (len(s) > 0) and s.startswith(commentChar): 154 while (len(s) > 0) and s.startswith(commentCharacter):
153 s = f.readline() 155 s = f.readline()
154 return s.strip() 156 return s.strip()
157
158 def getLines(f, delimiterCharacter = delimiterChar):
159 '''Gets a complete entry (all the lines) in between delimiterChar.'''
160 dataStrings = []
161 s = readline(f)
162 while (len(s) > 0) and (not s.startswith(delimiterCharacter)):
163 dataStrings += [s.strip()]
164 s = readline(f)
165 return dataStrings
155 166
156 def removeExtension(filename, delimiter = '.'): 167 def removeExtension(filename, delimiter = '.'):
157 '''Returns the filename minus the extension (all characters after last .)''' 168 '''Returns the filename minus the extension (all characters after last .)'''
158 i = filename.rfind(delimiter) 169 i = filename.rfind(delimiter)
159 if i>0: 170 if i>0:
180 '''Deletes the file while avoiding raising an error 191 '''Deletes the file while avoiding raising an error
181 if the file does not exist''' 192 if the file does not exist'''
182 if (os.path.exists(filename)): 193 if (os.path.exists(filename)):
183 os.remove(filename) 194 os.remove(filename)
184 195
185 def invertHomography(homography):
186 'Returns an inverted homography'
187 from numpy.linalg.linalg import inv
188 invH = inv(homography)
189 invH /= invH[2,2]
190 return invH
191
192 def plotPolygon(poly, options = ''): 196 def plotPolygon(poly, options = ''):
193 from numpy.core.multiarray import array 197 from numpy.core.multiarray import array
194 from matplotlib.pyplot import plot 198 from matplotlib.pyplot import plot
195 from shapely.geometry import Polygon 199 from shapely.geometry import Polygon
196 200