本文介绍了Python中的OpenCV 2.4估计Affine3D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用方法 cv2.estimateAffine3D 但没有成功.这是我的代码示例:

I'm trying to use the method cv2.estimateAffine3D but without success. Here is my code sample :

import numpy as np
import cv2

shape = (1, 4, 3)
source = np.zeros(shape, np.float32)

# [x, y, z]
source[0][0] = [857, 120, 854]
source[0][1] = [254, 120, 855]
source[0][2] = [256, 120, 255]
source[0][3] = [858, 120, 255]
target = source * 10

retval, M, inliers = cv2.estimateAffine3D(source, target)

当我尝试运行此示例时,我收到与其他帖子相同的错误 此处.

When I try to run this sample, I obtain the same error as this other post here.

我使用的是 OpenCV 2.4.3 和 Python 2.7.3

I'm using OpenCV 2.4.3 and Python 2.7.3

请帮帮我!

推荐答案

这是一个已知错误,已在 2.4.4 中修复.

This is a known bug that is fixed in 2.4.4.

http://code.opencv.org/issues/2375

如果您只需要刚性(旋转 + 平移)对齐,这里是标准方法:

If you just need rigid (rotation + translation) alignment, here's the standard method:

def get_rigid(src, dst): # Assumes both or Nx3 matrices
    src_mean = src.mean(0)
    dst_mean = dst.mean(0)
    # Compute covariance
    H = reduce(lambda s, (a,b) : s + np.outer(a, b), zip(src - src_mean, dst - dst_mean), np.zeros((3,3)))
    u, s, v = np.linalg.svd(H)
    R = v.T.dot(u.T) # Rotation
    T = - R.dot(src_mean) + dst_mean # Translation
    return np.hstack((R, T[:, np.newaxis]))

这篇关于Python中的OpenCV 2.4估计Affine3D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:48