比较两个多维Numpy数组

时间:2014-10-07 16:05:49

标签: python opencv

我正在研究用于检测后续图像快照中的运动的图像分析代码。为此,我决定在opencv中使用光流功能,找到在初始图像中跟踪的好点并预测后续图像中的点。

# feature extraction of points to track 
pt = cv2.goodFeaturesToTrack(img1,**features_param)

# convert points to floating-point
p0 =np.float32(pt).reshape(-1,1,2)

# get predicted points using lucas-kanade optical flow 
p1,st,err =cv2.calcOpticalFlowPyrLK(img1, img2,p0,
                                       None,**lk_params)

为了找到正确预测的点,光流功能反向运行(第二图像优先)。然后计算初始点(跟踪点)和后向预测(p0r)之间的绝对差值,如果该值低于1,那么如果不是正确的预测,那么这些点是正确预测的。点。

# forward-backward error detection
p0r,st,err =cv2.calcOpticalFlowPyrLK(img2,img1,p1,
                                        None,**lk_params)

# get correctly predicted points via absolute difference
d = abs(p0-p0r).reshape(-1, 2).max(-1)
good = d < 1

浏览预测点p1并找到适合&#34; good&#34;的值。条件。

# cycle through all current and new keypoints and only keep
# those that satisfy the "good" condition above

# Initialize a list to hold new keypoints
new_keypoints = list()

# get good points
 for (x, y), good_flag,ind in zip(p1.reshape(-1, 2), good,enumerate(good)):
        if not good_flag:
            continue
        new_keypoints.append((x,y))

我需要检查在new_keypoints中最终预测到的p0中的哪些原始点。

1 个答案:

答案 0 :(得分:1)

在折磨我的大脑后,我设法解决了我的问题。我创建了一个for循环,它遍历numpy数组(p0)中的每个点并预测点,如果符合&#34; good&#34;标准它被附加到列表,否则它被省略。然后我继续计算该点与其新预测位置之间的欧氏距离。这是代码:


解决方案

    # feature extraction of points to track
    pt = cv2.goodFeaturesToTrack(img1,**features_param)
    p0 =np.float32(pt).reshape(-1,1,2)

    pr,st,err =cv2.calcOpticalFlowPyrLK(img1, img2,p0,
                                            None,**lk_params)

    # append correctly predicted points
    dist = list()
    for loop in p0:
        p1,st,err =cv2.calcOpticalFlowPyrLK(img1, img2,loop,
                                            None,**lk_params)

        p0r,st,err =cv2.calcOpticalFlowPyrLK(img2,img1,p1,
                                        None,**lk_params)

        # calculate euclidean distance of predicted points
        if abs(loop-p0r).reshape(-1, 2).max(-1) < 1:
            dst = distance.euclidean(loop,p0r)
            dist.append(dst)