美文网首页拾贝
旋转图片并保证标注点随图片旋转

旋转图片并保证标注点随图片旋转

作者: Cat丹 | 来源:发表于2020-04-23 17:46 被阅读0次

    图片旋转是机器学习中常用的数据增广方式,如何优雅地旋转并保证图片内容的完整是一个比较复杂的问题。如果再考虑点,问题又变复杂了。以前我都是自己写点的变换公式,今天get到一个不错的函数,cv2.transform,只要获得旋转矩阵就可以直接求点坐标了,幸福来得太突然,记录下下!

    旋转矩阵

    link

    图旋转

    link

    
    def rotate_bound(image, angle):
        # grab the dimensions of the image and then determine the
        # center
        (h, w) = image.shape[:2]
        (cX, cY) = (w // 2, h // 2)
     
        # grab the rotation matrix (applying the negative of the
        # 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
     
        # perform the actual rotation and return the image
        return cv2.warpAffine(image, M, (nW, nH))
    
    

    点旋转

    直接用cv2.transform(),点与图对应,为绝对坐标

    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    pts=cv2.transform(np.asarray(pts,dtype=np.float64).reshape((-1,1,2)),M)
    

    相关文章

      网友评论

        本文标题:旋转图片并保证标注点随图片旋转

        本文链接:https://www.haomeiwen.com/subject/gjoaihtx.html