Mercurial > repos > imgteam > orientationpy
comparison orientationpy-cli.py @ 0:214f548481eb draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/orientationpy commit fa4501fb81c17f0b56889f250cf92396804295d1
author | imgteam |
---|---|
date | Tue, 12 Mar 2024 10:54:40 +0000 |
parents | |
children | cc8fe984ec3e |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:214f548481eb |
---|---|
1 import argparse | |
2 import csv | |
3 | |
4 import numpy as np | |
5 import orientationpy | |
6 import skimage.io | |
7 import skimage.util | |
8 | |
9 | |
10 if __name__ == '__main__': | |
11 | |
12 parser = argparse.ArgumentParser() | |
13 parser.add_argument('input', type=str) | |
14 parser.add_argument('--mode', type=str, required=True) | |
15 parser.add_argument('--sigma', type=float, required=True) | |
16 parser.add_argument('--min_coherency', type=float, required=True) | |
17 parser.add_argument('--min_energy', type=float, required=True) | |
18 parser.add_argument('--max_precision', type=int, required=True) | |
19 parser.add_argument('--output_angle_tsv', type=str, default=None) | |
20 args = parser.parse_args() | |
21 | |
22 im = skimage.io.imread(args.input) | |
23 im = skimage.util.img_as_float(im) | |
24 im = np.squeeze(im) | |
25 assert im.ndim == 2 | |
26 | |
27 Gy, Gx = orientationpy.computeGradient(im, mode=args.mode) | |
28 structureTensor = orientationpy.computeStructureTensor([Gy, Gx], sigma=args.sigma) | |
29 orientations = orientationpy.computeOrientation(structureTensor, computeEnergy=True, computeCoherency=True) | |
30 | |
31 # Compute angle according to https://bigwww.epfl.ch/demo/orientationj/#dist: | |
32 mask = np.logical_and( | |
33 orientations['coherency'] >= args.min_coherency, | |
34 orientations['energy'] >= args.min_energy * orientations['energy'].max(), | |
35 ) | |
36 angles = orientations['theta'][mask] | |
37 weights = orientations['coherency'][mask] | |
38 bin_size = 1 if args.max_precision == 0 else pow(10, -args.max_precision) | |
39 hist, bin_edges = np.histogram( | |
40 angles, | |
41 range=(-90, +90), | |
42 weights=weights, | |
43 bins=round(180 / bin_size), | |
44 ) | |
45 hidx = np.argmax(hist) | |
46 angle = (bin_edges[hidx] + bin_edges[hidx + 1]) / 2 | |
47 angle = round(angle, args.max_precision) | |
48 | |
49 # Write results | |
50 if args.output_angle_tsv: | |
51 with open(args.output_angle_tsv, 'w') as fp: | |
52 writer = csv.writer(fp, delimiter='\t', lineterminator='\n') | |
53 writer.writerow(['Angle']) | |
54 writer.writerow([angle]) |