赞
踩
- 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
替换为您要处理的图像文件名。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。