【python】opencv教程CV2模块——图片处理,剪切缩放旋转

opencv教程CV2模块——图片处理,剪切缩放旋转

import cv2
import numpy as np

# 读取照片
img = cv2.imread('./imgg/1.jpeg')

# 沿着横纵轴放大1.6倍,然后平移(-150,-240),最后沿原图大小截取,等效于裁剪并放大
M_crop_pic = np.array([
    [1.6, 0, -150],
    [0, 1.6, -240]
], dtype=np.float32)

img_pic = cv2.warpAffine(img, M_crop_pic, (960, 540))
cv2.imwrite('./out/lanka_pic.jpg', img_pic)

# x轴的剪切变换,角度15°
theta = 15 * np.pi / 180
M_shear = np.array([
    [1, np.tan(theta), 0],
    [0, 1, 0]
], dtype=np.float32)

img_sheared = cv2.warpAffine(img, M_shear, (400, 600))
cv2.imwrite('./out/lanka_safari_sheared.jpg', img_sheared)

# 顺时针旋转,角度15°
M_rotate = np.array([
    [np.cos(theta), -np.sin(theta), 0],
    [np.sin(theta), np.cos(theta), 0]
], dtype=np.float32)

img_rotated = cv2.warpAffine(img, M_rotate, (400, 600))
cv2.imwrite('./out/lanka_safari_rotated.jpg', img_rotated)

# 某种变换,具体旋转+缩放+旋转组合可以通过SVD分解理解
M = np.array([
    [1, 1.5, -400],
    [0.5, 2, -100]
], dtype=np.float32)

img_transformed = cv2.warpAffine(img, M, (400, 600))
cv2.imwrite('./out/lanka_safari_transformed.jpg', img_transformed)
原文地址:https://www.cnblogs.com/helenlee01/p/12707294.html