对原始图像进行以下形式的几何变换:缩放、旋转、平移、裁剪、镜像变换、在同一个窗口内分成六个子窗口来分别显示原图和变换后的图像 python实现
时间: 2023-07-10 09:18:39 浏览: 116
Python实现图像几何变换
好的,我理解了你的问题。以下是对原始图像进行几何变换并在同一个窗口内分成六个子窗口来分别显示原图和变换后的图像的 Python 代码:
```python
import cv2
import numpy as np
# 读取原始图像
img = cv2.imread('original_image.jpg')
# 缩放
scale_percent = 50 # 缩放比例为50%
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
resized_img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
# 旋转
angle = 45 # 旋转角度为45度
M = cv2.getRotationMatrix2D((img.shape[1]//2, img.shape[0]//2), angle, 1)
rotated_img = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
# 平移
tx, ty = 100, 50 # 水平平移100像素,垂直平移50像素
M = np.float32([[1, 0, tx], [0, 1, ty]])
translated_img = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
# 裁剪
x, y, w, h = 100, 100, 200, 200 # 裁剪出左上角坐标为(100, 100),宽度和高度均为200像素的区域
cropped_img = img[y:y+h, x:x+w]
# 镜像变换
mirror_img = cv2.flip(img, 1) # 水平镜像
# 在同一个窗口内分成六个子窗口来分别显示原图和变换后的图像
cv2.namedWindow('original and transformed images', cv2.WINDOW_NORMAL)
cv2.resizeWindow('original and transformed images', 1200, 800)
combined_img = np.zeros((2*img.shape[0]+20, 3*img.shape[1]+40, 3), dtype=np.uint8)
combined_img[0:img.shape[0], 0:img.shape[1], :] = img
combined_img[0:img.shape[0], img.shape[1]+20:2*img.shape[1]+20, :] = resized_img
combined_img[0:img.shape[0], 2*img.shape[1]+40:3*img.shape[1]+40, :] = rotated_img
combined_img[img.shape[0]+20:2*img.shape[0]+20, 0:img.shape[1], :] = translated_img
combined_img[img.shape[0]+20:2*img.shape[0]+20, img.shape[1]+20:2*img.shape[1]+20, :] = cropped_img
combined_img[img.shape[0]+20:2*img.shape[0]+20, 2*img.shape[1]+40:3*img.shape[1]+40, :] = mirror_img
cv2.imshow('original and transformed images', combined_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
请注意:这段代码只是一个示例,实际应用中,可能需要根据具体需求来调整变换参数、子窗口的数量和大小等。
阅读全文