| 111 | |
| 112 | |
| 113 | class Cutting_branch(object): |
| 114 | def __init__(self): |
| 115 | self.lst_bifur_pt = 0 |
| 116 | self.branch_state = 0 |
| 117 | self.lst_branch_state = 0 |
| 118 | self.direction2delta = {0: [-1, -1], 1: [-1, 0], 2: [-1, 1], 3: [ |
| 119 | 0, -1], 4: [0, 0], 5: [0, 1], 6: [1, -1], 7: [1, 0], 8: [1, 1]} |
| 120 | |
| 121 | def __find_start(self, lab): |
| 122 | y, x = lab.shape |
| 123 | idxes = np.asarray(np.nonzero(lab)) |
| 124 | for i in range(idxes.shape[1]): |
| 125 | pt = tuple([idxes[0, i], idxes[1, i]]) |
| 126 | assert lab[pt] == 1 |
| 127 | directions = [] |
| 128 | for d in range(9): |
| 129 | if d == 4: |
| 130 | continue |
| 131 | if self.__detect_pt_bifur_state(lab, pt, d): |
| 132 | directions.append(d) |
| 133 | if len(directions) == 1: |
| 134 | start = pt |
| 135 | self.start = start |
| 136 | self.output[start] = 1 |
| 137 | return start |
| 138 | start = tuple([idxes[0, 0], idxes[1, 0]]) |
| 139 | self.output[start] = 1 |
| 140 | self.start = start |
| 141 | return start |
| 142 | |
| 143 | def __detect_pt_bifur_state(self, lab, pt, direction): |
| 144 | |
| 145 | d = direction |
| 146 | y = pt[0] + self.direction2delta[d][0] |
| 147 | x = pt[1] + self.direction2delta[d][1] |
| 148 | if lab[y, x] > 0: |
| 149 | return True |
| 150 | else: |
| 151 | return False |
| 152 | |
| 153 | def __detect_neighbor_bifur_state(self, lab, pt): |
| 154 | directions = [] |
| 155 | for i in range(9): |
| 156 | if i == 4: |
| 157 | continue |
| 158 | if self.output[tuple([pt[0] + self.direction2delta[i][0], pt[1] + self.direction2delta[i][1]])] > 0: |
| 159 | continue |
| 160 | if self.__detect_pt_bifur_state(lab, pt, i): |
| 161 | directions.append(i) |
| 162 | |
| 163 | if len(directions) == 0: |
| 164 | self.end = pt |
| 165 | return False |
| 166 | else: |
| 167 | direction = random.sample(directions, 1)[0] |
| 168 | next_pt = tuple([pt[0] + self.direction2delta[direction] |
| 169 | [0], pt[1] + self.direction2delta[direction][1]]) |
| 170 | if len(directions) > 1 and pt != self.start: |