Browse Source

aligned with matlab

zhangkang 9 months ago
parent
commit
98962014c3
3 changed files with 54 additions and 17 deletions
  1. 18 6
      pmd.py
  2. 1 1
      src/recons.py
  3. 35 10
      src/utils.py

+ 18 - 6
pmd.py

@@ -221,7 +221,16 @@ def main(config_path, img_folder):
221 221
     for i in range(n_cam):
222 222
         print('cam_id = ', i)
223 223
         contours_point = get_meshgrid_contour(binary_masks[i], save_path, debug=False)
224
-        world_points, world_points_boundary, world_points_boundary_3 = get_world_points(contours_point, cam_params[i], i, grid_spacing, cfg['d'], erosion_pixels=2, debug=0)
224
+        #world_points, world_points_boundary, world_points_boundary_3 = get_world_points(contours_point, cam_params[i], i, grid_spacing, cfg['d'], erosion_pixels=2, debug=0)
225
+        #world_points_x, world_points_y = np.meshgrid(np.linspace(31.8020, 77.9135, 1), np.linspace(33.5894,79.9621,1))
226
+        min_x, max_x, min_y, max_y = 31.8020, 77.9135-1, 33.5894, 79.9621-1
227
+        meshwidth = 1
228
+        world_points_x, world_points_y = np.meshgrid(np.arange(min_x, max_x + meshwidth, meshwidth), np.arange(min_y, max_y + meshwidth, meshwidth))
229
+        world_points_z = np.zeros_like(world_points_x)-cfg['d']
230
+        #print()
231
+        print('world_points_z = ', world_points_x.reshape(-1, 1).shape, world_points_y.reshape(-1, 1).shape, world_points_z.reshape(-1, 1).shape)
232
+
233
+        world_points = np.hstack((world_points_x.reshape(-1, 1), world_points_y.reshape(-1, 1),  world_points_z.reshape(-1, 1)))
225 234
         camera_points, u_p, v_p = get_camera_points(world_points, cam_params[i], save_path, i, debug=0)
