这段代码在做什么?

What is this bit of code doing?

  for x, y in [np.int32(tr[-1]) for tr in self.tracks]:
                cv2.circle(mask, (x, y), 5, 0, -1)
            p = cv2.goodFeaturesToTrack(frame_gray, mask=mask, **feature_params)
            if p is not None:
                for x, y in np.float32(p).reshape(-1, 2):
                    self.tracks.append([(x, y)])

我对这些 for 循环感到困惑,我对 python 3.From 还很陌生那 -1 表示什么?)

无论如何,对于其实例中的每个 tr:self.tracks。底部的 for 循环在 numpy float array:p 中做同样的事情?他们为什么使用 'reshape'?那是干什么的?

如果有人不介意的话,也许可以帮我逐步完成 非常感谢。

self.tracks 似乎是一个 (n,2) list。外层循环获取这些值中的每一个,将它们两个两个地转换为整数 xy,然后使用对象 mask 执行 function/method cv2.circle 和其他几个参数。 container[-1]表示要container.

最后一个索引的值

function/method goodFeaturesToTrack 的值赋值给了 p (这似乎是一个数组或 None )。 ** 表示 feature_params 是一个参数字典(如果一个函数被定义为 myfunc(a,b=2,c=3,d=5) 你可以改变其中的一些值 通过调用 myfunc("value of a",**mydict) 来调用函数,其中 mydict 是一个包含零个或多个可选变量 a、b 和 c 的字典(例如 mydict={'b':8,d:0} 会将 b 和 d 从它们的默认值更改为分别为 8 和 0。

然后从 p 的重塑中提取新的(浮点值)xy 并作为一对附加回列表 self.tracks

reshape 中的 -1 表示您不关心给定轴的长度,只要另一个轴具有正确的形状即可。例如。一个包含 10 个值的数组将被重塑为 (5,2),一个 (4,4) 将被重塑为 (8,2) 等。这可以通过搜索 numpy.reshape:

找到

newshape : int or tuple of ints

The new shape should be compatible with the original shape. If an >integer, then the result will be a 1-D array of that length. One shape >dimension can be -1. In this case, the value is inferred from the length of >the array and remaining dimensions.