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

相关推荐
Moshow郑锴4 小时前
人工智能中的(特征选择)数据过滤方法和包裹方法
人工智能
TY-20255 小时前
【CV 目标检测】Fast RCNN模型①——与R-CNN区别
人工智能·目标检测·目标跟踪·cnn
CareyWYR6 小时前
苹果芯片Mac使用Docker部署MinerU api服务
人工智能
失散136 小时前
自然语言处理——02 文本预处理(下)
人工智能·自然语言处理
mit6.8246 小时前
[1Prompt1Story] 滑动窗口机制 | 图像生成管线 | VAE变分自编码器 | UNet去噪神经网络
人工智能·python
sinat_286945196 小时前
AI应用安全 - Prompt注入攻击
人工智能·安全·prompt
迈火8 小时前
ComfyUI-3D-Pack:3D创作的AI神器
人工智能·gpt·3d·ai·stable diffusion·aigc·midjourney
Moshow郑锴8 小时前
机器学习的特征工程(特征构造、特征选择、特征转换和特征提取)详解
人工智能·机器学习
CareyWYR9 小时前
每周AI论文速递(250811-250815)
人工智能
AI精钢9 小时前
H20芯片与中国的科技自立:一场隐形的博弈
人工智能·科技·stm32·单片机·物联网