KeyPoint 上的 OpenCV3 矩阵变换失败

林德拉

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

cv2.error: D:\opencv\sources\modules\core\src\matmul.cpp:1947: 错误: (-215) 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)    
martin_joerg

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Mac上为python 3.6安装opencv3

无法使用OpenCV3找到轮廓,但代码可在OpenCV2上使用

在OSX上使用python 3.5.0使用pyenv编译OpenCV3

如何在Ubuntu 16上安装适用于Python 3.6的OpenCV3?

在Windows 8.1(64位)上使用Python3.4安装OpenCV3

OpenCV未定义的引用(带有vector <KeyPoint>的FileStorage)

Point3f 代替 Point2f 用于 OpenCV 中的 KeyPoint

将Mac El Capitan上的Brew降级公式还原为先前版本(opencv3 / 3.0.0)

虚拟环境上的Opencv3和Python 2.7-AttributeError:“模块”对象没有属性“ createLBPHFaceRecognizer”

增强多边形上的几何矩阵变换

矩阵变换图像上的C#光标位置

如何从OpenCV“ cv2.keypoint”对象中提取x,y坐标?

'&':对绑定成员函数表达式的非法操作。从KeyPoint向量OpenCV获取容量

opencv矩阵伪逆失败

在矩阵上重新运行失败构建的脚本

在OpenCV中合并两个仿射变换矩阵

将变换矩阵应用于 OpenCV 中的 warpTransform

允许用户输入图像和变换矩阵并查看应用的变换的 OpenCV 程序

断言在OpenCV的solvePNP上的点数失败

在OS X 10.9上安装OpenCV失败

变换矩阵

OpenCV中的简单矩阵乘法失败

JS 的线性变换和矩阵乘法失败

稀疏矩阵上的成对距离失败,并显示错误消息

如何离线安装Anaconda3中的OpenCV3?

在OpenCV中可视化图像上的热图矩阵

当我们对其应用矩阵变换时,SVG 上的圆的半径会发生什么变化?

appengine 上的 Symfony3 会话失败

如何使用3x3旋转矩阵和平移矢量应用变换?