目标检测数据增强——裁剪

在图像识别任务中,裁剪是一种比较常用的数据增强方法。通过numpy对图像数组进行截取就可以实现裁剪的功能。

对于像目标检测这类带标注框的图像识别任务,裁剪要确保目标不被裁掉,另外,还要更新标注框的位置,因为经过裁剪后目标在图中的位置发生了变化。以下函数实现了:

  1. 在保证目标点points不被裁掉的情况下,随机裁剪图像。
  2. 计算原图中坐标点points在裁剪后的图像中的位置坐标。
import numpy as np
import random


def random_crop_with_points(image, points):
    """随机裁剪图像,并计算points在裁剪后的图像中的位置.
    Args:
        image: 图像数组
        points: [(x, y), ...],原图像中的坐标点集合
    Return:
        new_img: 裁剪后的图像
        new_pts: [(x, y), ...],原图中的点在裁剪后的图中的位置
    """

    h, w = image.shape[: 2]
    points = np.array(points, np.int32)
    min_x, min_y, max_x, max_y = np.min(points[:, 0]), np.min(points[:, 1]), np.max(points[:, 0]), np.max(points[:, 1])

    t, b, lft, r = (random.randint(0, min_y),
                    random.randint(max_y + 1, h) if max_y + 1 < h else max_y + 1,
                    random.randint(0, min_x),
                    random.randint(max_x + 1, w) if max_x + 1 < w else max_x + 1)

    new_img = image[t: b, lft: r, :]

    new_pts = [[x - lft, y - t] for x, y in points]

    return new_img, new_pts
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值