栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

numpy数组的边界框

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

numpy数组的边界框

通过使用

np.any
将包含非零值的行和列减少到一维向量,您可以大致减少执行时间,而无需使用
np.where
以下命令查找所有非零值的索引:

def bbox1(img):    a = np.where(img != 0)    bbox = np.min(a[0]), np.max(a[0]), np.min(a[1]), np.max(a[1])    return bboxdef bbox2(img):    rows = np.any(img, axis=1)    cols = np.any(img, axis=0)    rmin, rmax = np.where(rows)[0][[0, -1]]    cmin, cmax = np.where(cols)[0][[0, -1]]    return rmin, rmax, cmin, cmax

一些基准:

%timeit bbox1(img2)10000 loops, best of 3: 63.5 µs per loop%timeit bbox2(img2)10000 loops, best of 3: 37.1 µs per loop

将这种方法扩展到3D情况仅涉及沿每对轴执行缩减:

def bbox2_3D(img):    r = np.any(img, axis=(1, 2))    c = np.any(img, axis=(0, 2))    z = np.any(img, axis=(0, 1))    rmin, rmax = np.where(r)[0][[0, -1]]    cmin, cmax = np.where(c)[0][[0, -1]]    zmin, zmax = np.where(z)[0][[0, -1]]    return rmin, rmax, cmin, cmax, zmin, zmax

通过迭代每个轴的唯一组合来执行归约,很容易将其概括为 N个 维度

itertools.combinations

import itertoolsdef bbox2_ND(img):    N = img.ndim    out = []    for ax in itertools.combinations(reversed(range(N)), N - 1):        nonzero = np.any(img, axis=ax)        out.extend(np.where(nonzero)[0][[0, -1]])    return tuple(out)

如果您知道原始边界框角的坐标,旋转角度和旋转中心,则可以通过计算相应的仿射变换矩阵并将其与输入点相乘,直接获得已变换​​边界框角的坐标。坐标:

def bbox_rotate(bbox_in, angle, centre):    rmin, rmax, cmin, cmax = bbox_in    # bounding box corners in homogeneous coordinates    xyz_in = np.array(([[cmin, cmin, cmax, cmax],  [rmin, rmax, rmin, rmax],  [   1,    1,    1,    1]]))    # translate centre to origin    cr, cc = centre    cent2ori = np.eye(3)    cent2ori[:2, 2] = -cr, -cc    # rotate about the origin    theta = np.deg2rad(angle)    rmat = np.eye(3)    rmat[:2, :2] = np.array([[ np.cos(theta),-np.sin(theta)],       [ np.sin(theta), np.cos(theta)]])    # translate from origin back to centre    ori2cent = np.eye(3)    ori2cent[:2, 2] = cr, cc    # combine transformations (rightmost matrix is applied first)    xyz_out = ori2cent.dot(rmat).dot(cent2ori).dot(xyz_in)    r, c = xyz_out[:2]    rmin = int(r.min())    rmax = int(r.max())    cmin = int(c.min())    cmax = int(c.max())    return rmin, rmax, cmin, cmax

与使用

np.any
小型示例数组相比,这要快得多:

%timeit bbox_rotate([25, 75, 25, 75], 45, (50, 50))10000 loops, best of 3: 33 µs per loop

但是,由于此方法的速度与输入数组的大小无关,因此对于较大的数组,它的速度可能要快得多。

将变换方法扩展到3D会稍微复杂一点,因为旋转现在具有三个不同的分量(一个绕x轴,一个绕y轴,一个绕z轴),但是基本方法是相同的:

def bbox_rotate_3d(bbox_in, angle_x, angle_y, angle_z, centre):    rmin, rmax, cmin, cmax, zmin, zmax = bbox_in    # bounding box corners in homogeneous coordinates    xyzu_in = np.array(([[cmin, cmin, cmin, cmin, cmax, cmax, cmax, cmax],   [rmin, rmin, rmax, rmax, rmin, rmin, rmax, rmax],   [zmin, zmax, zmin, zmax, zmin, zmax, zmin, zmax],   [   1,    1,    1,    1,    1,    1,    1,    1]]))    # translate centre to origin    cr, cc, cz = centre    cent2ori = np.eye(4)    cent2ori[:3, 3] = -cr, -cc -cz    # rotation about the x-axis    theta = np.deg2rad(angle_x)    rmat_x = np.eye(4)    rmat_x[1:3, 1:3] = np.array([[ np.cos(theta),-np.sin(theta)],[ np.sin(theta), np.cos(theta)]])    # rotation about the y-axis    theta = np.deg2rad(angle_y)    rmat_y = np.eye(4)    rmat_y[[0, 0, 2, 2], [0, 2, 0, 2]] = (        np.cos(theta), np.sin(theta), -np.sin(theta), np.cos(theta))    # rotation about the z-axis    theta = np.deg2rad(angle_z)    rmat_z = np.eye(4)    rmat_z[:2, :2] = np.array([[ np.cos(theta),-np.sin(theta)],         [ np.sin(theta), np.cos(theta)]])    # translate from origin back to centre    ori2cent = np.eye(4)    ori2cent[:3, 3] = cr, cc, cz    # combine transformations (rightmost matrix is applied first)    tform = ori2cent.dot(rmat_z).dot(rmat_y).dot(rmat_x).dot(cent2ori)    xyzu_out = tform.dot(xyzu_in)    r, c, z = xyzu_out[:3]    rmin = int(r.min())    rmax = int(r.max())    cmin = int(c.min())    cmax = int(c.max())    zmin = int(z.min())    zmax = int(z.max())    return rmin, rmax, cmin, cmax, zmin, zmax

我从这里基本上使用旋转矩阵表达式修改了上面的函数-
我还没有时间编写测试用例,因此请谨慎使用。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/645696.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号