求解仿射变换矩阵

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

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)
相关推荐
爱喝纯牛奶的柠檬3 小时前
基于STM32的4*4矩阵软键盘驱动
stm32·嵌入式硬件·矩阵
Frostnova丶4 小时前
LeetCode 48 & 1886.矩阵旋转与判断
算法·leetcode·矩阵
jerryinwuhan7 小时前
python数据挖掘基础
python·数据挖掘·numpy
阿Y加油吧8 小时前
力扣打卡——搜索二维矩阵、相交链表
线性代数·leetcode·矩阵
qq_283720059 小时前
WebGL基础教程(十四):投影矩阵深度解析——正交 vs 透视,从公式推导到实战
线性代数·矩阵·webgl·正交·投影
We་ct10 小时前
LeetCode 74. 搜索二维矩阵:两种高效解题思路
前端·算法·leetcode·矩阵·typescript·二分查找
我就想睡到自然醒10 小时前
【论文翻译】CA注意力机制原文翻译 Coordinate Attention for Efficient Mobile Network Design
图像处理·人工智能·计算机视觉·目标跟踪·图像分类
Tisfy11 小时前
LeetCode 1886.判断矩阵经轮转后是否一致:模拟
算法·leetcode·矩阵·题解·模拟
Zaly.1 天前
【Python刷题】LeetCode 1727 重新排列后的最大子矩阵
算法·leetcode·矩阵
SugarFreeOixi1 天前
MATLAB绘图风格记录NP类型
python·matlab·numpy