查了不少资料,最后还是看函数文档看懂了。
cost_limit 是代价矩阵中每个元素的上限值,超过cost_limit的元素就不参与分配了。
Docstring: Solve linear assignment problem using Jonker-Volgenant algorithm. cost: an N x N matrix containing the assignment costs. Entry cost[i, j] is the cost of assigning row i to column j. extend_cost: whether or not extend a non-square matrix [default: False] cost_limit: an upper limit for a cost of a single assignment [default: np.inf] return_cost: whether or not to return the assignment cost Returns (opt, x, y) where: opt: cost of the assignment, not returned if return_cost is False. x: a size-N array specifying to which column each row is assigned. y: a size-N array specifying to which row each column is assigned. When extend_cost and/or cost_limit is set, all unmatched entries will be marked by -1 in x/y. Type: builtin_function_or_method
用视频跟踪举个例子:
如图为kilman预测和检测框计算出来的代价矩阵,一般计算方式为1-iou。
然后把代价矩阵送入lapjv,以总体最小代价(分配后元素和最小)实现任务分配。
K1 | K2 | K3 | |
D1 | 0.5 | 0.6 | 0.7 |
D2 | 0.4 | 0.7 | 0.5 |
from lap import lapjv
import numpy as np
a = np.array([[0.5,0.6,0.7],[0.4,0.7,0.5]])
c,x,y = lapjv(a,extend_cost=True,cost_limit=0.4)
print(c,x,y)
output:0.4 [-1 0] [ 1 -1 -1]
c为求得的最小代价之和,x和y为分配结果,一个是将y分配给x,一个是将x分配个y,是一样的; 以x为例,-1代表未被匹配。 所以[-1,0]代表D1没有匹配项,D2匹配到了K1; cost_limit为代价矩阵中每个元素可以取值的上限不能超过0.4。
cost_limit改为0.6看一下:
from lap import lapjv
import numpy as np
a = np.array([[0.5,0.6,0.7],[0.4,0.7,0.5]])
c,x,y = lapjv(a,extend_cost=True,cost_limit=0.6)
print(c,x,y)
output: 1.0 [0 2] [ 0 -1 1]
这回两个目标都匹配上了,x=[0,2]代表D1匹配了K1,D2匹配了K3。
在目标跟踪里, cost_limit设置过小,会漏掉能匹配上的目标,设置过大,会增加错误的匹配。