| 242 | return str_faces |
| 243 | |
| 244 | def reassign_floating_island(mesh, info_on_pix, image, depth): |
| 245 | H, W = mesh.graph['H'], mesh.graph['W'], |
| 246 | mesh_nodes = mesh.nodes |
| 247 | bord_up, bord_down = mesh.graph['bord_up'], mesh.graph['bord_down'] |
| 248 | bord_left, bord_right = mesh.graph['bord_left'], mesh.graph['bord_right'] |
| 249 | W = mesh.graph['W'] |
| 250 | lost_map = np.zeros((H, W)) |
| 251 | |
| 252 | ''' |
| 253 | (5) is_inside(x, y, xmin, xmax, ymin, ymax) : Check if a pixel(x, y) is inside the border. |
| 254 | (6) get_cross_nes(x, y) : Get the four cross neighbors of pixel(x, y). |
| 255 | ''' |
| 256 | key_exist = lambda d, k: k in d |
| 257 | is_inside = lambda x, y, xmin, xmax, ymin, ymax: xmin <= x < xmax and ymin <= y < ymax |
| 258 | get_cross_nes = lambda x, y: [(x + 1, y), (x - 1, y), (x, y - 1), (x, y + 1)] |
| 259 | ''' |
| 260 | (A) Highlight the pixels on isolated floating island. |
| 261 | (B) Number those isolated floating islands with connected component analysis. |
| 262 | (C) For each isolated island: |
| 263 | (1) Find its longest surrounded depth edge. |
| 264 | (2) Propogate depth from that depth edge to the pixels on the isolated island. |
| 265 | (3) Build the connection between the depth edge and that isolated island. |
| 266 | ''' |
| 267 | for x in range(H): |
| 268 | for y in range(W): |
| 269 | if is_inside(x, y, bord_up, bord_down, bord_left, bord_right) and not(key_exist(info_on_pix, (x, y))): |
| 270 | lost_map[x, y] = 1 |
| 271 | _, label_lost_map = cv2.connectedComponents(lost_map.astype(np.uint8), connectivity=4) |
| 272 | mask = np.zeros((H, W)) |
| 273 | mask[bord_up:bord_down, bord_left:bord_right] = 1 |
| 274 | label_lost_map = (label_lost_map * mask).astype(int) |
| 275 | |
| 276 | for i in range(1, label_lost_map.max()+1): |
| 277 | lost_xs, lost_ys = np.where(label_lost_map == i) |
| 278 | surr_edge_ids = {} |
| 279 | for lost_x, lost_y in zip(lost_xs, lost_ys): |
| 280 | #if (lost_x, lost_y) == (295, 389) or (lost_x, lost_y) == (296, 389): |
| 281 | # import pdb; pdb.set_trace() |
| 282 | for ne in get_cross_nes(lost_x, lost_y): |
| 283 | if key_exist(info_on_pix, ne): |
| 284 | for info in info_on_pix[ne]: |
| 285 | ne_node = (ne[0], ne[1], info['depth']) |
| 286 | if key_exist(mesh_nodes[ne_node], 'edge_id'): |
| 287 | edge_id = mesh_nodes[ne_node]['edge_id'] |
| 288 | surr_edge_ids[edge_id] = surr_edge_ids[edge_id] + [ne_node] if \ |
| 289 | key_exist(surr_edge_ids, edge_id) else [ne_node] |
| 290 | if len(surr_edge_ids) == 0: |
| 291 | continue |
| 292 | edge_id, edge_nodes = sorted([*surr_edge_ids.items()], key=lambda x: len(x[1]), reverse=True)[0] |
| 293 | edge_depth_map = np.zeros((H, W)) |
| 294 | for node in edge_nodes: |
| 295 | edge_depth_map[node[0], node[1]] = node[2] |
| 296 | lost_xs, lost_ys = np.where(label_lost_map == i) |
| 297 | while lost_xs.shape[0] > 0: |
| 298 | lost_xs, lost_ys = np.where(label_lost_map == i) |
| 299 | for lost_x, lost_y in zip(lost_xs, lost_ys): |
| 300 | propagated_depth = [] |
| 301 | real_nes = [] |