opencv官方文档:图像任意角度的旋转,图像的读取和存储

opencv文件的处理官方参考Image file reading and writing:https://docs.opencv.org/4.x/d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56
参考:https://zhuanlan.zhihu.com/p/431530466

# -*- coding: utf-8 -*-

import cv2
import os
import numpy as np
import time

images = 't1.jpg'
images_out = 'out-' + 't1.jpg'
root_path = os.path.abspath(os.path.dirname(__file__))
im_path = os.path.join(root_path, images)
img = cv2.imread(im_path)


def rotate_bound(image, angle):
    (h, w) = image.shape[:2]
    (cX, cY) = (w / 2, h / 2)

    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    cos = np.abs(M[0, 0])
    sin = np.abs(M[0, 1])

    nW = int((h * sin) + (w * cos))
    nH = int((h * cos) + (w * sin))

    M[0, 2] += (nW / 2) - cX
    M[1, 2] += (nH / 2) - cY

    return cv2.warpAffine(image, M, (nW, nH))


res = rotate_bound(img, 0)
print(img == res)
cv2.imwrite(images_out, res)
print(res)

猜你喜欢

转载自blog.csdn.net/qq_15821487/article/details/126484852