如何找到两组3D点之间的仿射变换矩阵? [英] How to find affine transformation matrix between two sets of 3D points?

查看:454
本文介绍了如何找到两组3D点之间的仿射变换矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要注册为视频的每一帧指定的一些3D面部界标.对于此任务,我试图找出为连续帧提供的几个界标坐标之间的转换矩阵.例如,第1帧和第2帧中3个地标的3D坐标为:

I need to register some 3D facial landmarks given for each frame of a video. For this task, I am trying to find out a transformation matrix between a few landmark coordinates given for the consecutive frames. For example, 3D coordinates of 3 landmarks in frame 1 and frame 2 are given as:

frame1 = [2 4 15; 4 15 14; 20 11 7]
frame2 = [16 5 12; 5 7 9; 11 6 19]

我尝试使用matlab提供的imregtform函数和用于matlab的ABSOR工具.

I have tried using imregtform function provided by matlab and ABSOR tool for matlab.

tform = imregtform(frame1, frame2, 'affine','OnePlusOneEvolutionary','MeanSquares');

tform = absor(frame1, frame2)

使用imregtform时发生以下错误:

Error using imregtform>parseInputs (line 261)
The value of 'MovingImage' is invalid. All dimensions of the moving image should be greater than 4.

Error in imregtform (line 124)
parsedInputs = parseInputs(varargin{:});

注意:ABSOR找不到仿射变换,而是找到相似变换.

Note: ABSOR doesn't find affine transformation, it finds similarity transformation.

推荐答案

首先,只有3点太少,无法恢复仿射变换-您需要4点.对于N维空间,有一个简单的规则:要明确地恢复仿射变换,您应该知道N + 1个点的图像,这些图像形成了单纯形-对于2D来说是三角形,对于3D来说是金字塔等.只有3个点,您才可以检索2D仿射变换.您可以在"初学者的仿射简单映射指南中找到很好的解释. .

First of all, 3 points are too little to recover affine transformation -- you need 4 points. For N-dimensional space there is a simple rule: to unambiguously recover affine transformation you should know images of N+1 points that form a simplex --- triangle for 2D, pyramid for 3D, etc. With 3 points you could only retrieve 2D affine transformation. A good explanation of why this is the case you may find in "Beginner's guide to mapping simplexes affinely".

关于某些检索算法.恐怕,我不知道Matlab为您提供适当的代码,但是我稍微使用了Python,也许此代码可以提供帮助(对不起,不好的代码风格-我是数学家,而不是程序员)

Regarding some retrieval algorithm. I'm afraid, I don't know Matlab to provide you with appropriate code, but I worked with Python a little bit, maybe this code can help (sorry for bad codestyle -- I'm mathematician, not programmer)

import numpy as np
# input data
ins = [[1, 1, 2], [2, 3, 0], [3, 2, -2], [-2, 2, 3]]  # <- points
out = [[0, 2, 1], [1, 2, 2], [-2, -1, 6], [4, 1, -3]] # <- mapped to
# calculations
l = len(ins)
B = np.vstack([np.transpose(ins), np.ones(l)])
D = 1.0 / np.linalg.det(B)
entry = lambda r,d: np.linalg.det(np.delete(np.vstack([r, B]), (d+1), axis=0))
M = [[(-1)**i * D * entry(R, i) for i in range(l)] for R in np.transpose(out)]
A, t = np.hsplit(np.array(M), [l-1])
t = np.transpose(t)[0]
# output
print("Affine transformation matrix:\n", A)
print("Affine transformation translation vector:\n", t)
# unittests
print("TESTING:")
for p, P in zip(np.array(ins), np.array(out)):
  image_p = np.dot(A, p) + t
  result = "[OK]" if np.allclose(image_p, P) else "[ERROR]"
  print(p, " mapped to: ", image_p, " ; expected: ", P, result)

此代码演示了如何将仿射变换恢复为矩阵和向量,并测试了初始点已映射到它们应在的位置.它基于"初学者的仿射简单映射初学者指南中给出的等式,描述了矩阵恢复在规范表示法的恢复"部分中.相同的作者发表了"仿射映射单纯形的工作手册",其中包含许多实际示例亲切的.

This code demonstrates how to recover affine transformation as matrix and vector and tests that initial points are mapped to where they should. It is based on equation presented in "Beginner's guide to mapping simplexes affinely", matrix recovery is described in section "Recovery of canonical notation". The same authors published "Workbook on mapping simplexes affinely" that contains many practical examples of this kind.

这篇关于如何找到两组3D点之间的仿射变换矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