简体   繁体   English

比较两个多维Numpy数组

[英]Comparing two Multidimensional Numpy arrays

I'm working on a image analysis code for detecting motion in subsequent image snapshots. 我正在研究一种图像分析代码,用于检测后续图像快照中的运动。 To do this, I decided to use the optical flow function in opencv that finds good points to track in an initial image and predict the points in a subsequent image. 为此,我决定在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)

In order to find points that were predicted correctly the optical flow function is run in reverse (second image first). 为了找到正确预测的点,反向运行光流函数(先显示第二张图像)。 Then an absolute difference is calculated between initial points (tracked points) and the backward predicted (p0r), if the value is below one then that points was predicted correctly if not it is a "bad" point. 然后,计算初始点(跟踪的点)和向后预测的点(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

Go through the predicted points p1 and find values that fit the "good" condition. 遍历预测点p1并找到适合“良好”条件的值。

# 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))

I need to check which original points in p0 ended up predicted in new_keypoints. 我需要检查p0中的哪些原始点最终在new_keypoints中预测。

After a torturing my brain I managed to get a solution to my problem. 在折磨我的大脑之后,我设法找到了解决我问题的方法。 I created a for loop that goes through each point in the numpy array (p0) and predicts the point, if meets the "good" criterion it is appended to a list otherwise it is omitted. 我创建了一个for循环,该循环遍历numpy数组(p0)中的每个点并预测该点,如果满足“良好”条件,则将其附加到列表中,否则将其省略。 I then proceed to calculate the euclidean distance between the point and its newly predicted position. 然后,我继续计算该点与其新预测的位置之间的欧式距离。 Here's the code: 这是代码:


Solution

    # 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM