将一张图片通过水平翻转,竖直翻转,平移,旋转的四张小图平成一张大图,来展示一张图片经过变化后形成的图像数据,增强该图像的多样性。
import cv2
import numpy as np
# 读取图像
image = cv2.imread('result/00020.jpg')
# 获取图像尺寸
height, width = image.shape[:2]
# 定义水平翻转
flipped_image = cv2.flip(image, 1)
# 定义竖直翻转
flipped_vertically_image = cv2.flip(image, 0)
# 定义平移参数
x_offset = 50
y_offset = 100
# 创建一个平移矩阵
translation_matrix = np.float32([[1, 0, x_offset], [0, 1, y_offset]])
# 执行平移操作
translated_image = cv2.warpAffine(image, translation_matrix, (width, height))
# 定义旋转
angle = 45
center = (width // 2, height // 2)
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
# 创建一个空白的大图像
large_image = np.zeros((2*height, 2*width, 3), dtype=np.uint8)
# 将四张小图像放置在大图像上
large_image[:height, :width] = flipped_image
large_image[:height, width:] = flipped_vertically_image
large_image[height:, :width] = translated_image
large_image[height:, width:] = rotated_image
# 保存四张小图像和大图像
cv2.imwrite('result/flipped_image.jpg', flipped_image)
cv2.imwrite('result/flipped_vertically_image.jpg', flipped_vertically_image)
cv2.imwrite('result/translated_image.jpg', translated_image)
cv2.imwrite('result/rotated_image.jpg', rotated_image)
cv2.imwrite('result/large_image.jpg', large_image)
# 显示大图像
cv2.imshow('Combined Images', large_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

被折叠的 条评论
为什么被折叠?



