You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何合并平移与旋转仿射矩阵,实现单次warpAffine操作?

Merging Translation and Rotation Affine Transformations in OpenCV/Numpy

Great question! Combining these two affine transformations into one is totally doable, and it’ll cut down on redundant computation by only running cv2.warpAffine once. Let’s walk through how to pull this off, with clear math context and code examples.

Quick Background: Affine Transforms in OpenCV

OpenCV uses 2x3 affine matrices for transformations, but these are shorthand for 3x3 homogeneous coordinate matrices. The extra row lets us handle translations (which can’t be represented with 2x2 matrices alone).

For any point (x, y), we represent it as a homogeneous vector (x, y, 1), then multiply by the 3x3 matrix to get the transformed point.

Step 1: Convert Your Existing Matrices to Homogeneous Form

First, we’ll expand your 2x3 translation and rotation matrices into full 3x3 matrices:

import numpy as np
import cv2

# Your original parameters
img = ...  # Replace with your input image
y_size, x_size = img.shape[:2]

# Original translation matrix
M_shift = np.float32([[1, 0, 20], [0, 1, 10]])
# Original rotation matrix
center_of_rot = (500, 500)
angle = 1.23
scale = 1.0
M_rot = cv2.getRotationMatrix2D(center_of_rot, angle, scale)

# Convert to 3x3 homogeneous matrices
M_shift_homo = np.vstack([M_shift, [0, 0, 1]])
M_rot_homo = np.vstack([M_rot, [0, 0, 1]])

Step 2: Multiply the Matrices (Order Is Critical!)

Since you’re applying translation first, then rotation, the combined transformation matrix is the product of the rotation matrix and the translation matrix (matrix multiplication order is reversed from the order of operations).

Here’s why: If P is your original point:

  1. Translate to get P1 = M_shift(P)
  2. Rotate to get P2 = M_rot(P1) = M_rot(M_shift(P)) = (M_rot × M_shift)(P)

So we multiply the matrices like this:

# Compute combined homogeneous matrix
M_combined_homo = M_rot_homo @ M_shift_homo

# Convert back to OpenCV's 2x3 format
M_combined = M_combined_homo[:2, :]

Step 3: Run a Single WarpAffine

Now use the combined matrix to perform your transformation in one step:

transformed = cv2.warpAffine(img, M_combined, (y_size, x_size), flags=cv2.INTER_LANCZOS4)

Double-Check the Result

To confirm this works, you can compare the transformed image to your original rotated image—they should be identical (minus tiny floating-point errors that won’t be visible to the eye).

内容的提问来源于stack exchange,提问作者Mihail

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:47:42