图像仿射变换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 分钟前
Python 装饰器:代码功能的优雅增强(二十九)
人工智能·python·面试
_一条咸鱼_2 分钟前
Python 文件处理(二十一)
人工智能·python·面试
_一条咸鱼_3 分钟前
Python函数的基本使用(二十三)
人工智能·python·面试
_一条咸鱼_4 分钟前
Python 之函数 Type - hinting(二十四)
人工智能·python·面试
_一条咸鱼_4 分钟前
Python 之函数的参数(二十五)
人工智能·python·面试
飞哥数智坊42 分钟前
AI协同研发探索:Claude Code 初试
人工智能
szxinmai主板定制专家1 小时前
国产RK3568+FPGA以 ‌“实时控制+高精度采集+灵活扩展”‌ 为核心的解决方案
大数据·运维·网络·人工智能·fpga开发·机器人
訾博ZiBo1 小时前
AI日报 - 2025年04月25日
人工智能
jndingxin1 小时前
OpenCV 图形API(52)颜色空间转换-----将 NV12 格式的图像数据转换为 RGB 格式的图像
人工智能·opencv·计算机视觉
华为云PaaS服务小智1 小时前
《重塑AI应用架构》系列: Serverless与MCP融合创新,构建AI应用全新智能中枢
人工智能·架构·serverless·华为云