Каков альтернативный подход к циклическому анализу при сравнении массивов? - PullRequest
0 голосов
/ 08 апреля 2020

Я пытаюсь объединить два массива lidar_PC (mx 5) и depth_PC (nx 5) на основе первых двух столбцов. Оба значения (фи и тета) должны быть одинаковыми, чтобы точка была добавлена ​​в новый массив final_PC. Если фи и тета совпадают, данные из других 3 столбцов также сохраняются.

Ниже приведен пример двух массивов, объединенных в выходной массив, где зеленые строки - это строки, которые нужно добавить в выходной массив (поскольку phi и theta одинаковы):

example arrays

В данный момент я просто перебираю массив, что по понятным причинам занимает слишком много времени. Что было бы лучше / быстрее сделать это?


'''Combine lidar and camera point clouds'''

def combined_PC(depth_im, lidar_data, cam_labels, lidar_labels, color_im, plot = False):
    '''load data'''
    pc = point_cloud_cam(depth_im, cam_labels, color_im)
    depth_PC = polar_cam(pc)
    lidar_PC = polar_lidar(lidar_data, lidar_labels)

    '''
    set variables
    '''
    theta_cam = depth_PC[:,0]
    phi_cam = depth_PC[:,1]
    r_cam = depth_PC[:,2]
    label_cam = depth_PC[:,3]
    label_lane = depth_PC[:,4]

    new_data = [] #this will contain the merged information
    count = 0
    phi_old = -1
    theta_old = -1

    '''
    Only add to the merged point cloud the combination of phi and thetas
    that appear in both camera and lidar
    '''
    start = timer()
    for i in range(len(lidar_PC)):
        [theta,phi,r, label_l] = lidar_PC[i]

        if phi == phi_old and theta == theta_old:
            count += 1
            '''In case there is more than one radius for a set of phi and theta (len(depth_t2)>1)'''
            new_data.append([theta,phi,r,depth_t2[count][2],label_l, depth_t2[count][3],depth_t2[count][4]])

        else:

            if phi in phi_cam:
                idx_d = (depth_PC[:,1]==phi)
                depth_t = depth_PC[idx_d,:]

                if theta in theta_cam:
                    idx_d2 = (depth_t[:,0]==theta)
                    depth_t2 = depth_t[idx_d2,:] #depth_t2 can have more than one entry due to rounding to 2 decimal places

                    if len(depth_t2) != 0:
                        '''
                        Add to new array in the format (Theta, phi, r_lidar, r_camera, label_lidar, label_camera, lane_label)
                        '''
                        new_data.append([theta,phi,r,depth_t2[0][2], label_l, depth_t2[0][3], depth_t2[0][4]])

                        theta_old,phi_old = theta, phi #keep track of the values added to new_data
    end = timer()
    print("Time for the loop = ", end - start)

    final_PC = np.array(new_data)

1 Ответ

0 голосов
/ 08 апреля 2020
lidar = np.array([[1.1,2.03,28,2,0],[1.1,2.05,34,3,0],[1.2,2.09,5,1,0]])
depth = np.array([[1.1,2.05,33,7,0],[1.2,3.05,23,1,0],[1.5,1.95,5,5,1],[1.2,2.09,5,1,0]])

match = np.array([all(depth[:,0:2][i] == lidar[:,0:2][j]) for i in range(depth.shape[0]) for j in range(lidar.shape[0])]).reshape(depth.shape[0],lidar.shape[0]) # find where they match
#match = = [[all(depth[:,0:2][i] == lidar[:,0:2][j]) for j in range(lidar.shape[0])] for i in range(depth.shape[0])] # without reshaping

depth_idx , lidar_idx = np.where(match) # get the indices of the match

output = np.c_[lidar[lidar_idx] , depth[depth_idx][:,2:]]

другой подход для получения match через широковещательную рассылку и итерации только по 1 массиву

match = np.array([np.sum(depth[:,0:2][i] - lidar[:,0:2],axis=1) for i in range(depth.shape[0])])
depth_idx , lidar_idx = np.where(match==0)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...