图像仿射变换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 替换为您要处理的图像文件名。

相关推荐
玄米乌龙茶1234 分钟前
LLM成长笔记(五):提示词工程与模型调用
人工智能·笔记
h64648564h8 分钟前
CANN 昇腾 FP16 vs FP32 精度博弈:深度学习数值精度实战指南
人工智能·深度学习
霸道流氓气质10 分钟前
Spring AI 多工具链式调用(Tool Chain)极简实战
java·人工智能·spring
不脱发的程序猿13 分钟前
嵌入式软件工程师,怎么把 AI 工具用顺手?
人工智能·单片机·嵌入式硬件·嵌入式
莞凰17 分钟前
昇腾CANN的“御剑飞行“:ATB仓库探秘
人工智能·flutter·transformer
心中有国也有家29 分钟前
hccl 架构拆解:昇腾集合通信库到底在做什么?
人工智能·经验分享·笔记·分布式·算法·架构
這花開嗎39 分钟前
试了一圈配音网站,说说我的感受
人工智能·语音识别
w_t_y_y42 分钟前
AI应用demo(二)打造个人的code agent
人工智能·语音识别
Raink老师1 小时前
【AI面试临阵磨枪-60】微服务下 AI 能力如何封装、网关、限流、监控
人工智能·微服务·面试
ApiHug1 小时前
Mintlify、Stainless & ApiHug 在AI 时代的战略意义
人工智能