Mercurial Hosting > traffic-intelligence
changeset 641:9fe254f11743
cleaning under way and renaming
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 09 Apr 2015 16:55:24 +0200 |
parents | fe34c0f79c32 |
children | 932f96c89212 |
files | python/objectsmoothing.py |
diffstat | 1 files changed, 46 insertions(+), 42 deletions(-) [+] |
line wrap: on
line diff
--- a/python/objectsmoothing.py Thu Apr 09 13:21:22 2015 +0200 +++ b/python/objectsmoothing.py Thu Apr 09 16:55:24 2015 +0200 @@ -14,7 +14,6 @@ return min(dist, key=dist.get) # = utils.argmaxDict(dist) def getFeatures(obj,features,featureID): - #longestFeature = utils.argmaxDict({f:f.length() for i,f in enumerate(obj.features)}) t1,t3 = features[featureID].getFirstInstant(), features[featureID].getLastInstant() listFeatures=[[features[featureID],t1,t3,moving.Point(0,0)]] # find the features to fill in the beginning of the object existence @@ -64,16 +63,16 @@ angle2 = degrees(atan2(p2.y -p1.y, p2.x -p1.x)) bearing2 = (90 - angle2) % 360 dist= moving.Point.distanceNorm2(p1, p2) - return [dist,bearing1,bearing2,bearing2-bearing1] + return [dist,bearing1,bearing2,bearing2-bearing1] #Quantitative analysis "CSJ" functions -def computeVelocities (object,smoothing=True,halfWidth=3): #compute velocities from positions +def computeVelocities(obj,smoothing=True,halfWidth=3): #compute velocities from positions velocities={} - for i in list(object.timeInterval)[:-1]: - p1= object.getPositionAtInstant(i) - p2= object.getPositionAtInstant(i+1) + for i in list(obj.timeInterval)[:-1]: + p1= obj.getPositionAtInstant(i) + p2= obj.getPositionAtInstant(i+1) velocities[i]=p2-p1 - velocities[object.getLastInstant()]= velocities[object.getLastInstant()-1] # duplicate last point + velocities[obj.getLastInstant()]= velocities[obj.getLastInstant()-1] # duplicate last point if smoothing: velX= [velocities[y].aslist()[0] for y in sorted(velocities.keys())] velY= [velocities[y].aslist()[1] for y in sorted(velocities.keys())] @@ -85,30 +84,30 @@ velocities=smoothedVelocity return velocities -def computeAcceleration (object,fromPosition=True): +def computeAcceleration(obj,fromPosition=True): acceleration={} if fromPosition: - velocities=computeVelocities(object,False,1) - for i in sorted (velocities.keys()): - if i != sorted (velocities.keys())[-1]: + velocities=computeVelocities(obj,False,1) + for i in sorted(velocities.keys()): + if i != sorted(velocities.keys())[-1]: acceleration[i]= velocities[i+1]-velocities[i] else: - for i in list(object.timeInterval)[:-1]: - v1= object.getVelocityAtInstant(i) - v2= object.getVelocityAtInstant(i+1) + for i in list(obj.timeInterval)[:-1]: + v1= obj.getVelocityAtInstant(i) + v2= obj.getVelocityAtInstant(i+1) acceleration[i]= v2-v1 return acceleration -def computeJerk (object,fromPosition=True): +def computeJerk(obj,fromPosition=True): jerk={} - acceleration=computeAcceleration (object,fromPosition=fromPosition) - for i in sorted (acceleration.keys()): - if i != sorted (acceleration.keys())[-1]: - jerk[i]= (acceleration[i+1]-acceleration[i]).norm2() + acceleration=computeAcceleration(obj,fromPosition=fromPosition) + for i in sorted(acceleration.keys()): + if i != sorted(acceleration.keys())[-1]: + jerk[i] = (acceleration[i+1]-acceleration[i]).norm2() return jerk -def sumSquaredJerk (object,fromPosition=True): - jerk= computeJerk (object,fromPosition=fromPosition) +def sumSquaredJerk(obj,fromPosition=True): + jerk= computeJerk(obj,fromPosition=fromPosition) t=0 for i in sorted(jerk.keys()): t+= jerk[i]* jerk[i] @@ -118,9 +117,9 @@ results=[] bearing={} if create: - feature= buildFeature(obj,features,featureID,num=1) + feature = buildFeature(obj,features,featureID,num=1) else: - feature=features[featureID] + feature = features[featureID] for t in feature.getTimeInterval(): p1= feature.getPositionAtInstant(t) p2= obj.getPositionAtInstant(t) @@ -131,8 +130,7 @@ p3= feature.getPositionAtInstant(t) bearing[t]= getBearing(p1,p2,p3)[1] results.append(getBearing(p1,p2,p3)) - - + medianResults=np.median(results,0) dist= medianResults[0] angle= medianResults[3] @@ -175,33 +173,37 @@ newObj= moving.MovingObject(newNum,timeInterval=feature.getTimeInterval(),positions=translated) return newObj -def smoothObjectTrajectory(obj,features,newNum,minLengthParam=0.7,smoothing=False,plotResults=True,halfWidth=3,computeVelocities=True,optimize=True,create=False): +def smoothObject(obj,features,newNum,minLengthParam=0.7,smoothing=False,plotResults=True,halfWidth=3, _computeVelocities=True,optimize=True,create=False): + '''Computes a smoother trajectory for the object + and optionnally smoother velocities + + TODO: check whether features are necessary''' featureList=[i.num for i in obj.features if i.length() >= minLengthParam*obj.length()] if featureList==[]: - featureList.append(longestFeature(obj)) - create=True - objs=[] + featureList.append(utils.argmaxDict({f.getNum():f.length() for f in obj.features})) + create = True + newObjects = [] for featureID in featureList: - objTMP=smoothObjectTrajectory(obj,features,featureID,newNum,smoothing=smoothing,halfWidth=halfWidth,create=create) - objs.append(objTMP) + newObjects.append(smoothObjectTrajectory(obj,features,featureID,newNum,smoothing=smoothing,halfWidth=halfWidth,create=create)) + newTranslated = moving.Trajectory() - newInterval=[] - for t in obj.timeInterval: + newInterval = [] + for t in obj.getTimeInterval(): xCoord=[] yCoord=[] - for i in objs: + for i in newObjects: if i.existsAtInstant(t): p1= i.getPositionAtInstant(t) xCoord.append(p1.x) yCoord.append(p1.y) - if xCoord!=[]: + if xCoord != []: tmp= moving.Point(np.median(xCoord),np.median(yCoord)) newInterval.append(t) newTranslated.addPosition(tmp) - newObj= moving.MovingObject(newNum,timeInterval=moving.TimeInterval(min(newInterval),max(newInterval)),positions=newTranslated) + newObj= moving.MovingObject(newNum, timeInterval = moving.TimeInterval(min(newInterval),max(newInterval)),positions=newTranslated) - if computeVelocities: + if _computeVelocities: tmpTraj = moving.Trajectory() velocities= computeVelocities(newObj,True,5) for i in sorted(velocities.keys()): @@ -211,19 +213,21 @@ newObj.velocities=obj.velocities if optimize: - csj1= sumSquaredJerk (obj,fromPosition=True) - csj2= sumSquaredJerk (newObj,fromPosition=True) + csj1= sumSquaredJerk(obj,fromPosition=True) + csj2= sumSquaredJerk(newObj,fromPosition=True) if csj1<csj2: newObj=obj newObj.velocities=obj.velocities - if computeVelocities and csj1>=csj2: - csj3= sumSquaredJerk (obj,fromPosition=False) - csj4= sumSquaredJerk (newObj,fromPosition=False) + if _computeVelocities and csj1>=csj2: + csj3= sumSquaredJerk(obj,fromPosition=False) + csj4= sumSquaredJerk(newObj,fromPosition=False) if csj4<=csj3: newObj.velocities= obj.velocities + newObj.featureNumbers=obj.featureNumbers newObj.features=obj.features newObj.userType=obj.userType + if plotResults: plt.figure() plt.title('objects_id = {}'.format(obj.num))