【问题标题】:Get orientation of an image with respect to base image using Surf and Perspective Transform使用 Surf 和 Perspective Transform 获取图像相对于基础图像的方向
【发布时间】:2017-12-30 21:12:47
【问题描述】:

在模块 del5.py 中

import cv2
import numpy as np

base_img = cv2.imread("/tmp/a/1.jpg")
test_img = cv2.imread("/tmp/a/1_1.jpg")

surf = cv2.xfeatures2d.SURF_create()

base_keyPoints,base_descriptors=surf.detectAndCompute(base_img,None)
test_keyPoints,test_descriptors=surf.detectAndCompute(test_img,None)

bf = cv2.BFMatcher()

matches = bf.knnMatch(base_descriptors, test_descriptors,k=2)#, k=2)

goodMatches = []

for m, n in matches:
    if m.distance < 0.7 * n.distance:
        goodMatches.append(m)

print len(goodMatches)

sourcePoints=np.float32([base_keyPoints[m.queryIdx].pt for m in goodMatches])
destinationPoints=np.float32([test_keyPoints[m.trainIdx].pt for m in goodMatches ])

print len(sourcePoints)
print len(destinationPoints)

sourcePoints = np.float32([[c[0],c[1] ]for c in sourcePoints])
destinationPoints = np.float32([[c[0],c[1] ]for c in destinationPoints])

_m = cv2.getPerspectiveTransform(sourcePoints, destinationPoints)

我正在使用 python 2.7 和 OpenCV 3。我必须使用相同的图像,但测试图像相对于基础图像旋转了 90 度

在上面的代码中,我尝试像基本图像一样获得测试(旋转图像)的完美视图,我的算法步骤是:

  1. 读取两个图像(基础和测试)
  2. 创建冲浪
  3. 获取两张图片的特征
  4. 提取好的特征
  5. 获取两个图像的特征点(源点和目标点)
  6. 获取透视变换并对图像执行扭曲透视以获得完美视图

但是当尝试获取透视图时,我得到了错误

输出:

> 4116
> 4116 
> 4116 
> OpenCV Error: Assertion failed (src.checkVector(2,
> CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4) in
> getPerspectiveTransform, file
> /opt/opencv/modules/imgproc/src/imgwarp.cpp, line 7135 Traceback (most
> recent call last):   File "del5.py", line 41, in <module>
>     _m = cv2.getPerspectiveTransform(sourcePoints, destinationPoints) cv2.error: /opt/opencv/modules/imgproc/src/imgwarp.cpp:7135: error:
> (-215) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F)
> == 4 in function getPerspectiveTransform

【问题讨论】:

    标签: python python-2.7 opencv numpy image-processing


    【解决方案1】:

    在你的 getPerspectiveTransform 函数中你只需要通过四个点。 你正在尝试传递一个列表。

    //In your code change this line to this.
    _m = cv2.getPerspectiveTransform(sourcePoints, destinationPoints)
    
    //Change into this one.
    _m = cv2.getPerspectiveTransform(sourcePoints[0:4], destinationPoints[0:4])
    

    对于透视变换,您需要一个 3x3 变换矩阵。即使在转换之后,直线也将保持笔直。要找到这个变换矩阵,您需要输入图像上的 4 个点和输出图像上的对应点。这4个点中,有3个不应该共线。变换矩阵可以通过函数 cv2.getPerspectiveTransform 找到。然后用这个 3x3 变换矩阵应用 cv2.warpPerspective。

    根据 OpenCV 文档Perspective Transformation

    【讨论】:

    • 要添加到这个答案,findHomography() 将使用 RANSAC 过滤匹配项基于 >=4 点找到最佳单应性。所以不要只使用四个点......使用所有它们!
    猜你喜欢
    • 2020-04-13
    • 2023-03-30
    • 1970-01-01
    • 2016-06-26
    • 2016-02-09
    • 1970-01-01
    • 2014-12-05
    • 2020-02-16
    • 1970-01-01
    相关资源
    最近更新 更多