Lyndra
Lyndra

Reputation: 107

OpenCV3 matrix transform on KeyPoint fails

I'm using Python 3.5.2 and opencv 3.1.0. I'm trying to warp some keypoints from a query image with a transformation matrix I generated with cv.getAffineTransform() (see below code). Whatever I try to pass to the transform function it will always throw me this error:

cv2.error: D:\opencv\sources\modules\core\src\matmul.cpp:1947: error: (-215) scn == m.cols || scn + 1 == m.cols in function cv::transform

How do I have to pass the keypoints to make cv2.transform() work?

import cv2
import numpy as np
import random

queryImage_path = "C:\tmp\query.jpg"
trainImage_path = "C:\tmp\train.jpg"

queryImage = cv2.imread(queryImage_path, cv2.IMREAD_COLOR)
trainImage = cv2.imread(trainImage_path, cv2.IMREAD_COLOR)

surf = cv2.xfeatures2d.SURF_create()

queryImage_keypoints = surf.detect(queryImage,None)
trainImage_keypoints = surf.detect(trainImage, None)

queryImage_keypoints, queryImage_descriptors = surf.compute(queryImage, queryImage_keypoints)
trainImage_keypoints, trainImage_descriptors = surf.compute(trainImage, trainImage_keypoints)

bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True)
matches = bf.match(queryImage_descriptors, trainImage_descriptors)

# get three random match indices which are not the same
match_index_a = random.randint(0, len(matches) - 1)
match_index_b = random.randint(0, len(matches) - 1)
match_index_c = random.randint(0, len(matches) - 1)

# get Keypoints from match indices

# queryImage- keypoints
queryImage_keypoint_a = queryImage_keypoints[matches[match_index_a].queryIdx]
queryImage_keypoint_b = queryImage_keypoints[matches[match_index_b].queryIdx]
queryImage_keypoint_c = queryImage_keypoints[matches[match_index_c].queryIdx]
# trainImage-keypoints
trainImage_keypoint_a = trainImage_keypoints[matches[match_index_a].trainIdx]
trainImage_keypoint_b = trainImage_keypoints[matches[match_index_b].trainIdx]
trainImage_keypoint_c = trainImage_keypoints[matches[match_index_c].trainIdx]

# get affine transformation matrix from these 6 keypoints
trainImage_points = np.float32([[trainImage_keypoint_a.pt[0], trainImage_keypoint_a.pt[1]],
                                [trainImage_keypoint_b.pt[0], trainImage_keypoint_b.pt[1]],
                                [trainImage_keypoint_c.pt[0], trainImage_keypoint_c.pt[1]]])
queryImage_points = np.float32([[queryImage_keypoint_a.pt[0], queryImage_keypoint_a.pt[1]],
                                [queryImage_keypoint_b.pt[0], queryImage_keypoint_b.pt[1]],
                                [queryImage_keypoint_c.pt[0], queryImage_keypoint_c.pt[1]]])

# get transformation matrix for current points
currentMatrix = cv2.getAffineTransform(queryImage_points, trainImage_points)

queryImage_keypoint = queryImage_keypoints[matches[0].queryIdx]

keypoint_asArray = np.array([[queryImage_keypoint.pt[0]], [queryImage_keypoint.pt[1]], [1]])

#queryImage_warped_keypoint = currentMatrix.dot(keypoint_asArray)
queryImage_warped_keypoint = cv2.transform(keypoint_asArray,currentMatrix)    

Upvotes: 0

Views: 480

Answers (1)

martin_joerg
martin_joerg

Reputation: 1163

Use

keypoint_asArray = np.array([[[queryImage_keypoint.pt[0], queryImage_keypoint.pt[1], 1]]])

instead of

keypoint_asArray = np.array([[queryImage_keypoint.pt[0]], [queryImage_keypoint.pt[1]], [1]])

Upvotes: 1

Related Questions