python+opencv摳圖並旋轉(根據座標摳圖)

import cv2
import numpy as np

def subimage(image, center, theta, width, height):
    theta *= np.pi / 180 # convert to rad
    v_x = (np.cos(theta), np.sin(theta))
    v_y = (-np.sin(theta), np.cos(theta))
    s_x = center[0] - v_x[0] * (width / 2) - v_y[0] * (height / 2)
    s_y = center[1] - v_x[1] * (width / 2) - v_y[1] * (height / 2)
    mapping = np.array([[v_x[0],v_y[0], s_x],
                        [v_x[1],v_y[1], s_y]])
    return cv2.warpAffine(image,mapping,(width, height),flags=cv2.WARP_INVERSE_MAP,borderMode=cv2.BORDER_REPLICATE)

image = cv2.imread('1.jpg')
image = subimage(image, center=(746, 1104), theta=0, width=762, height=428)
#cv2.imwrite('patch.jpg', image)
cv2.imshow('patch.jpg',image)
cv2.waitKey(0)
cv2.destroyAllWindows()

  theta:旋轉度數,theta=0表明不旋轉。html

                                                 算法原理python

發現這個cv2.warpAffine是個很強大的函數, 要扣明白了,找到了CSDN博文:圖像的幾何變換
很詳細了, 就不轉了!算法

相關文章
相關標籤/搜索