轨迹链接:生成两个配置之间所有可能的链接,包括出生和死亡

时间:2018-07-13 04:47:58

标签: python itertools

我想在视频帧之间链接粒子轨迹。在帧t0中,标识s0特征。在帧t1中,标识s1个特征。

问题是要找到特征1,2,...,s0与特征1,2,...,s1之间最可能的链接。每个帧中的粒子数量不必相等,实际上,它们可以在帧之间出生和死亡。此问题与所有可能的链接的确定有关。

例如,如果在第t0帧中有2个粒子,在第t1帧中有3个粒子,则潜在配置为

[[1,1],[2,2],['birth',3]]
[[1,2],[2,1],['birth',3]]
[[1,3],[2,2],[3,1]]
[[1,'death'],['birth',1],[2,2],['birth',3]]

,依此类推。

对于任意s0和s1,我都有解决此问题的方法,但是随着这些数字变大,它无法扩展。是

link_set = [] # this is the set of all links not including birth or death 
for i in range(s0): 
    for j in range(s1):
        link_set.append((i,j))

# link_set is now the set of all conventional links without birth and death 
broken_links = []

# now every particle in t0 can die 
for i in range(s0):
    broken_links.append((i,'death'))
# and every particle in t1 can be born 
for j in range(s1):
    broken_links.append(('birth', j))


all_links = link_set + broken_links 
# now filter all configurations which address each particle more than once or less than once 

def filterfn(config):
    """filter configurations which do not address every particle or which address one more than once""" 

    t0linksnobirth = []
    t1linksnodeath = []
    for [i,j] in config:
        if i!='birth':
            t0linksnobirth.append(i)
        if j!='death':
            t1linksnodeath.append(j)

    # ensure no duplicate assignments
    if len(set(t0linksnobirth))!=len(t0linksnobirth):
        return False
    elif len(set(t1linksnodeath))!=len(t1linksnodeath):
        return False
    # ensure all particles assigned 
    if set(t0linksnobirth)==set(range(s0)) and set(t1linksnodeath)==set(range(s1)):
        return True
    else:
        return False

configs = []

for r in range(max(s0,s1),s0+s1):
    r_configs = list(itertools.combinations(all_links,r))
    r_configs = filter(lambda c: filterfn(c), r_configs)
    configs.extend(r_configs)

# now configs is a list containing all possible configurations 
# [[[1,1],[2,2],['birth',3],...],[[2,1],[1,2],...],...] and so on 

这里filterfn正在过滤所有组合的集合,以便输出集仅包括帧t0中的s0粒子和帧t1中的s1粒子的实际配置。这变得过大。有一种方法可以在评估所有组合的集合之前进行此过滤?

感谢您的帮助。我知道这个问题陈述有些令人困惑:我担心这是我目前能做的最好的事情。如果不清楚,请提出问题。

0 个答案:

没有答案