简体   繁体   中英

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.

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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