KeyPoint上的OpenCV3矩阵变换失败

时间:2017-02-26 13:01:28

标签: python-3.5 opencv3.0

我正在使用Python 3.5.2和opencv 3.1.0。我试图用我用cv.getAffineTransform()生成的变换矩阵来查询查询图像中的一些关键点(参见下面的代码)。无论我尝试传递给变换函数,它总会抛出这个错误:

  

cv2.error:D:\ opencv \ sources \ modules \ core \ src \ matmul.cpp:1947:error:( - 1515)scn == m.cols ||函数cv :: transform

中的scn + 1 == m.cols

如何通过关键点使cv2.transform()有效?

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)    

1 个答案:

答案 0 :(得分:0)

使用

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

而不是

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