python opencv 旋转图片

 

 

# -*- coding:utf-8 -*-
import cv2
import numpy as np
import sys

#origin_img_path = "J:\\Select_pic\\20230203\\1001123756063.jpg"
origin_img_path = "J:\\Select_pic\\20230203\\1001131714512.jpg"


img = cv2.imread(origin_img_path)
# cv2.imshow("original", img)


# 可选,扩展图像,保证内容不超出可视范围
#img = cv2.copyMakeBorder(img, 200, 200, 200, 200, cv2.BORDER_CONSTANT, 0)
#img = cv2.resize(img, (640,360), interpolation = cv2.INTER_AREA)


img_w, img_h = img.shape[0:2]
rotate_anglex = 0
rotate_angley = 30
rotate_anglez = -27 # 是旋转
img_fov = 1
r = 0
def get_rad1(x):
    return x * np.pi / 180
def get_imgWarpR():
    global rotate_anglex,rotate_angley,rotate_anglez,img_fov,img_w,img_h,r
    # 镜头与图像间的距离,21为半可视角,算z的距离是为了保证在此可视角度下恰好显示整幅图像
    z = np.sqrt(img_w ** 2 + img_h ** 2) / 2 / np.tan(get_rad1(img_fov / 2))
    # 齐次变换矩阵
    rx = np.array([[1, 0, 0, 0],[0, np.cos(get_rad1(rotate_anglex)), -np.sin(get_rad1(rotate_anglex)), 0],[0, -np.sin(get_rad1(rotate_anglex)), np.cos(get_rad1(rotate_anglex)), 0, ],[0, 0, 0, 1]], np.float32)
    ry = np.array([[np.cos(get_rad1(rotate_angley)), 0, np.sin(get_rad1(rotate_angley)), 0],[0, 1, 0, 0],[-np.sin(get_rad1(rotate_angley)), 0, np.cos(get_rad1(rotate_angley)), 0, ],[0, 0, 0, 1]], np.float32)
    rz = np.array([[np.cos(get_rad1(rotate_anglez)), np.sin(get_rad1(rotate_anglez)), 0, 0],[-np.sin(get_rad1(rotate_anglez)), np.cos(get_rad1(rotate_anglez)), 0, 0],[0, 0, 1, 0],[0, 0, 0, 1]], np.float32)
    r = rx.dot(ry).dot(rz)
    # 四对点的生成
    pcenter = np.array([img_h / 2, img_w / 2, 0, 0], np.float32)
    p1 = np.array([0, 0, 0, 0], np.float32) - pcenter
    p2 = np.array([img_w, 0, 0, 0], np.float32) - pcenter
    p3 = np.array([0, img_h, 0, 0], np.float32) - pcenter
    p4 = np.array([img_w, img_h, 0, 0], np.float32) - pcenter
    img_dst1 = r.dot(p1)
    img_dst2 = r.dot(p2)
    img_dst3 = r.dot(p3)
    img_dst4 = r.dot(p4)
    list_img_dst = [img_dst1, img_dst2, img_dst3, img_dst4]
    img_org = np.array([[0, 0],[img_w, 0],[0, img_h],[img_w, img_h]], np.float32)
    img_dst = np.zeros((4, 2), np.float32)
    # 投影至成像平面
    for i in range(4):
        img_dst[i, 0] = list_img_dst[i][0] * z / (z - list_img_dst[i][2]) + pcenter[0]
        img_dst[i, 1] = list_img_dst[i][1] * z / (z - list_img_dst[i][2]) + pcenter[1]
    warpR = cv2.getPerspectiveTransform(img_org, img_dst)
    return warpR
def control():
    global rotate_anglex,rotate_angley,rotate_anglez,img_fov,r
    # 键盘控制
    if 27 == c: # Esc quit
        sys.exit()
    if c == ord('w'):
        rotate_anglex += 1
    if c == ord('s'):
        rotate_anglex -= 1
    if c == ord('a'):
        rotate_angley += 1
        print(rotate_angley)
        # dx=0
    if c == ord('d'):
        rotate_angley -= 1
    if c == ord('u'):
        rotate_anglez += 1
    if c == ord('p'):
        rotate_anglez -= 1
    if c == ord('t'):
        img_fov += 1
    if c == ord('r'):
        img_fov -= 1
    if c == ord(' '):
        rotate_anglex = rotate_angley = rotate_anglez = 0
    if c == ord('e'):
        print("======================================")
        print('Rotation Matrix:')
        print(r)
        print('angle alpha(rotate_anglex):')
        print(rotate_anglex)
        print('angle beta(rotate_angley):')
        print(rotate_angley)
        print('dz(rotate_anglez):')
        print(rotate_anglez)
while True:
    warpR = get_imgWarpR()
    result = cv2.warpPerspective(img, warpR, (img_h, img_w))
    cv2.namedWindow('result',2)
    cv2.imshow("result", result)
    c = cv2.waitKey(30)
    control()
cv2.destroyAllWindows()

 

 

 

 

 

######################

posted @ 2023-02-26 15:10  西北逍遥  阅读(46)  评论(0编辑  收藏  举报