MCPcopy Index your code
hub / github.com/thygate/stable-diffusion-webui-depthmap-script / remove_dangling

Function remove_dangling

inpaint/mesh.py:739–908  ·  view source on GitHub ↗
(mesh, edge_ccs, edge_mesh, info_on_pix, image, depth, config)

Source from the content-addressed store, hash-verified

737 return mark
738
739def remove_dangling(mesh, edge_ccs, edge_mesh, info_on_pix, image, depth, config):
740
741 tmp_edge_ccs = copy.deepcopy(edge_ccs)
742 for edge_cc_id, valid_edge_cc in enumerate(tmp_edge_ccs):
743 if len(valid_edge_cc) > 1 or len(valid_edge_cc) == 0:
744 continue
745 single_edge_node = [*valid_edge_cc][0]
746 hx, hy, hz = single_edge_node
747 eight_nes = set([(x, y, info_on_pix[(x, y)][0]['depth']) for x, y in [(hx + 1, hy), (hx - 1, hy), (hx, hy + 1), (hx, hy - 1),
748 (hx + 1, hy + 1), (hx - 1, hy - 1), (hx - 1, hy + 1), (hx + 1, hy - 1)] \
749 if info_on_pix.get((x, y)) is not None])
750 four_nes = [(x, y, info_on_pix[(x, y)][0]['depth']) for x, y in [(hx + 1, hy), (hx - 1, hy), (hx, hy + 1), (hx, hy - 1)] \
751 if info_on_pix.get((x, y)) is not None]
752 sub_mesh = mesh.subgraph(eight_nes).copy()
753 ccs = netx.connected_components(sub_mesh)
754 four_ccs = []
755 for cc_id, _cc in enumerate(ccs):
756 four_ccs.append(set())
757 for cc_node in _cc:
758 if abs(cc_node[0] - hx) + abs(cc_node[1] - hy) < 2:
759 four_ccs[cc_id].add(cc_node)
760 largest_cc = sorted(four_ccs, key=lambda x: (len(x), -np.sum([abs(xx[2] - hz) for xx in x])))[-1]
761 if len(largest_cc) < 2:
762 for ne in four_nes:
763 mesh.add_edge(single_edge_node, ne)
764 else:
765 mesh.remove_edges_from([(single_edge_node, ne) for ne in mesh.neighbors(single_edge_node)])
766 new_depth = np.mean([xx[2] for xx in largest_cc])
767 info_on_pix[(hx, hy)][0]['depth'] = new_depth
768 info_on_pix[(hx, hy)][0]['disp'] = 1./new_depth
769 new_node = (hx, hy, new_depth)
770 mesh = refresh_node(single_edge_node, mesh.nodes[single_edge_node], new_node, dict(), mesh)
771 edge_ccs[edge_cc_id] = set([new_node])
772 for ne in largest_cc:
773 mesh.add_edge(new_node, ne)
774
775 mark = np.zeros((mesh.graph['H'], mesh.graph['W']))
776 for edge_idx, edge_cc in enumerate(edge_ccs):
777 for edge_node in edge_cc:
778 if not (mesh.graph['bord_up'] <= edge_node[0] < mesh.graph['bord_down']-1) or \
779 not (mesh.graph['bord_left'] <= edge_node[1] < mesh.graph['bord_right']-1):
780 continue
781 mesh_neighbors = [*mesh.neighbors(edge_node)]
782 mesh_neighbors = [xx for xx in mesh_neighbors \
783 if mesh.graph['bord_up'] < xx[0] < mesh.graph['bord_down'] - 1 and \
784 mesh.graph['bord_left'] < xx[1] < mesh.graph['bord_right'] - 1]
785 if len([*mesh.neighbors(edge_node)]) >= 3:
786 continue
787 elif len([*mesh.neighbors(edge_node)]) <= 1:
788 mark[edge_node[0], edge_node[1]] += (len([*mesh.neighbors(edge_node)]) + 1)
789 else:
790 dan_ne_node_a = [*mesh.neighbors(edge_node)][0]
791 dan_ne_node_b = [*mesh.neighbors(edge_node)][1]
792 if abs(dan_ne_node_a[0] - dan_ne_node_b[0]) > 1 or \
793 abs(dan_ne_node_a[1] - dan_ne_node_b[1]) > 1:
794 mark[edge_node[0], edge_node[1]] += 3
795 mxs, mys = np.where(mark == 1)
796 conn_0_nodes = [(x[0], x[1], info_on_pix[(x[0], x[1])][0]['depth']) for x in zip(mxs, mys) \

Callers 1

write_meshFunction · 0.85

Calls 5

refresh_nodeFunction · 0.90
update_infoFunction · 0.90
recursive_add_edgeFunction · 0.90
getMethod · 0.80
appendMethod · 0.80

Tested by

no test coverage detected