gpt4 book ai didi

matlab - 如何找到两组3D点之间的仿射变换矩阵?

转载 作者:行者123 更新时间:2023-12-02 16:56:32 39 4
gpt4 key购买 nike

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

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

我试过使用matlab提供的imregtform函数和matlab的ABSOR工具。

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不求仿射变换,求相似变换。

最佳答案

首先,3 个点太少无法恢复仿射变换——您需要 4 个点。对于 N 维空间,有一个简单的规则:要明确地恢复仿射变换,您应该知道形成单纯形的 N+1 个点的图像——2D 的三角形,3D 的金字塔等。对于 3 个点,您只能检索 2D仿射变换。您可能会在“Beginner's guide to mapping simplexes affinely”中找到关于为什么会出现这种情况的很好的解释。

关于一些检索算法。恐怕,我不知道 Matlab 是否可以为您提供合适的代码,但我使用了一点 Python,也许这段代码可以提供帮助(抱歉代码风格不好——我是数学家,不是程序员)

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)

此代码演示了如何将仿射变换恢复为矩阵和向量,并测试初始点是否映射到它们应该映射的位置。它基于“Beginner's guide to mapping simplexes affinely”中给出的等式,矩阵恢复在“规范符号的恢复”一节中进行了描述。同一作者发表了“Workbook on mapping simplexes affinely”,其中包含许多此类实际示例。

关于matlab - 如何找到两组3D点之间的仿射变换矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56166088/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com