rotate函数有两个,imutils.rotate(img,angle)和imutils.rotate_bound(img,angle)
原图(从网上找的,侵删):
第一个旋转后new_img的宽高和img是一样的,所以会导致部分信息丢失:
new_img = imutils.rotate(img, 30)
第二个,旋转角度跟第一个一致,但是反向:
new_img = imutils.rotate_bound(img, 30)
文本框的坐标点假设如下:
rotate_xy 坐标旋转函数:
def rotate_xy(x,y,angle,cx,cy):
"""
点(x,y) 绕(cx,cy)点旋转
"""
angle = angle*pi/180
x_new = (x-cx)*cos(angle) - (y-cy)*sin(angle)+cx
y_new = (x-cx)*sin(angle) + (y-cy)*cos(angle)+cy
return x_new,y_new
按照rotate_xy函数旋转,第一个图旋转之后的坐标是对应的:
dt_boxes = [[[105,15],[165,15],[165,25],[105,25]]]
new_img = imutils.rotate(img, 30)
(image_h, image_w) = img.shape[:2]
cx, cy = image_w / 2, image_h / 2
boxes = [rotate_xy(j[0],j[1],-30,cx,cy) for j in dt_boxes[0]]
旋转80度:
对于第二种rotate_bound,可以看出就是在第一个的基础上,将宽高多出的部分进行位置上的调整:
new_img = imutils.rotate_bound(img, 70)
(image_h, image_w) = img.shape[:2]
cx, cy = image_w / 2, image_h / 2
boxes = [rotate_xy(j[0],j[1],70,cx,cy) for j in dt_boxes[0]]
diffx = (new_img.shape[1]-img.shape[1])/2
diffy = (new_img.shape[0]-img.shape[0])/2
boxes =[(i[0]+diffx,i[1]+ diffy) for i in boxes]
旋转170度