PointCloud.pde 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. class PointCloud {
  2. ArrayList<PVector> points; // array to save points
  3. IntList point_colors; // array to save points color
  4. PVector cloud_mass;
  5. float[] depth;
  6. boolean[] real;
  7. PointCloud() {
  8. // initialize
  9. points = new ArrayList<PVector>();
  10. point_colors = new IntList();
  11. cloud_mass = new PVector(0, 0, 0);
  12. depth = new float[width * height];
  13. real = new boolean[width * height];
  14. }
  15. void generate(PImage rgb_img, PImage depth_img, Transform trans) {
  16. if (depth_img.width != width || depth_img.height != height ||
  17. rgb_img.width != width || rgb_img.height != height) {
  18. println("rgb and depth file dimension should be same with window size");
  19. exit();
  20. }
  21. // clear depth and real
  22. for (int i = 0; i < width * height; i++) {
  23. depth[i] = 0;
  24. real[i] = false;
  25. }
  26. for (int v = 0; v < height; v++)
  27. for (int u = 0; u < width; u++) {
  28. // get depth value (red channel)
  29. color depth_px = depth_img.get(u, v);
  30. depth[v * width + u] = depth_px & 0x0000FFFF;
  31. if (int(depth[v * width + u]) != 0) {
  32. real[v * width + u] = true;
  33. }
  34. point_colors.append(rgb_img.get(u, v));
  35. }
  36. for (int v = 0; v < height; v++)
  37. for (int u = 0; u < width; u++) {
  38. if (int(depth[v * width + u]) == 0) {
  39. interpolateDepth(v, u);
  40. }
  41. // add transformed pixel as well as pixel color to the list
  42. PVector pos = trans.transform(u, v, int(depth[v * width + u]));
  43. points.add(pos);
  44. // accumulate z value
  45. cloud_mass = PVector.add(cloud_mass, pos);
  46. }
  47. }
  48. void fillInDepthAlongPath(float d, Node node) {
  49. node = node.parent;
  50. while (node != null) {
  51. int i = node.row;
  52. int j = node.col;
  53. if (depth[i * width + j] == 0) {
  54. depth[i * width + j] = d;
  55. }
  56. node = node.parent;
  57. }
  58. }
  59. // interpolate
  60. void interpolateDepth(int row, int col) {
  61. if (row < 0 || row >= height || col < 0 || col >= width ||
  62. int(depth[row * width + col]) != 0) {
  63. return;
  64. }
  65. ArrayList<Node> queue = new ArrayList<Node>();
  66. queue.add(new Node(row, col, null));
  67. boolean[] visited = new boolean[width * height];
  68. for (int i = 0; i < width * height; i++) visited[i] = false;
  69. visited[row * width + col] = true;
  70. // Using BFS to Find the Nearest Neighbor
  71. while (queue.size() > 0) {
  72. // pop
  73. Node node = queue.get(0);
  74. queue.remove(0);
  75. int i = node.row;
  76. int j = node.col;
  77. // if current position have a real depth
  78. if (depth[i * width + j] != 0 && real[i * width + j]) {
  79. fillInDepthAlongPath(depth[i * width + j], node);
  80. break;
  81. } else {
  82. // search unvisited 8 neighbors
  83. for (int r = max(0, i - 1); r < min(height, i + 2); r++) {
  84. for (int c = max(0, j - 1); c < min(width, j + 2); c++) {
  85. if (!visited[r * width + c]) {
  86. visited[r * width + c] = true;
  87. queue.add(new Node(r, c, node));
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. // get point cloud size
  95. int size() { return points.size(); }
  96. // get ith position
  97. PVector getPosition(int i) {
  98. if (i >= points.size()) {
  99. println("point position: index " + str(i) + " exceeds");
  100. exit();
  101. }
  102. return points.get(i);
  103. }
  104. // get ith color
  105. color getColor(int i) {
  106. if (i >= point_colors.size()) {
  107. println("point color: index " + str(i) + " exceeds");
  108. exit();
  109. }
  110. return point_colors.get(i);
  111. }
  112. // get cloud center
  113. PVector getCloudCenter() {
  114. if (points.size() > 0) {
  115. return PVector.div(cloud_mass, points.size());
  116. }
  117. return new PVector(0, 0, 0);
  118. }
  119. // merge two clouds
  120. void merge(PointCloud point_cloud) {
  121. for (int i = 0; i < point_cloud.size(); i++) {
  122. points.add(point_cloud.getPosition(i));
  123. point_colors.append(point_cloud.getColor(i));
  124. }
  125. cloud_mass = PVector.add(cloud_mass, point_cloud.cloud_mass);
  126. }
  127. }
  128. class Node {
  129. int row, col;
  130. Node parent;
  131. Node(int row, int col, Node parent) {
  132. this.row = row;
  133. this.col = col;
  134. this.parent = parent;
  135. }
  136. }