226 235
         point_data = {'x_w': world_points[:, 0], 'y_w': world_points[:, 1], 'z_w': world_points[:, 2], 
227 236
                     'x_c': camera_points[:, 0], 'y_c': camera_points[:, 1], 'z_c': camera_points[:, 2],  
@@ -231,6 +240,8 @@ def main(config_path, img_folder):
231 240
         z_raw,  gradient_xy = reconstruction_cumsum(world_points, camera_points, screen_points, debug=0, smooth=smooth, align=align, denoise=denoise)
232 241
         
233 242
         z_raw_xy = np.round(z_raw[:, :2]).astype(int)
243
+
244
+        print('world_points =', world_points)
234 245
         
235 246
         # # 创建布尔掩码,初始为 True
236 247
         # mask = np.ones(len(z_raw_xy), dtype=bool)
@@ -248,9 +259,9 @@ def main(config_path, img_folder):
248 259
         mask = np.ones(len(z_raw_xy), dtype=bool)
249 260
 
250 261
         # 遍历每个边界点,标记它们在 aligned 中的位置
251
-        for boundary_point in world_points_boundary_3:
252
-            # 标记与当前边界点相同 xy 坐标的行
253
-            mask &= ~np.all(z_raw_xy == boundary_point[:2], axis=1)
262
+        # for boundary_point in world_points_boundary_3:
263
+        #     # 标记与当前边界点相同 xy 坐标的行
264
+        #     mask &= ~np.all(z_raw_xy == boundary_point[:2], axis=1)
254 265
         
255 266
         # 使用掩码过滤出非边界点
256 267
         non_boundary_points = z_raw[mask] 
@@ -312,13 +323,14 @@ def main(config_path, img_folder):
312 323
     total_cloud_point[:,0] = np.round(total_cloud_point[:,0])
313 324
     total_cloud_point[:,1] = np.round(total_cloud_point[:,1])
314 325
     #fitted_points = post_process(total_cloud_point, debug=0)
315
-    fitted_points = post_process_with_grad(img_folder, n_cam, 1)
326
+    test = post_process_with_grad(img_folder, n_cam, 1)
327
+    fitted_points = total_cloud_point
316 328
     #fitted_points = total_cloud_point
317 329
     #align_fitted, _ = align2ref(fitted_points)
318 330
     align_fitted = fitted_points
319 331
     
320 332
     write_point_cloud(os.path.join(img_folder, 'cloudpoint.txt'), np.round(align_fitted[:, 0]-np.mean(align_fitted[:, 0])), np.round(align_fitted[:, 1]-np.mean(align_fitted[:, 1])), align_fitted[:,2]-np.min(align_fitted[:,2]))
321
-    if 1:
333
+    if 0:
322 334
         fig = plt.figure()
323 335
         ax = fig.add_subplot(111, projection='3d')
324 336
 

+ 1 - 1
src/recons.py

@@ -336,7 +336,7 @@ def reconstruction_cumsum(world_points, camera_points, screen_points, debug=Fals
336 336
  
337 337
     normal_vector = np.nan_to_num(normal_vector, nan=0.0)
338 338
 
339
-    #print('normal_vector = ', normal_vector[0])
339
+    #print('normal_vector = ', normal_vector)
340 340
 
341 341
     # 计算法向量在X方向的梯度
342 342
     gradient_x = normal_vector[0]

+ 35 - 10
src/utils.py

@@ -433,6 +433,7 @@ def get_camera_points(world_points, world_mat_contents, save_path, cam_id, debug
433 433
         v_p_array.append(v_p)
434 434
     
435 435
     x_c_array, y_c_array, z_c_array, u_p_array, v_p_array = np.array(x_c_array), np.array(y_c_array), np.array(z_c_array), np.array(u_p_array), np.array(v_p_array)
436
+    #print('camera_points =', np.mean(x_c_array), np.mean(y_c_array), np.mean(z_c_array))
436 437
     camera_points = np.column_stack((x_c_array, y_c_array, z_c_array))
437 438
 
438 439
     if debug:
@@ -470,15 +471,24 @@ def get_screen_points(point_data, x_phase_unwrapped, y_phase_unwrapped, screen_p
470 471
     Rs2w = screen_to_world['screen_to_world_rotation']
471 472
     Ts2w = screen_to_world['screen_to_world_translation']
472 473
 
474
+    #checked
475
+    #print('Rs2w = ', Rs2w)
476
+    #print('Ts2w = ', Ts2w)
477
+
473 478
     # 计算 x_phase_unwrapped 中所有非 NaN 值的平均值
474 479
     average_x_phase_unwrapped = np.nanmean(x_phase_unwrapped)
475 480
     average_y_phase_unwrapped = np.nanmean(y_phase_unwrapped)
476 481
 
482
+    #checked
483
+    # print('average_x_phase_unwrapped = ', average_x_phase_unwrapped)
484
+    # print('average_y_phase_unwrapped = ', average_y_phase_unwrapped)
485
+
477 486
     # import pdb; pdb.set_trace()
478 487
     # 计算 value_map 并找到最小值
479 488
     value_map = np.abs(x_phase_unwrapped - average_x_phase_unwrapped) + np.abs(y_phase_unwrapped - average_y_phase_unwrapped)
480 489
     min_index = np.unravel_index(np.nanargmin(value_map), value_map.shape)
481 490
     
491
+    
482 492
     # import pdb; pdb.set_trace()
483 493
     # 选定一个中心相位值作为绝对相位值
484 494
     # abs_phasepoint_picture = np.array(min_index)
@@ -496,7 +506,8 @@ def get_screen_points(point_data, x_phase_unwrapped, y_phase_unwrapped, screen_p
496 506
                             Axis_conversion[1] * cfg['pixelSize_mm'] * (abs_phasepoint_screen[1] - screenorigin_point[1]),
497 507
                             0])
498 508
     abs_phasepoint_world = np.dot(Rs2w, arrow_abs_mm) + Ts2w.T
499
-    
509
+
510
+    #checked
500 511
     print('abs_phasepoint 绝对相位值:')
501 512
     print(abs_phasepoint)
502 513
     print('abs_phasepoint_world 绝对相位值对应的世界坐标系的位置:')
@@ -517,6 +528,9 @@ def get_screen_points(point_data, x_phase_unwrapped, y_phase_unwrapped, screen_p
517 528
         phase_x_now = x_phase_unwrapped[v_now, u_now]  # 注意这里的索引要按照Python的行列顺序,与MATLAB相反 raw
518 529
         phase_y_now = y_phase_unwrapped[v_now, u_now]
519 530
 
531
+        #print('phase_x_now = ', phase_x_now)
532
+        #print('phase_y_now = ', phase_y_now)
533
+
520 534
         #phase_x_now = x_phase_unwrapped[u_now, v_now]  
521 535
         #phase_y_now = y_phase_unwrapped[u_now, v_now]
522 536
 
@@ -539,6 +553,8 @@ def get_screen_points(point_data, x_phase_unwrapped, y_phase_unwrapped, screen_p
539 553
     x_data = np.array(x_data)
540 554
     y_data = np.array(y_data)
541 555
     z_data = np.array(z_data)   
556
+    print('u_p size = ', x_data.shape)
557
+    print('screen_points = ', x_data, np.nanmean(x_data),np.nanmean(y_data),np.nanmean(z_data))
542 558
     screen_points = np.column_stack((x_data, y_data, z_data))
543 559
 
544 560
     if debug:
@@ -1287,15 +1303,23 @@ def post_process_with_grad(img_folder, n_cam, debug=False):
1287 1303
     #print(x_grad_expand.shape, y_grad_expand.shape, x_expand.shape, y_expand.shape)
1288 1304
     # 调用 Southwell 重建函数
1289 1305
     #Z_reconstructed = southwell_integrate_smooth(x_grad_expand, y_grad_expand, x_expand, y_expand, (0, 0))
1306
+    print('x_gradient shape = ', x_gradient.shape)
1307
+    x_gradient = x_gradient.reshape(47, -1)
1308
+    y_gradient = y_gradient.reshape(47, -1)
1309
+    Z_reconstructed = np.cumsum(x_gradient, 1)*1 + np.cumsum(y_gradient, 0)*1
1290 1310
 
1291
-    Z_reconstructed = np.cumsum(x_grad_expand, 1)*1 + np.cumsum(y_grad_expand, 0)*1
1292
-    print('min_z =', np.min(Z_reconstructed))
1311
+   
1312
+    #print('y_grad_expand =', np.cumsum(y_grad_expand, 0))
1313
+    #print('max_z =', np.max(Z_reconstructed))
1293 1314
 
1294
-    #Z_reconstructed = poisson_reconstruct(x_grad_expand, y_grad_expand)
1315
+    #Z_reconstructed = poisson_reconstruct(x_gradient, y_gradient)
1316
+    print('min_z =', np.min(Z_reconstructed))
1317
+    print('max_z =', np.max(Z_reconstructed))
1318
+    print('x_grad_expand =', np.cumsum(x_grad_expand, 1))
1295 1319
     
1296
-    z1_lookup = {(x1, y1): z1 for x1, y1, z1 in np.column_stack((x_expand.reshape(-1,1), y_expand.reshape(-1,1), Z_reconstructed.reshape(-1,1)))}
1320
+    #z1_lookup = {(x1, y1): z1 for x1, y1, z1 in np.column_stack((x_expand.reshape(-1,1), y_expand.reshape(-1,1), Z_reconstructed.reshape(-1,1)))}
1297 1321
 
1298
-    z1_values = np.array([z1_lookup.get((x, y), np.nan) for x, y in np.column_stack((x, y))])
1322
+    #z1_values = np.array([z1_lookup.get((x, y), np.nan) for x, y in np.column_stack((x, y))])
1299 1323
     #fitted_points = post_process(np.column_stack((x,y,z1_values)), debug=0)
1300 1324
 
1301 1325
 
@@ -1308,9 +1332,9 @@ def post_process_with_grad(img_folder, n_cam, debug=False):
1308 1332
         # x_vals = fitted_points[:,0]
1309 1333
         # y_vals = fitted_points[:,1]
1310 1334
         # z_vals = fitted_points[:,2]
1311
-        x_vals = x_expand
1312
-        y_vals = y_expand
1313
-        z_vals = x_grad_expand
1335
+        x_vals = x.reshape(47, -1)
1336
+        y_vals = y.reshape(47, -1)
1337
+        z_vals = Z_reconstructed
1314 1338
         # 绘制3D点云
1315 1339
         ax.scatter(x_vals, y_vals, z_vals, c=z_vals, cmap='viridis', marker='o')
1316 1340
 
@@ -1322,7 +1346,8 @@ def post_process_with_grad(img_folder, n_cam, debug=False):
1322 1346
         plt.show()
1323 1347
     
1324 1348
     #return fitted_points
1325
-    return np.column_stack((x,y,z1_values))
1349
+    #return np.column_stack((x,y,Z_reconstructed))
1350
+    return 0
1326 1351
 
1327 1352
 
1328 1353