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

相关推荐
shayudiandian1 小时前
深度学习中的激活函数全解析:该选哪一个?
人工智能·深度学习
视界先声2 小时前
如何选择合适的养老服务机器人
人工智能·物联网·机器人
RPA机器人就选八爪鱼2 小时前
RPA财务机器人:重塑财务效率,数字化转型的核心利器
大数据·数据库·人工智能·机器人·rpa
腾讯WeTest2 小时前
Al in CrashSight ——基于AI优化异常堆栈分类模型
人工智能·分类·数据挖掘
凯子坚持 c2 小时前
openGauss向量数据库技术演进与AI应用生态全景
数据库·人工智能
嵌入式-老费3 小时前
自己动手写深度学习框架(从网络训练到部署)
人工智能·深度学习
温柔哥`3 小时前
HiProbe-VAD:通过在免微调多模态大语言模型中探测隐状态实现视频异常检测
人工智能·语言模型·音视频
强化学习与机器人控制仿真3 小时前
字节最新开源模型 DA3(Depth Anything 3)使用教程(一)从任意视角恢复视觉空间
人工智能·深度学习·神经网络·opencv·算法·目标检测·计算机视觉
机器之心4 小时前
如视发布空间大模型Argus1.0,支持全景图等多元输入,行业首创!
人工智能·openai