comparison python/objectsmoothing.py @ 606:75ad9c0d6cc3

update the method to use multi featutes instead on single feature
author MohamedGomaa
date Mon, 24 Nov 2014 13:02:10 -0500
parents 3550da215e7a
children 6ee8765bb8db
comparison
equal deleted inserted replaced
605:3550da215e7a 606:75ad9c0d6cc3
10 if reverse: 10 if reverse:
11 dist[f]= moving.Point.distanceNorm2(feat.getPositionAtInstant(t+1),f.getPositionAtInstant(t)) 11 dist[f]= moving.Point.distanceNorm2(feat.getPositionAtInstant(t+1),f.getPositionAtInstant(t))
12 else: 12 else:
13 dist[f]= moving.Point.distanceNorm2(feat.getPositionAtInstant(t-1),f.getPositionAtInstant(t)) 13 dist[f]= moving.Point.distanceNorm2(feat.getPositionAtInstant(t-1),f.getPositionAtInstant(t))
14 return min(dist, key=dist.get) # = utils.argmaxDict(dist) 14 return min(dist, key=dist.get) # = utils.argmaxDict(dist)
15
15 def FeatureList(obj,minLengthParam=0.7): 16 def FeatureList(obj,minLengthParam=0.7):
16 featureList=[] 17 featureList=[]
17 for i in obj.features: 18 for i in obj.features:
18 if i.length>= minLengthParam*obj.length(): 19 if i.length>= minLengthParam*obj.length():
19 featureList.append(i.num) 20 featureList.append(i.num)
68 angle = degrees(atan2(p3.y -p1.y, p3.x -p1.x)) 69 angle = degrees(atan2(p3.y -p1.y, p3.x -p1.x))
69 bearing1 = (90 - angle) % 360 70 bearing1 = (90 - angle) % 360
70 angle2 = degrees(atan2(p2.y -p1.y, p2.x -p1.x)) 71 angle2 = degrees(atan2(p2.y -p1.y, p2.x -p1.x))
71 bearing2 = (90 - angle2) % 360 72 bearing2 = (90 - angle2) % 360
72 dist= moving.Point.distanceNorm2(p1, p2) 73 dist= moving.Point.distanceNorm2(p1, p2)
73 return [dist,bearing1,bearing2,bearing2-bearing1] 74 return [dist,bearing1,bearing2,bearing2-bearing1]
74 75
75 def computeSmoothVelocity (object,smoothing=True,halfWidth=3): 76 #Quantitative analysis "CSJ" functions
76 velocities=[[],[]] 77 def computeVelocities (object,smoothing=True,halfWidth=3): #compute velocities from positions
78 velocities={}
77 for i in list(object.timeInterval)[:-1]: 79 for i in list(object.timeInterval)[:-1]:
78 p1= object.getPositionAtInstant(i) 80 p1= object.getPositionAtInstant(i)
79 p2= object.getPositionAtInstant(i+1) 81 p2= object.getPositionAtInstant(i+1)
80 v=p2-p1 82 velocities[i]=p2-p1
81 velocities[0].append(v.x) 83 velocities[object.timeInterval.last]= velocities[object.timeInterval.last-1] # duplicate last point
82 velocities[1].append(v.y)
83 velocities[0].append(v.x) # duplicate last point
84 velocities[1].append(v.y)
85 if smoothing: 84 if smoothing:
86 v1= list(utils.filterMovingWindow(velocities[0], halfWidth)) 85 velX= [velocities[y].aslist()[0] for y in sorted(velocities.keys())]
87 v2= list(utils.filterMovingWindow(velocities[1], halfWidth)) 86 velY= [velocities[y].aslist()[1] for y in sorted(velocities.keys())]
88 velocities=[v1,v2] 87 v1= list(utils.filterMovingWindow(velX, halfWidth))
88 v2= list(utils.filterMovingWindow(velY, halfWidth))
89 smoothedVelocity={}
90 for t,i in enumerate(sorted(velocities.keys())):
91 smoothedVelocity[i]=moving.Point(v1[t], v2[t])
92 velocities=smoothedVelocity
89 return velocities 93 return velocities
90 94
91 #Quantitative analysis "CSJ" functions
92 def computeVelocities (object): #compute velocities from positions TODO: combine with "computeSmoothVelocity"
93 speedMagnitude={}
94 speedVector={}
95 for i in list(object.timeInterval)[:-1]:
96 p1= object.getPositionAtInstant(i)
97 p2= object.getPositionAtInstant(i+1)
98 speedMagnitude[i]=(p2-p1).norm2()
99 speedVector[i]= p2-p1
100 return speedMagnitude,speedVector
101
102 def computeAcceleration (object,fromPosition=True): 95 def computeAcceleration (object,fromPosition=True):
103 accMagnitude={} 96 acceleration={}
104 accVector={}
105 if fromPosition: 97 if fromPosition:
106 tmp,sp=computeVelocities(object) 98 velocities=computeVelocities(object,False,1)
107 for i in sorted (sp.keys()): 99 for i in sorted (velocities.keys()):
108 if i != sorted (sp.keys())[-1]: 100 if i != sorted (velocities.keys())[-1]:
109 accMagnitude[i]= (sp[i+1]-sp[i]).norm2() 101 acceleration[i]= velocities[i+1]-velocities[i]
110 accVector[i]= sp[i+1]-sp[i]
111 else: 102 else:
112 for i in list(object.timeInterval)[:-1]: 103 for i in list(object.timeInterval)[:-1]:
113 v1= object.getVelocityAtInstant(i) 104 v1= object.getVelocityAtInstant(i)
114 v2= object.getVelocityAtInstant(i+1) 105 v2= object.getVelocityAtInstant(i+1)
115 accMagnitude[i]=(v2-v1).norm2() 106 acceleration[i]= v2-v1
116 accVector[i]= v2-v1 107 return acceleration
117 return accMagnitude,accVector
118 108
119 def computeJerk (object,fromPosition=True): 109 def computeJerk (object,fromPosition=True):
120 jerk={} 110 jerk={}
121 tmp,acc=computeAcceleration (object,fromPosition=fromPosition) 111 acceleration=computeAcceleration (object,fromPosition=fromPosition)
122 for i in sorted (acc.keys()): 112 for i in sorted (acceleration.keys()):
123 if i != sorted (acc.keys())[-1]: 113 if i != sorted (acceleration.keys())[-1]:
124 jerk[i]= (acc[i+1]-acc[i]).norm2() 114 jerk[i]= (acceleration[i+1]-acceleration[i]).norm2()
125 return jerk 115 return jerk
126 116
127 def squaredSumJerk (object,fromPosition=True): 117 def sumSquaredJerk (object,fromPosition=True):
128 jerk= computeJerk (object,fromPosition=fromPosition) 118 jerk= computeJerk (object,fromPosition=fromPosition)
129 t=0 119 t=0
130 for i in sorted(jerk.keys()): 120 for i in sorted(jerk.keys()):
131 t+= jerk[i]* jerk[i] 121 t+= jerk[i]* jerk[i]
132 return t 122 return t
133 123
134 def getObject(obj,features,featureID,newNum,smoothing=False,halfWidth=3,computeVelocities=True,create=False): 124 def getObject(obj,features,featureID,newNum,smoothing=False,halfWidth=3,create=False):
135 results=[] 125 results=[]
136 bearing={} 126 bearing={}
137 if create: 127 if create:
138 feature= buildFeature(obj,features,featureID,num=1) 128 feature= buildFeature(obj,features,featureID,num=1)
139 else: 129 else:
199 if featureList==[]: 189 if featureList==[]:
200 featureList.append(longestFeature(obj)) 190 featureList.append(longestFeature(obj))
201 create=True 191 create=True
202 objs=[] 192 objs=[]
203 for featureID in featureList: 193 for featureID in featureList:
204 objTMP=getObject(obj,features,featureID,newNum,smoothing=smoothing,halfWidth=halfWidth,computeVelocities=computeVelocities,create=create) 194 objTMP=getObject(obj,features,featureID,newNum,smoothing=smoothing,halfWidth=halfWidth,create=create)
205 objs.append(objTMP) 195 objs.append(objTMP)
206 newTranslated = moving.Trajectory() 196 newTranslated = moving.Trajectory()
207 newInterval=[] 197 newInterval=[]
208 for t in obj.timeInterval: 198 for t in obj.timeInterval:
209 xCoord=[] 199 xCoord=[]
220 210
221 newObj= moving.MovingObject(newNum,timeInterval=moving.TimeInterval(min(newInterval),max(newInterval)),positions=newTranslated) 211 newObj= moving.MovingObject(newNum,timeInterval=moving.TimeInterval(min(newInterval),max(newInterval)),positions=newTranslated)
222 212
223 if computeVelocities: 213 if computeVelocities:
224 tmpTraj = moving.Trajectory() 214 tmpTraj = moving.Trajectory()
225 velocities= computeSmoothVelocity(newObj,True,5) 215 velocities= computeVelocities(newObj,True,5)
226 for i in xrange(len(velocities[0])): 216 for i in sorted(velocities.keys()):
227 p=moving.Point(velocities[0][i], velocities[1][i]) 217 tmpTraj.addPosition(velocities[i])
228 tmpTraj.addPosition(p)
229 newObj.velocities=tmpTraj 218 newObj.velocities=tmpTraj
230 else: 219 else:
231 newObj.velocities=obj.velocities 220 newObj.velocities=obj.velocities
232 221
233 if optimize: 222 if optimize:
234 csj1= squaredSumJerk (obj,fromPosition=True) 223 csj1= sumSquaredJerk (obj,fromPosition=True)
235 csj2= squaredSumJerk (newObj,fromPosition=True) 224 csj2= sumSquaredJerk (newObj,fromPosition=True)
236 if csj1<csj2: 225 if csj1<csj2:
237 newObj=obj 226 newObj=obj
238 newObj.velocities=obj.velocities 227 newObj.velocities=obj.velocities
239 if computeVelocities and csj1>=csj2: 228 if computeVelocities and csj1>=csj2:
240 csj3= squaredSumJerk (obj,fromPosition=False) 229 csj3= sumSquaredJerk (obj,fromPosition=False)
241 csj4= squaredSumJerk (newObj,fromPosition=False) 230 csj4= sumSquaredJerk (newObj,fromPosition=False)
242 if csj4<=csj3: 231 if csj4<=csj3:
243 newObj.velocities= obj.velocities 232 newObj.velocities= obj.velocities
244 newObj.featureNumbers=obj.featureNumbers 233 newObj.featureNumbers=obj.featureNumbers
245 newObj.features=obj.features 234 newObj.features=obj.features
246 newObj.userType=obj.userType 235 newObj.userType=obj.userType