python opencv 旋转图片

旋转图像:
import numpy as np
import argparse
import cv2

#旋转后图像完整,图像会增大
def rotate_bound(image, angle):
    (h, w) = image.shape[:2]
    (cX, cY) = (w // 2, h // 2)

    # angle to rotate clockwise), then grab the sine and cosine
    # (i.e., the rotation components of the matrix)
    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    cos = np.abs(M[0, 0])
    sin = np.abs(M[0, 1])

    # compute the new bounding dimensions of the image
    nW = int((h * sin) + (w * cos))
    nH = int((h * cos) + (w * sin))

    # adjust the rotation matrix to take into account translation
    M[0, 2] += (nW / 2) - cX
    M[1, 2] += (nH / 2) - cY
    img = cv2.warpAffine(image, M, (nW, nH))
    # perform the actual rotation and return the image
    return img
#旋转后图像大小不变,多余会裁剪
def rotate(image, angle, center=None, scale=1.0):
    (h, w) = image.shape[:2]
    if center is None:
        center = (w / 2, h / 2)
    M = cv2.getRotationMatrix2D(center, angle, scale)
    rotated = cv2.warpAffine(image, M, (w, h))
    return rotated

if __name__ == '__main__':

    img_path=r'E:\project\angle_net\128data1204\img_o\train\data1\12340.png'

    image = cv2.imread(img_path)
    cv2.imshow("Original", image)

    # rotated = rotate(image, 45)
    # cv2.imshow("Rotated by 45 Degrees", rotated)

    bbb=rotate_bound(image, 60)

    cv2.imshow("bbb", bbb)

    cv2.waitKey(0)
旋转后的坐标点:

这个也可以参考:
https://blog.csdn.net/jacke121/article/details/107026537  
#点p_x, p_y 围绕center_x, center_y顺时针旋转angle度
import cv2
import math
import numpy as np
import os
 
def get_degree(p1, p0):
    aaa = math.degrees(math.atan2((p1[1] - p0[1]), (p1[0] - p0[0])))
    if aaa < 0:
        aaa = 360 + aaa
    aaa = (aaa + 90)
    if aaa > 360:
        aaa = aaa - 360
    return aaa
 
def rotate_image(box_points, angle, scale=1.):
    c_x=box_points[0][0]
    c_y=box_points[0][1]
    rot_mat = cv2.getRotationMatrix2D((c_x, c_y), angle, scale)
 
    new_box_points = []
    for point in box_points:
        new_points = []
        p_x, p_y = point[2],point[3]
        point1 = np.dot(rot_mat, np.array([p_x, p_y, 1]))
        new_points.append((point1))
        new_box_points.append(new_points)
 
    return new_box_points
 
 
def Srotate(angle, valuex, valuey, pointx, pointy):
    valuex = np.array(valuex)
    valuey = np.array(valuey)
    sRotatex = (valuex - pointx) * math.cos(angle) + (valuey - pointy) * math.sin(angle) + pointx
    sRotatey = (valuey - pointy) * math.cos(angle) - (valuex - pointx) * math.sin(angle) + pointy
 
    return sRotatex, sRotatey
 
 
def get_point(angle_boxes):
 
    new_box_points = rotate_image(angle_boxes, -angle)
    print(new_box_points)
    return new_box_points
    new_angle_boxes = []
    for index, box_point in enumerate(new_box_points):
        box_point = np.asarray(box_point)
        center_p = box_point[1]
 
        center_x = int(center_p[0])
        center_y = int(center_p[1])
 
        new_anglebox = angle_boxes[index]
 
        new_anglebox[0] = center_x
        new_anglebox[1] = center_y
 
        head_center_x = box_point[0][0]
        head_center_y = box_point[0][1]
 
        aaa = math.degrees(math.atan2((head_center_y - center_y), (head_center_x - center_x)))
 
        if aaa < 0:
            aaa = 360 + aaa
        aaa = (aaa + 90)
        if aaa > 360:
            aaa = aaa - 360
 
        new_anglebox[4] = aaa
        new_angle_boxes.append(new_anglebox)
 
    return new_angle_boxes,new_box_points
 
 
if __name__ == '__main__':
    angle_boxes = []
    center_x = 320
    center_y = 240
    p_x, p_y = 320,240 - 68
    angle = 30
 
    angle_boxes.append([center_x, center_y, p_x, p_y])
    #点p_x, p_y 围绕center_x, center_y顺时针旋转angle度
 
    new_box_points = get_point(angle_boxes)
    print("new", new_box_points)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值