3

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.

I'm using OpenCV 2.4.3 and Python 2.7.3

Please help me!

Community
  • 1
  • 1
Jeep87c
  • 1,050
  • 16
  • 36

2 Answers2

3

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])) 
Geoff
  • 7,935
  • 3
  • 35
  • 43
1

Change covariance toH = reduce(lambda s, a: s + np.outer(a[0], a[1]), zip(src - src_mean, dst - dst_mean), np.zeros((3,3))) for python3 in previous post. Can't comment bacause of reputation score.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33576662) – Julia Jan 10 '23 at 15:14