Я пытаюсь объединить два массива lidar_PC
(mx 5) и depth_PC
(nx 5) на основе первых двух столбцов. Оба значения (фи и тета) должны быть одинаковыми, чтобы точка была добавлена в новый массив final_PC
. Если фи и тета совпадают, данные из других 3 столбцов также сохраняются.
Ниже приведен пример двух массивов, объединенных в выходной массив, где зеленые строки - это строки, которые нужно добавить в выходной массив (поскольку phi и theta одинаковы):
![example arrays](https://i.stack.imgur.com/6o4Wh.png)
В данный момент я просто перебираю массив, что по понятным причинам занимает слишком много времени. Что было бы лучше / быстрее сделать это?
'''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)