旋转图片和坐标变换

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度

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值