| 211 | |
| 212 | |
| 213 | def scrible_2d(label, iteration=[4, 10]): |
| 214 | lab = label |
| 215 | skeleton_map = np.zeros_like(lab, dtype=np.int32) |
| 216 | for i in range(lab.shape[0]): |
| 217 | if np.sum(lab[i]) == 0: |
| 218 | continue |
| 219 | struct = ndimage.generate_binary_structure(2, 2) |
| 220 | if np.sum(lab[i]) > 900 and iteration != 0 and iteration != [0] and iteration != None: |
| 221 | iter_num = math.ceil( |
| 222 | iteration[0]+random.random() * (iteration[1]-iteration[0])) |
| 223 | slic = ndimage.morphology.binary_erosion( |
| 224 | lab[i], structure=struct, iterations=iter_num) |
| 225 | else: |
| 226 | slic = lab[i] |
| 227 | sk_slice = skeletonize(slic, method='lee') |
| 228 | sk_slice = np.asarray((sk_slice == 255), dtype=np.int32) |
| 229 | skeleton_map[i] = sk_slice |
| 230 | return skeleton_map |
| 231 | |
| 232 | |
| 233 | def scribble4class(label, class_id, class_num, iteration=[4, 10], cut_branch=True): |