Mercurial > repos > imgteam > 2d_feature_extraction
comparison 2d_feature_extraction.py @ 5:2436a8807ad1 draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_feature_extraction/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
author | imgteam |
---|---|
date | Thu, 04 Apr 2024 15:23:23 +0000 |
parents | 0a53256b48c6 |
children | 5bc8cdc17fd0 |
comparison
equal
deleted
inserted
replaced
4:0a53256b48c6 | 5:2436a8807ad1 |
---|---|
6 import skimage.io | 6 import skimage.io |
7 import skimage.measure | 7 import skimage.measure |
8 import skimage.morphology | 8 import skimage.morphology |
9 import skimage.segmentation | 9 import skimage.segmentation |
10 | 10 |
11 # TODO make importable by python script | |
12 | 11 |
13 parser = argparse.ArgumentParser(description='Extract Features 2D') | 12 if __name__ == '__main__': |
14 | 13 |
15 # TODO create factory for boilerplate code | 14 parser = argparse.ArgumentParser(description='Extract image features') |
16 features = parser.add_argument_group('compute features') | |
17 features.add_argument('--all', dest='all_features', action='store_true') | |
18 features.add_argument('--label', dest='add_label', action='store_true') | |
19 features.add_argument('--patches', dest='add_roi_patches', action='store_true') | |
20 features.add_argument('--max_intensity', dest='max_intensity', action='store_true') | |
21 features.add_argument('--mean_intensity', dest='mean_intensity', action='store_true') | |
22 features.add_argument('--min_intensity', dest='min_intensity', action='store_true') | |
23 features.add_argument('--moments_hu', dest='moments_hu', action='store_true') | |
24 features.add_argument('--centroid', dest='centroid', action='store_true') | |
25 features.add_argument('--bbox', dest='bbox', action='store_true') | |
26 features.add_argument('--area', dest='area', action='store_true') | |
27 features.add_argument('--filled_area', dest='filled_area', action='store_true') | |
28 features.add_argument('--convex_area', dest='convex_area', action='store_true') | |
29 features.add_argument('--perimeter', dest='perimeter', action='store_true') | |
30 features.add_argument('--extent', dest='extent', action='store_true') | |
31 features.add_argument('--eccentricity', dest='eccentricity', action='store_true') | |
32 features.add_argument('--equivalent_diameter', dest='equivalent_diameter', action='store_true') | |
33 features.add_argument('--euler_number', dest='euler_number', action='store_true') | |
34 features.add_argument('--inertia_tensor_eigvals', dest='inertia_tensor_eigvals', action='store_true') | |
35 features.add_argument('--major_axis_length', dest='major_axis_length', action='store_true') | |
36 features.add_argument('--minor_axis_length', dest='minor_axis_length', action='store_true') | |
37 features.add_argument('--orientation', dest='orientation', action='store_true') | |
38 features.add_argument('--solidity', dest='solidity', action='store_true') | |
39 features.add_argument('--moments', dest='moments', action='store_true') | |
40 features.add_argument('--convexity', dest='convexity', action='store_true') | |
41 | 15 |
42 parser.add_argument('--label_file_binary', dest='label_file_binary', action='store_true') | 16 # TODO create factory for boilerplate code |
17 features = parser.add_argument_group('compute features') | |
18 features.add_argument('--all', dest='all_features', action='store_true') | |
19 features.add_argument('--label', dest='add_label', action='store_true') | |
20 features.add_argument('--patches', dest='add_roi_patches', action='store_true') | |
21 features.add_argument('--max_intensity', dest='max_intensity', action='store_true') | |
22 features.add_argument('--mean_intensity', dest='mean_intensity', action='store_true') | |
23 features.add_argument('--min_intensity', dest='min_intensity', action='store_true') | |
24 features.add_argument('--moments_hu', dest='moments_hu', action='store_true') | |
25 features.add_argument('--centroid', dest='centroid', action='store_true') | |
26 features.add_argument('--bbox', dest='bbox', action='store_true') | |
27 features.add_argument('--area', dest='area', action='store_true') | |
28 features.add_argument('--filled_area', dest='filled_area', action='store_true') | |
29 features.add_argument('--convex_area', dest='convex_area', action='store_true') | |
30 features.add_argument('--perimeter', dest='perimeter', action='store_true') | |
31 features.add_argument('--extent', dest='extent', action='store_true') | |
32 features.add_argument('--eccentricity', dest='eccentricity', action='store_true') | |
33 features.add_argument('--equivalent_diameter', dest='equivalent_diameter', action='store_true') | |
34 features.add_argument('--euler_number', dest='euler_number', action='store_true') | |
35 features.add_argument('--inertia_tensor_eigvals', dest='inertia_tensor_eigvals', action='store_true') | |
36 features.add_argument('--major_axis_length', dest='major_axis_length', action='store_true') | |
37 features.add_argument('--minor_axis_length', dest='minor_axis_length', action='store_true') | |
38 features.add_argument('--orientation', dest='orientation', action='store_true') | |
39 features.add_argument('--solidity', dest='solidity', action='store_true') | |
40 features.add_argument('--moments', dest='moments', action='store_true') | |
41 features.add_argument('--convexity', dest='convexity', action='store_true') | |
43 | 42 |
44 parser.add_argument('--raw', dest='raw_file', type=argparse.FileType('r'), | 43 parser.add_argument('--label_file_binary', dest='label_file_binary', action='store_true') |
45 help='Original input file', required=False) | |
46 parser.add_argument('label_file', type=argparse.FileType('r'), | |
47 help='Label input file') | |
48 parser.add_argument('output_file', type=argparse.FileType('w'), | |
49 help='Tabular output file') | |
50 args = parser.parse_args() | |
51 | 44 |
52 label_file_binary = args.label_file_binary | 45 parser.add_argument('--raw', dest='raw_file', type=argparse.FileType('r'), |
53 label_file = args.label_file.name | 46 help='Original input file', required=False) |
54 out_file = args.output_file.name | 47 parser.add_argument('label_file', type=argparse.FileType('r'), |
55 add_patch = args.add_roi_patches | 48 help='Label input file') |
49 parser.add_argument('output_file', type=argparse.FileType('w'), | |
50 help='Tabular output file') | |
51 args = parser.parse_args() | |
56 | 52 |
57 raw_image = None | 53 label_file_binary = args.label_file_binary |
58 if args.raw_file is not None: | 54 label_file = args.label_file.name |
59 raw_image = skimage.io.imread(args.raw_file.name) | 55 out_file = args.output_file.name |
56 add_patch = args.add_roi_patches | |
60 | 57 |
61 raw_label_image = skimage.io.imread(label_file) | 58 raw_image = None |
59 if args.raw_file is not None: | |
60 raw_image = skimage.io.imread(args.raw_file.name) | |
62 | 61 |
63 df = pd.DataFrame() | 62 raw_label_image = skimage.io.imread(label_file) |
64 if label_file_binary: | |
65 raw_label_image = skimage.measure.label(raw_label_image) | |
66 regions = skimage.measure.regionprops(raw_label_image, intensity_image=raw_image) | |
67 | 63 |
68 df['it'] = np.arange(len(regions)) | 64 df = pd.DataFrame() |
65 if label_file_binary: | |
66 raw_label_image = skimage.measure.label(raw_label_image) | |
67 regions = skimage.measure.regionprops(raw_label_image, intensity_image=raw_image) | |
69 | 68 |
70 if add_patch: | 69 df['it'] = np.arange(len(regions)) |
71 df['image'] = df['it'].map(lambda ait: regions[ait].image.astype(np.float).tolist()) | |
72 df['intensity_image'] = df['it'].map(lambda ait: regions[ait].intensity_image.astype(np.float).tolist()) | |
73 | 70 |
74 # TODO no matrix features, but split in own rows? | 71 if add_patch: |
75 if args.add_label or args.all_features: | 72 df['image'] = df['it'].map(lambda ait: regions[ait].image.astype(np.float).tolist()) |
76 df['label'] = df['it'].map(lambda ait: regions[ait].label) | 73 df['intensity_image'] = df['it'].map(lambda ait: regions[ait].intensity_image.astype(np.float).tolist()) |
77 | 74 |
78 if raw_image is not None: | 75 # TODO no matrix features, but split in own rows? |
79 if args.max_intensity or args.all_features: | 76 if args.add_label or args.all_features: |
80 df['max_intensity'] = df['it'].map(lambda ait: regions[ait].max_intensity) | 77 df['label'] = df['it'].map(lambda ait: regions[ait].label) |
81 if args.mean_intensity or args.all_features: | |
82 df['mean_intensity'] = df['it'].map(lambda ait: regions[ait].mean_intensity) | |
83 if args.min_intensity or args.all_features: | |
84 df['min_intensity'] = df['it'].map(lambda ait: regions[ait].min_intensity) | |
85 if args.moments_hu or args.all_features: | |
86 df['moments_hu'] = df['it'].map(lambda ait: regions[ait].moments_hu) | |
87 | 78 |
88 if args.centroid or args.all_features: | 79 if raw_image is not None: |
89 df['centroid'] = df['it'].map(lambda ait: regions[ait].centroid) | 80 if args.max_intensity or args.all_features: |
90 if args.bbox or args.all_features: | 81 df['max_intensity'] = df['it'].map(lambda ait: regions[ait].max_intensity) |
91 df['bbox'] = df['it'].map(lambda ait: regions[ait].bbox) | 82 if args.mean_intensity or args.all_features: |
92 if args.area or args.all_features: | 83 df['mean_intensity'] = df['it'].map(lambda ait: regions[ait].mean_intensity) |
93 df['area'] = df['it'].map(lambda ait: regions[ait].area) | 84 if args.min_intensity or args.all_features: |
94 if args.filled_area or args.all_features: | 85 df['min_intensity'] = df['it'].map(lambda ait: regions[ait].min_intensity) |
95 df['filled_area'] = df['it'].map(lambda ait: regions[ait].filled_area) | 86 if args.moments_hu or args.all_features: |
96 if args.convex_area or args.all_features: | 87 df['moments_hu'] = df['it'].map(lambda ait: regions[ait].moments_hu) |
97 df['convex_area'] = df['it'].map(lambda ait: regions[ait].convex_area) | |
98 if args.perimeter or args.all_features: | |
99 df['perimeter'] = df['it'].map(lambda ait: regions[ait].perimeter) | |
100 if args.extent or args.all_features: | |
101 df['extent'] = df['it'].map(lambda ait: regions[ait].extent) | |
102 if args.eccentricity or args.all_features: | |
103 df['eccentricity'] = df['it'].map(lambda ait: regions[ait].eccentricity) | |
104 if args.equivalent_diameter or args.all_features: | |
105 df['equivalent_diameter'] = df['it'].map(lambda ait: regions[ait].equivalent_diameter) | |
106 if args.euler_number or args.all_features: | |
107 df['euler_number'] = df['it'].map(lambda ait: regions[ait].euler_number) | |
108 if args.inertia_tensor_eigvals or args.all_features: | |
109 df['inertia_tensor_eigvals'] = df['it'].map(lambda ait: regions[ait].inertia_tensor_eigvals) | |
110 if args.major_axis_length or args.all_features: | |
111 df['major_axis_length'] = df['it'].map(lambda ait: regions[ait].major_axis_length) | |
112 if args.minor_axis_length or args.all_features: | |
113 df['minor_axis_length'] = df['it'].map(lambda ait: regions[ait].minor_axis_length) | |
114 if args.orientation or args.all_features: | |
115 df['orientation'] = df['it'].map(lambda ait: regions[ait].orientation) | |
116 if args.solidity or args.all_features: | |
117 df['solidity'] = df['it'].map(lambda ait: regions[ait].solidity) | |
118 if args.moments or args.all_features: | |
119 df['moments'] = df['it'].map(lambda ait: regions[ait].moments) | |
120 if args.convexity or args.all_features: | |
121 perimeter = df['it'].map(lambda ait: regions[ait].perimeter) | |
122 area = df['it'].map(lambda ait: regions[ait].area) | |
123 df['convexity'] = area / (perimeter * perimeter) | |
124 | 88 |
125 del df['it'] | 89 if args.centroid or args.all_features: |
126 df.to_csv(out_file, sep='\t', line_terminator='\n', index=False) | 90 df['centroid'] = df['it'].map(lambda ait: regions[ait].centroid) |
91 if args.bbox or args.all_features: | |
92 df['bbox'] = df['it'].map(lambda ait: regions[ait].bbox) | |
93 if args.area or args.all_features: | |
94 df['area'] = df['it'].map(lambda ait: regions[ait].area) | |
95 if args.filled_area or args.all_features: | |
96 df['filled_area'] = df['it'].map(lambda ait: regions[ait].filled_area) | |
97 if args.convex_area or args.all_features: | |
98 df['convex_area'] = df['it'].map(lambda ait: regions[ait].convex_area) | |
99 if args.perimeter or args.all_features: | |
100 df['perimeter'] = df['it'].map(lambda ait: regions[ait].perimeter) | |
101 if args.extent or args.all_features: | |
102 df['extent'] = df['it'].map(lambda ait: regions[ait].extent) | |
103 if args.eccentricity or args.all_features: | |
104 df['eccentricity'] = df['it'].map(lambda ait: regions[ait].eccentricity) | |
105 if args.equivalent_diameter or args.all_features: | |
106 df['equivalent_diameter'] = df['it'].map(lambda ait: regions[ait].equivalent_diameter) | |
107 if args.euler_number or args.all_features: | |
108 df['euler_number'] = df['it'].map(lambda ait: regions[ait].euler_number) | |
109 if args.inertia_tensor_eigvals or args.all_features: | |
110 df['inertia_tensor_eigvals'] = df['it'].map(lambda ait: regions[ait].inertia_tensor_eigvals) | |
111 if args.major_axis_length or args.all_features: | |
112 df['major_axis_length'] = df['it'].map(lambda ait: regions[ait].major_axis_length) | |
113 if args.minor_axis_length or args.all_features: | |
114 df['minor_axis_length'] = df['it'].map(lambda ait: regions[ait].minor_axis_length) | |
115 if args.orientation or args.all_features: | |
116 df['orientation'] = df['it'].map(lambda ait: regions[ait].orientation) | |
117 if args.solidity or args.all_features: | |
118 df['solidity'] = df['it'].map(lambda ait: regions[ait].solidity) | |
119 if args.moments or args.all_features: | |
120 df['moments'] = df['it'].map(lambda ait: regions[ait].moments) | |
121 if args.convexity or args.all_features: | |
122 perimeter = df['it'].map(lambda ait: regions[ait].perimeter) | |
123 area = df['it'].map(lambda ait: regions[ait].area) | |
124 df['convexity'] = area / (perimeter * perimeter) | |
125 | |
126 del df['it'] | |
127 df.to_csv(out_file, sep='\t', line_terminator='\n', index=False) |