在图像中求多边形区域内平均值(python)

bitmap是网络求出的分数图,

_box是多边形位置坐标。

(1)先求出box所在的最小外接矩形位置(xmin, ymin, ymin, ymax);

(2)生成此外接矩形大小的mask;

(3)为了在此mask填充box多边形,需要将box移动到左上角;

(4)在bitmap裁剪对应的外接矩形,再与mask对应起来求均值。

def box_score_fast(bitmap, _box):
    """
    在bitmap分数图上,求多边形box位置上的平均值。
    Args:
        bitmap: score map
        _box: loc. (num, 2). 字符轮廓点集: [[x,y], [x,y], ...]
                    eg. [[166 963], [171 979], [488 968], [479 956], [178 955]]

    Returns:

    """
    h, w = bitmap.shape[:2]
    box = _box.copy()
    xmin = np.clip(np.floor(box[:, 0].min()).astype(np.int32), 0, w - 1)  # 左上
    xmax = np.clip(np.ceil(box[:, 0].max()).astype(np.int32), 0, w - 1)
    ymin = np.clip(np.floor(box[:, 1].min()).astype(np.int32), 0, h - 1)  # 右下
    ymax = np.clip(np.ceil(box[:, 1].max()).astype(np.int32), 0, h - 1)

    mask = np.zeros((ymax - ymin + 1, xmax - xmin + 1), dtype=np.uint8)  # box大小的mask,此时mask左上边坐标是(0,0)
    box[:, 0] = box[:, 0] - xmin  # box位置整体向左平移
    box[:, 1] = box[:, 1] - ymin  # box位置整体向上平移。这样box的位置也是相对于(0,0)开始的。
    cv2.fillPoly(mask, box.reshape(1, -1, 2).astype(np.int32), 1)  # 在mask图上box位置填充1.
    return cv2.mean(bitmap[ymin:ymax + 1, xmin:xmax + 1], mask)[0]  # roi bitmap + mask

参考:mmocr/models/textdet/postprocess/utils.py

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mr.Q

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

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

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

打赏作者

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

抵扣说明:

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

余额充值