def convert_hiearchy_to_PyPolyTree(self): """ Build a root node and find first level node in hiearchy, then add these node to root. Finally, use recusive_add_node(node,0) to generate hiearchy tree. Return contour tree (in PyPolyNode) ref: http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Classes/PolyTree/_Body.htm ref: https://stackoverflow.com/questions/32182544/pyclipper-crash-on-trivial-case-terminate-called-throwing-an-exception """ if self.hiearchy == None: return # find first contour in hiearchy-0 root = pyclipper.PyPolyNode() idx = -1 for row in self.hiearchy[0]: Next, Previous, First_Child, Parent = row if Previous == -1 and Parent == -1: idx += 1 node = pyclipper.PyPolyNode() node.Parent = root node.Contour = self.contours[idx].reshape( (-1, 2)) # (x rows, 1, 2) -> (x rows, 2) node.IsOpen = False node.IsHole = False root.Childs.append(node) self.recusive_add_node(node, idx) break self.root_of_region_contour = root return root
def setUp(self): tree = pyclipper.PyPolyNode() tree.Contour.append(PATH_CLIP_1) tree.IsOpen = True child = pyclipper.PyPolyNode() child.IsOpen = False child.Parent = tree child.Contour = PATH_SUBJ_1 tree.Childs.append(child) child = pyclipper.PyPolyNode() child.IsOpen = True child.Parent = tree child.Contour = PATH_SUBJ_2 tree.Childs.append(child) child2 = pyclipper.PyPolyNode() child2.IsOpen = False child2.Parent = child child2.Contour = PATTERN child.Childs.append(child2) # empty contour should not # be included in filtered results child2 = pyclipper.PyPolyNode() child2.IsOpen = False child2.Parent = child child2.Contour = [] child.Childs.append(child2) self.tree = tree
def recusive_add_node(self, node, idx): """ This function recursively add a child node add a brother node into current node based on the hiearchy matrix. The detail can be refered to https://docs.opencv.org/trunk/d9/d8b/tutorial_py_contours_hierarchy.html return @cur_node: current node, first input is a empty root node @idx: current index Note: In each node, the shape of contour from opencv is converted from (x,1,2) to (x,2) """ if idx >= self.hiearchy.shape[1]: return Next, Previous, First_Child, Parent = self.hiearchy[0][idx] new_node = pyclipper.PyPolyNode() new_node.IsOpen = False new_node.IsHole = False # add new node if First_Child != -1: new_node.Contour = self.contours[First_Child].reshape( (-1, 2)) # (x rows, 1, 2) -> (x rows, 2) new_node.Parent = node node.Childs.append(new_node) self.recusive_add_node(new_node, First_Child) if Next != -1: new_node.Contour = self.contours[Next].reshape((-1, 2)) new_node.Parent = node.Parent new_node.Parent.Childs.append(new_node) self.recusive_add_node(new_node, Next) return
def convert_hiearchy_to_PyPolyTree(): global hiearchy global contours # find first contour in hiearchy-0 root = pyclipper.PyPolyNode() idx = -1 for row in hiearchy[0]: Next, Previous, First_Child, Parent = row if Previous == -1 and Parent == -1: idx += 1 node = pyclipper.PyPolyNode() node.Parent = root node.Contour = contours[idx] node.IsOpen = False node.IsHole = False root.Childs.append(node) recusive_add_node(node, idx) break return root
def convert_hiearchy_to_PyPolyTree(self): if self.hiearchy == None: return # find first contour in hiearchy-0 root = pyclipper.PyPolyNode() idx = -1 for row in self.hiearchy[0]: Next, Previous, First_Child, Parent = row if Previous == -1 and Parent == -1: idx += 1 node = pyclipper.PyPolyNode() node.Parent = root node.Contour = self.contours[idx].reshape( (-1, 2)) # (x rows, 1, 2) -> (x rows, 2) node.IsOpen = False node.IsHole = False root.Childs.append(node) self.recusive_add_node(node, idx) break self.root_of_region_contour = root return root
def recusive_add_node(self, node, idx): if idx >= self.hiearchy.shape[1]: return Next, Previous, First_Child, Parent = self.hiearchy[0][idx] new_node = pyclipper.PyPolyNode() new_node.IsOpen = False new_node.IsHole = False # add new node if First_Child != -1: new_node.Contour = self.contours[First_Child].reshape( (-1, 2)) # (x rows, 1, 2) -> (x rows, 2) new_node.Parent = node node.Childs.append(new_node) self.recusive_add_node(new_node, First_Child) if Next != -1: new_node.Contour = self.contours[Next].reshape((-1, 2)) new_node.Parent = node.Parent new_node.Parent.Childs.append(new_node) self.recusive_add_node(new_node, Next) return
def recusive_add_node(node, idx): global hiearchy global contours if idx >= hiearchy.shape[1]: return Next, Previous, First_Child, Parent = hiearchy[0][idx] new_node = pyclipper.PyPolyNode() new_node.IsOpen = False new_node.IsHole = False # add new node if First_Child != -1: new_node.Contour = contours[First_Child] new_node.Parent = node node.Childs.append(new_node) recusive_add_node(new_node, First_Child) if Next != -1: new_node.Contour = contours[Next] new_node.Parent = node.Parent new_node.Parent.Childs.append(new_node) recusive_add_node(new_node, Next) return