Source code for TimeTrack
# CellTracking.py
# By MW, Jun 2013
# GPLv3+
#
# Class managing a timetraack, an abstract object handling up timepoints
import logging, cv2, imp
[docs]class TimeTrack :
def __init__(self) :
self.times = []
self.pols = []
[docs] def add_timepoint(self, time, pol) :
ex = self.get_index(time) # Check if the timepoint exists and get its index
if ex[0] : # Edit it
self.pols[ex[1]] = pol
else :# Create it
self.times.insert(ex[1], time)
self.pols.insert(ex[1], pol)
[docs] def del_timepoint(self, time) :
ex = self.get_index(time)
if not ex[0] :
logging.error('The timepoint %s cannot be removed because it does not exist.', time)
else :
self.times.pop(ex[1])
self.pols.pop(ex[1])
[docs] def get_polygon(self, time) :
ex = self.get_index(time)
if ex[0] :
return self.pols[ex[1]]
elif self.times != [] :
if ex[1] == len(self.times) :
return self.pols[-1]
elif ex[1] == 0 :
return self.pols[0]
else : # We have to interpolate the polygon
t1 = self.times[ex[1]-1]
t2 = self.times[ex[1]]
pol1 = self.pols[ex[1]-1]
pol2 = self.pols[ex[1]]
pol = []
for i in range(len(pol1)) :
point = []
for coord in range(len(pol1[0])) : # Iterate over x and y
#print pol1[i][coord]
#print (pol2[i][coord]-pol1[i][coord])
#print (t2-time)/(t2-float(t1))
point.append(pol1[i][coord]+
(pol2[i][coord]-pol1[i][coord])*
(time-t1)/(t2-float(t1)) )
pol.append(tuple(point))
return pol
else :
logging.error('No timepoint instanciated, cannot interpolate.')
[docs] def add_point(self, time, pos, point) :
"""Function that add a point in the polygon"""
ex = self.get_index(time)
if not ex[0] :
logging.error("Cannot add a point to a frame that does not exist. Please create it before")
else :
for i in range(len(self.times)) :
t = self.times[i]
if t != time :
pol = self.pols[i]
if pos == 0 :
p1 = pol[-1]
p2 = pol[0]
else :
p1 = pol[pos-1]
p2 = pol[pos]
p = []
for coord in range(len(p1)) :
p.append((p2[coord]-p1[coord])*0.5)
self.pols[i].insert(pos, p)
else :
self.pols[i].insert(pos, tuple(point))
[docs] def get_index(self, time) :
if time in self.times :
return (True, self.times.index(time))
elif self.times == [] :
return (False, 0)
else :
i = 0
while (i<len(self.times)) and (self.times[i] < time) :
i+=1
return (False, i)