求解仿射变换矩阵

仿射变换是图形学中经常用到的方法,通常但是仿射变换的系数是未知的,需要找到变换前后的三对对应点进行求解。

python 复制代码
from affine import Affine
import numpy as np

参考文献
矩阵最小二乘法求解仿射变换矩阵

python 复制代码
def solve_affine(init_points, goal_points) -> Affine:
    # 分别整理成上面分析的6x6和6x1的矩阵
    # 先定义变量保留6个坐标的值
    (ax, ay), (bx, by), (cx, cy) = init_points
    (ax1, ay1), (bx1, by1), (cx1, cy1) = goal_points

    A = np.array([
        [ax, ay, 1, 0, 0, 0],
        [0, 0, 0, ax, ay, 1],
        [bx, by, 1, 0, 0, 0],
        [0, 0, 0, bx, by, 1],
        [cx, cy, 1, 0, 0, 0],
        [0, 0, 0, cx, cy, 1]
    ])

    B = np.array([ax1, ay1, bx1, by1, cx1, cy1]).reshape(6, 1)  # 比手写6X1矩阵要省事
    M = np.linalg.inv(A.T @ A) @ A.T @ B  # 套公式
    
    M=M.flatten().tolist()
    return Affine(*M) #转换成Affine对象
python 复制代码
A = [[0,0], [50, 0], [50, 50]]
B = [[30, 30], [130, 30], [130, 130]]

transform=solve_affine(A,B)

for a,b in zip(A,B):
    print(a,b,transform*a)
相关推荐
做cv的小昊5 小时前
【TJU】应用统计学——第五周作业(3.1 假设检验的基本思想、3.2 单个正态总体参数的假设检验)
学习·线性代数·机器学习·数学建模·矩阵·概率论·tju
Hello.Reader6 小时前
为什么学线性代数(一)
线性代数·算法·机器学习
书中玉6 小时前
矩阵行最简形唯一
线性代数·矩阵·行最简形
kyle~8 小时前
工程数学---Eigen库(C++唯一标配线性代数库)
开发语言·c++·线性代数
披着羊皮不是狼8 小时前
矩阵乘加速@CPU
线性代数·矩阵
Hello.Reader9 小时前
什么是线性代数(二)
线性代数
kyle~10 小时前
工程数学---机器人变化矩阵求解
网络·矩阵·机器人
kcuwu.13 小时前
Python 数据分析实战:NumPy、Pandas、Matplotlib 融合
python·数据分析·numpy
6Hzlia1 天前
【Hot 100 刷题计划】 LeetCode 48. 旋转图像 | C++ 矩阵变换题解
c++·leetcode·矩阵
star learning white1 天前
线性代数1
线性代数