图片旋转90、180、270度以及标注坐标的变换

代码 

def rotate_im_poly(im, text_polys):
    """
    rotate image with 90 / 180 / 270 degre
    """
    im_w, im_h = im.shape[1], im.shape[0]
    dst_im = im.copy()
    dst_polys = []
    rand_degree_ratio = np.random.rand()
    rand_degree_cnt = 1
    if 0.333 < rand_degree_ratio < 0.666:
        rand_degree_cnt = 2
    elif rand_degree_ratio > 0.666:
        rand_degree_cnt = 3
    for i in range(rand_degree_cnt):
        dst_im = np.rot90(dst_im)
    rot_degree = -90 * rand_degree_cnt
    rot_angle = rot_degree * math.pi / 180.0
    n_poly = text_polys.shape[0]
    cx, cy = 0.5 * im_w, 0.5 * im_h
    ncx, ncy = 0.5 * dst_im.shape[1], 0.5 * dst_im.shape[0]
    for i in range(n_poly):
        wordBB = text_polys[i]
        poly = []
        for j in range(4):
            sx, sy = wordBB[j][0], wordBB[j][1]
            dx = math.cos(rot_angle) * (sx - cx)\
                - math.sin(rot_angle) * (sy - cy) + ncx
            dy = math.sin(rot_angle) * (sx - cx)\
                + math.cos(rot_angle) * (sy - cy) + ncy
            poly.append([dx, dy])
        dst_polys.append(poly)
    dst_polys = np.array(dst_polys, dtype=np.float32)
    return dst_im, dst_polys

注意

  1. 这里图片旋转后没有保持原始size,比如(1280,720,3)的图片旋转90度后size变成(720,1280,3),而不是保持原始size然后四周填白
  2. 代码中计算坐标变换采用的是顺时针计算公式,因此角度rot_degree是乘以-90
  3.  因为旋转后没有保持原始size,所以计算坐标变换最后+ncx是旋转后图片中心点横坐标,而不是加上旋转前中心点横坐标cx

参考

cv2.getRotationMatrix2D的旋转矩阵的正确形式

图像旋转后的还原图像坐标

图像中一个坐标绕图像中心旋转90度、-90度、180度的新坐标求解

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

00000cj

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

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

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

打赏作者

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

抵扣说明:

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

余额充值