You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB

  1. from numba import jit, njit
  2. import numpy as np
  3. @njit(fastmath=True)
  4. def numba_image_to_pointcloud(depth_image, bounding_box, camera_matrix):
  5. x_min, y_min, x_max, y_max = bounding_box
  6. h, w = depth_image.shape
  7. # check and correct the bounding box to be within the rgb_image
  8. x_min = int(round(max(0, x_min)))
  9. y_min = int(round(max(0, y_min)))
  10. x_max = int(round(min(w-1, x_max)))
  11. y_max = int(round(min(h-1, y_max)))
  12. if x_max < x_min:
  13. x_max = x_min
  14. if y_max < y_min:
  15. y_max = y_min
  16. f_x = camera_matrix[0,0]
  17. c_x = camera_matrix[0,2]
  18. f_y = camera_matrix[1,1]
  19. c_y = camera_matrix[1,2]
  20. out_w = (x_max - x_min) + 1
  21. out_h = (y_max - y_min) + 1
  22. points = np.empty((out_h * out_w, 3), dtype=np.float32)
  23. i = 0
  24. x = x_min
  25. while x <= x_max:
  26. y = y_min
  27. while y <= y_max:
  28. z_3d = depth_image[y,x] / 1000.0
  29. x_3d = ((x - c_x) / f_x) * z_3d
  30. y_3d = ((y - c_y) / f_y) * z_3d
  31. points[i] = (x_3d, y_3d, z_3d)
  32. i += 1
  33. y += 1
  34. x += 1
  35. return points