How to convert Keypoints to an argument for findhomography()?
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?
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 Isn't the reshape essentially the same thing as converting it into Point2f form? This is what Keypoint_convert appears to do also.