seglink.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:seglink 作者: dengdan 项目源码 文件源码
def transform_cv_rect(rects):
    """Transform the rects from opencv method minAreaRect to our rects. 
    Step 1 of Figure 5 in seglink paper

    In cv2.minAreaRect, the w, h and theta values in the returned rect are not convenient to use (at least for me), so 
            the Oriented (or rotated) Rectangle object in seglink algorithm is defined different from cv2.

    Rect definition in Seglink:
        1. The angle value between a side and x-axis is:
            positive: if it rotates clockwisely, with y-axis increasing downwards.
            negative: if it rotates counter-clockwisely.
            This is opposite to cv2, and it is only a personal preference. 

        2. The width is the length of side taking a smaller absolute angle with the x-axis. 
        3. The theta value of a rect is the signed angle value between width-side and x-axis
        4. To rotate a rect to horizontal direction, just rotate its width-side horizontally,
             i.e., rotate it by a angle of theta using cv2 method. 
             (see the method rotate_oriented_bbox_to_horizontal for rotation detail)


    Args:
        rects: ndarray with shape = (5, ) or (N, 5).
    Return:
        transformed rects.
    """
    only_one = False
    if len(np.shape(rects)) == 1:
        rects = np.expand_dims(rects, axis = 0)
        only_one = True
    assert np.shape(rects)[1] == 5, 'The shape of rects must be (N, 5), but meet %s'%(str(np.shape(rects)))

    rects = np.asarray(rects, dtype = np.float32).copy()
    num_rects = np.shape(rects)[0]
    for idx in xrange(num_rects):
        cx, cy, w, h, theta = rects[idx, ...];
        #assert theta < 0 and theta >= -90, "invalid theta: %f"%(theta) 
        if abs(theta) > 45 or (abs(theta) == 45 and w < h):
            w, h = [h, w]
            theta = 90 + theta
        rects[idx, ...] = [cx, cy, w, h, theta]
    if only_one:
        return rects[0, ...]
    return rects
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号