Ask Your Question
0

How to convert Keypoints to an argument for findhomography()?

asked 2017-01-15 20:39:35 -0600

tamina gravatar image

I've seen some tutorials use this code to convert Keypoints (from a list of matches) to an argument for findHomography().

src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)

I would prefer to use an existing function if it exists( ie. Keypoint_convert() ) for better readability. However when I try this:

  kp_template_match = [kp_template[m.queryIdx].pt for m in matches]
  kp_scene_match = [kp_scene[m.trainIdx].pt for m in matches]

  src_pts = cv2.KeyPoint_convert(kp_template_match)
  dst_pts = cv2.KeyPoint_convert(kp_scene_match)

  homography, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC)

, I get this error:

> TypeError: srcPoints is not a numpy array, neither a scalar

Can anyone explain why?

edit retag flag offensive close merge delete

Comments

kp_template[m.queryIdx] is the keypoint, kp_template[m.queryIdx].pt the resp. Point, so your kp_template_match is already an array of points, albeit not a numpy one.

i'm not sure, if using cv2.KeyPoint_convert makes a lot of sense here.

berak gravatar imageberak ( 2017-01-16 02:52:50 -0600 )edit

@berak Isn't the reshape essentially the same thing as converting it into Point2f form? This is what Keypoint_convert appears to do also.

tamina gravatar imagetamina ( 2017-01-16 16:39:13 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2017-01-16 16:39:28 -0600

tamina gravatar image

I somehow got this to run without errors now, I had used a separate function to get the keypoints and matches, by putting it all into 1 function the error was removed. (Maybe there was some data lost in the function call, or I had a typo somewhere)

edit flag offensive delete link more
0

answered 2017-03-28 15:26:37 -0600

    tp=[]
    qp=[]
    for m in goodMatch:
        tp.append(trainKP[m.trainIdx].pt)
        qp.append(queryKP[m.queryIdx].pt)
    tp,qp=np.float32((tp,qp))
    H,mask=cv2.findHomography(tp,qp,cv2.RANSAC,3.0)

I think this will work, source

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-01-15 20:39:35 -0600

Seen: 3,917 times

Last updated: Mar 28 '17