MCPcopy Create free account
hub / github.com/HiLab-git/WORD / get_largest_two_component_2D

Function get_largest_two_component_2D

scribbles_generator.py:63–110  ·  view source on GitHub ↗

Get the largest two components of a binary volume inputs: img: the input 2D volume threshold: a size threshold outputs: out_img: the output volume

(img, print_info=False, threshold=None)

Source from the content-addressed store, hash-verified

61
62
63def get_largest_two_component_2D(img, print_info=False, threshold=None):
64 """
65 Get the largest two components of a binary volume
66 inputs:
67 img: the input 2D volume
68 threshold: a size threshold
69 outputs:
70 out_img: the output volume
71 """
72 s = ndimage.generate_binary_structure(2, 2) # iterate structure
73 labeled_array, numpatches = ndimage.label(img, s) # labeling
74 sizes = ndimage.sum(img, labeled_array, range(1, numpatches+1))
75 sizes_list = [sizes[i] for i in range(len(sizes))]
76 sizes_list.sort()
77 if(print_info):
78 print('component size', sizes_list)
79 if(len(sizes) == 1):
80 out_img = [img]
81 else:
82 if(threshold):
83 max_size1 = sizes_list[-1]
84 max_label1 = np.where(sizes == max_size1)[0] + 1
85 if max_label1.shape[0] > 1:
86 max_label1 = max_label1[0]
87 component1 = labeled_array == max_label1
88 out_img = [component1]
89 for temp_size in sizes_list:
90 if(temp_size > threshold):
91 temp_lab = np.where(sizes == temp_size)[0] + 1
92 temp_cmp = labeled_array == temp_lab[0]
93 out_img.append(temp_cmp)
94 return out_img
95 else:
96 max_size1 = sizes_list[-1]
97 max_size2 = sizes_list[-2]
98 max_label1 = np.where(sizes == max_size1)[0] + 1
99 max_label2 = np.where(sizes == max_size2)[0] + 1
100 if max_label1.shape[0] > 1:
101 max_label1 = max_label1[0]
102 if max_label2.shape[0] > 1:
103 max_label2 = max_label2[0]
104 component1 = labeled_array == max_label1
105 component2 = labeled_array == max_label2
106 if(max_size2*10 > max_size1):
107 out_img = [component1, component2]
108 else:
109 out_img = [component1]
110 return out_img
111
112
113class Cutting_branch(object):

Callers 1

__call__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected