點雲中的數據增強(Point cloud)

點雲中的數據增強方法

本文基於相機座標展示(kitti中的標籤是相機座標系)

一 旋轉(相機座標系沿y軸旋轉)

import numpy as np

def rotation_points_single_angle(points, angle, axis=0):
    # points: [N, 3]
    rot_sin = np.sin(angle)
    rot_cos = np.cos(angle)
    if axis == 1:
        rot_mat_T = np.array(
            [[rot_cos, 0, -rot_sin], [0, 1, 0], [rot_sin, 0, rot_cos]],
            dtype=points.dtype)
    elif axis == 2 or axis == -1:
        rot_mat_T = np.array(
            [[rot_cos, -rot_sin, 0], [rot_sin, rot_cos, 0], [0, 0, 1]],
            dtype=points.dtype)
    elif axis == 0:
        rot_mat_T = np.array(
            [[1, 0, 0], [0, rot_cos, -rot_sin], [0, rot_sin, rot_cos]],
            dtype=points.dtype)
    else:
        raise ValueError("axis should in range")

    return points @ rot_mat_T



def global_rotation(gt_boxes, points):
    noise_rotation = np.random.uniform(-90, 90)
    points[:, :3] = rotation_points_single_angle(
        points[:, :3], noise_rotation, axis=1)
    gt_boxes[:, :3] = rotation_points_single_angle(
        gt_boxes[:, :3], noise_rotation, axis=1)
    gt_boxes[:, 6] += noise_rotation
    return gt_boxes, points

 輸出:

二 鏡像

x軸鏡像和z軸鏡像

import numpy as np
def random_flip(gt_boxes, points, probability=0.5):
    x = np.random.choice(
         [False, True], replace=False, p=[1 - probability, probability])
    z =  np.random.choice(
         [False, True], replace=False, p=[1 - probability, probability])
    if z:
         gt_boxes[:, 2] = -gt_boxes[:, 2]
         gt_boxes[:, 6] = -gt_boxes[:, 6] + np.pi
         points[:, 2] = -points[:, 2]
    if x:
        gt_boxes[:, 0] = -gt_boxes[:, 0]
        gt_boxes[:, 6] = -gt_boxes[:, 6] + np.pi
        points[:, 0] = -points[:, 0]
    return gt_boxes, points

x軸鏡像:

三 真值提取


if __name__=='__main__':
    import time

    pt   #相機座標系下的點雲
    dets # 對應標籤的八個定點
    
    
    t = time.time()
    x = pt[:, 0]
    y= pt[:, 1]
    z = pt[:, 2]
    dets = det.copy()[0]
    xmin = min(dets[:,0])
    xmax= max(dets[:, 0])
    ymin = min(dets[:, 1])
    ymax = max(dets[:, 1])
    zmin = min(dets[:, 2])
    zmax = max(dets[:, 2])
    print(xmin,xmax,ymin,ymax,zmin,zmax)
    x_filt = np.logical_and((x > xmin), (x < xmax))
    y_filt = np.logical_and((y > ymin), (y < ymax))
    z_filt = np.logical_and((z > zmin), (z < zmax))
    filter = np.logical_and(x_filt, y_filt)
    filter = np.logical_and(filter, z_filt)
    indices = np.argwhere(filter).flatten()
    nn= pt[indices]
    print(time.time()-t)
    # show(pt)
轉換時間(是其他轉換方法的100分之一左右)
0.0018105506896972656

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章