旋转图像:
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)