2D笛卡尔平面变换,从一个平面映射到另一个。

huangapple go评论74阅读模式
英文:

2d cartesian plane transformation, mapping from one plane to other

问题

如果你有4个点(xi, yi); 1<=i<=4
并且想要将它们进行投影变换到新的点(Xi, Yi),你应该如何操作?

可以通过矩阵来实现... 详细解释请参考这里的CSS变换

我刚刚自己回答了这个问题,所以希望能对某人有所帮助。

英文:

If you have 4 points (xi,yi); 1<=i<=4
And you want to transform them like projective transformation to new points (Xi,Yi), how do you do it?

It can be done with matrixes... Refer here for indepth explanation transformation for css

I just answered the question myself so yeah...I hope that it helps somebody.

答案1

得分: -1

import numpy as np

def derive_transformation_matrix(points, mapped_points):
    # 创建线性方程组的矩阵A
    A = []
    for i in range(len(points)):
        xi, yi = points[i]
        ui, vi = mapped_points[i]
        A.append([xi, yi, 1, 0, 0, 0, -ui * xi, -ui * yi, -ui])
        A.append([0, 0, 0, xi, yi, 1, -vi * xi, -vi * yi, -vi])
    
    # 将A转换为numpy数组
    A = np.array(A)

    # 使用奇异值分解来解决线性方程组
    _, _, V = np.linalg.svd(A)
    H = V[-1] / V[-1, -1]

    # 将H重新形状为3x3的变换矩阵
    H_matrix = H.reshape((3, 3))

    return H_matrix

def apply_transformation(matrix, x, y):
    # 创建齐次点坐标 (x, y, 1)
    point = np.array([x, y, 1])

    # 使用矩阵乘法进行变换
    transformed_point = np.dot(matrix, point)

    # 将变换后的点坐标转换为笛卡尔坐标 (u, v)
    u = transformed_point[0] / transformed_point[2]
    v = transformed_point[1] / transformed_point[2]

    return round(u, 8), round(v, 8)  # 四舍五入到8位小数

# 给出4个源点和目标点
# 给定的点和映射点
points = [(0, 0), (1, 0), (1, -1), (0, -1)]
mapped_points = [(0, 0), (2, 0), (2, -2), (0, -2)]

# 推导变换矩阵
transformation_matrix = derive_transformation_matrix(points, mapped_points)

# 给定点 (x, y)
x = 32.5
y = 32.5

# 应用变换以获得新的坐标 (u, v)
point = np.dot(transformation_matrix, [x, y, 1])
point /= point[2]

# 将变换后的坐标 (u, v) 四舍五入到8位小数
u, v = round(point[0], 8), round(point[1], 8)

print(f"原始点坐标: ({x}, {y})")
print(f"变换后的点坐标: ({u}, {v})")
英文:
import numpy as np
def derive_transformation_matrix(points, mapped_points):
# Create the matrix A for the linear equations
A = []
for i in range(len(points)):
xi, yi = points[i]
ui, vi = mapped_points[i]
A.append([xi, yi, 1, 0, 0, 0, -ui * xi, -ui * yi, -ui])
A.append([0, 0, 0, xi, yi, 1, -vi * xi, -vi * yi, -vi])
# Convert A to a numpy array
A = np.array(A)
# Perform Singular Value Decomposition to solve the linear equations
_, _, V = np.linalg.svd(A)
H = V[-1] / V[-1, -1]
# Reshape H to a 3x3 transformation matrix
H_matrix = H.reshape((3, 3))
return H_matrix

def apply_transformation(matrix, x, y):
# Create the homogeneous point (x, y, 1)
point = np.array([x, y, 1])
# Perform the transformation using matrix multiplication
transformed_point = np.dot(matrix, point)
# Convert the transformed point to Cartesian coordinates (u, v)
u = transformed_point[0] / transformed_point[2]
v = transformed_point[1] / transformed_point[2]
return round(u, 8), round(v, 8)  # Rounding to 8 decimal places

Give 4 points as source and destination

# Given points and mapped_points
points = [(0, 0), (1, 0), (1, -1), (0, -1)]
mapped_points = [(0, 0), (2, 0), (2, -2), (0, -2)]
# Derive the transformation matrix
transformation_matrix = derive_transformation_matrix(points, mapped_points)
# Given point (x, y)
x = 32.5
y = 32.5
# Apply the transformation to get the new coordinates (u, v)
point = np.dot(transformation_matrix, [x, y, 1])
point /= point[2]
# Round the transformed coordinates (u, v) to 8 decimal places
u, v = round(point[0], 8), round(point[1], 8)
print(f&quot;Original Point: ({x}, {y})&quot;)
print(f&quot;Transformed Point: ({u}, {v})&quot;)

huangapple
  • 本文由 发表于 2023年7月20日 21:55:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定