图像仿射变换Opencv实现

python 复制代码
import cv2
import numpy as np

# 读取图像
image = cv2.imread('input_image.jpg')

# 图像宽度和高度
height, width = image.shape[:2]

# 旋转
angle = 45
rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), angle, 1)
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))

# 平移
x_translation = 100
y_translation = 50
translation_matrix = np.float32([[1, 0, x_translation], [0, 1, y_translation]])
translated_image = cv2.warpAffine(image, translation_matrix, (width, height))

# 缩放
scale_factor = 0.5
scaled_image = cv2.resize(image, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)

# 错切
shear_matrix = np.float32([[1, 0.5, 0], [0.5, 1, 0]])
sheared_image = cv2.warpAffine(image, shear_matrix, (width, height))

# 显示结果
cv2.imshow('Original Image', image)
cv2.imshow('Rotated Image', rotated_image)
cv2.imshow('Translated Image', translated_image)
cv2.imshow('Scaled Image', scaled_image)
cv2.imshow('Sheared Image', sheared_image)

# 等待按键
cv2.waitKey(0)
cv2.destroyAllWindows()

这段代码演示了如何对图像进行旋转、平移、缩放和错切等仿射变换,并使用 OpenCV 库进行实现。请确保将 input_image.jpg 替换为您要处理的图像文件名。

相关推荐
杨凯凡1 小时前
IT日报|2026-11-13:GitHub 大规模凭据攻击、Signal 形式化验证、Claude 新模型与 AI 安全诉讼
人工智能·安全·github
一个天蝎座 白勺 程序猿2 小时前
别让收藏夹替你学习:我搭AI知识体系的完整方法,脚本直接拿去用
人工智能·学习·ai·ai学习
伞伞悦读2 小时前
【第83期】特征选择中的验证泄漏排查的实验与排查
人工智能·深度学习·机器学习
用户697625682562 小时前
用 Alook 把本机编程 agent 拉进频道:Pi + MiniMax 实操
人工智能
P1Browser2 小时前
账号触发平台安全审核?拆解标准申诉流程与材料清单
大数据·人工智能·安全·多账号管理·指纹浏览器·账户安全·运营合规
丘山录2 小时前
本地部署 Edge0-145B,98GB 放 NAS 慢 51 倍
人工智能
一只废狗狗狗狗狗狗狗狗狗3 小时前
自监督式学习
人工智能·深度学习·机器学习·bert
武子康3 小时前
给 H3-World 叠加 Turbo,动作 LoRA 为什么还要重新验收?
人工智能·agent·claude
Dawson Zhu3 小时前
【分布式训练④】张量并行与序列并行:把单层参数切碎分到多张卡
人工智能·语言模型·架构·aigc·agi