【问题标题】:'cv2' has no attribute 'transform''cv2' 没有属性'transform'
【发布时间】:2020-09-18 01:16:03
【问题描述】:

我正在尝试运行 Harry R. 建议的代码 here(问题的第一个解决方案),我收到一条错误消息,提示 cv2 没有 transform 方法:

Traceback (most recent call last):
  File "/Users/kkk/PycharmProjects/TestICPPython3.7/ICPTest.py", line 64, in <module>
    M2 = icp(a, b, [0.1, 0.33, np.pi / 2.2], 30)
  File "/Users/kkk/PycharmProjects/TestICPPython3.7/ICPTest.py", line 32, in icp
    src = cv2.transform(src, Tr[0:2])
AttributeError: module 'cv2' has no attribute 'transform'

有人收到了吗?有什么问题?谢谢

【问题讨论】:

  • 我认为您将转换称为不是一种方法。你输入了 transform() 吗?
  • 是的,我做到了。它说NameError: name 'transform' is not defined。其实是通过cv2定义的——docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html
  • 您是否有自己的名为cv2.py 的脚本被导入而不是实际模块?
  • 不,实际上它似乎不适用于 Python 3.7。它适用于 Python 3.0。

标签: python opencv cv2 image-registration


【解决方案1】:

好的,我终于让它在 Python 3.0(我认为原始版本是 Python 2.7)中工作,而在 Python 3.7 中不是。请注意,cv2.estimateRigidTransform 被更改为 cv2.estimateAffinePartial2D 并且我们有两个返回变量而不是原始代码中的一个,因为现在消除了 cv2.estimateRigidTransform

import cv2
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import NearestNeighbors

def icp(a, b, init_pose=(0, 0, 0), no_iterations=13):

    """
    The Iterative Closest Point estimator.
    Takes two cloudpoints a[x,y], b[x,y], an initial estimation of
    their relative pose and the number of iterations
    Returns the affine transform that transforms
    the cloudpoint a to the cloudpoint b.
    Note:
        (1) This method works for cloudpoints with minor
        transformations. Thus, the result depents greatly on
        the initial pose estimation.
        (2) A large number of iterations does not necessarily
        ensure convergence. Contrarily, most of the time it
        produces worse results.
    """

    src = np.array([a.T], copy=True).astype(np.float32)
    dst = np.array([b.T], copy=True).astype(np.float32)

    # Initialise with the initial pose estimation
    Tr = np.array([[np.cos(init_pose[2]), -np.sin(init_pose[2]), init_pose[0]],
                   [np.sin(init_pose[2]), np.cos(init_pose[2]), init_pose[1]],
                   [0, 0, 1]])

    src = cv2.transform(src, Tr[0:2])

    for i in range(no_iterations):

        # Find the nearest neighbours between the current source and the
        # destination cloudpoint

        nbrs = NearestNeighbors(n_neighbors=1, algorithm='auto').fit(dst[0])
        distances, indices = nbrs.kneighbors(src[0])

        # Compute the transformation between the current source
        # and destination cloudpoint
        T, tmp = cv2.estimateAffinePartial2D(src, dst[0, indices.T], False)
        # Transform the previous source and update the
        # current source cloudpoint
        src = cv2.transform(src, T)
        # Save the transformation from the actual source cloudpoint
        # to the destination
        Tr = np.dot(Tr, np.vstack((T, [0, 0, 1])))

    return Tr[0:2]


# Create the datasets
ang = np.linspace(-np.pi / 2, np.pi / 2, 320)
a = np.array([ang, np.sin(ang)])
th = np.pi / 2
rot = np.array([[np.cos(th), -np.sin(th)], [np.sin(th), np.cos(th)]])
b = np.dot(rot, a) + np.array([[0.2], [0.3]])

# Run the icp
M2 = icp(a, b, [0.1, 0.33, np.pi / 2.2], 30)
#
# # Plot the result
src = np.array([a.T]).astype(np.float32)
res = cv2.transform(src, M2)
plt.figure()
plt.plot(b[0], b[1])
plt.plot(res[0].T[0], res[0].T[1], 'r.')
plt.plot(a[0], a[1])
plt.show()

【讨论】:

    猜你喜欢
    • 2020-06-04
    • 2019-08-14
    • 1970-01-01
    • 2019-12-17
    • 2021-02-22
    • 2019-09-14
    • 2019-06-07
    • 1970-01-01
    • 2022-06-15
    相关资源
    最近更新 更多