Mercurial > repos > imgteam > overlay_segmentation_mask
annotate overlay_segmentation_mask.py @ 1:8ad39f493587 draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit b2acc1845a25828181597fe5b6982fe116a7796d
| author | imgteam | 
|---|---|
| date | Mon, 22 Jul 2019 05:03:51 -0400 | 
| parents | 63b2a10b1001 | 
| children | 22e573b7dcb1 | 
| rev | line source | 
|---|---|
| 
0
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
1 import argparse | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
2 import sys | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
3 import os | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
4 | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
5 import matplotlib | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
6 matplotlib.use('Agg') | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
7 import matplotlib.pyplot as plt | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
8 | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
9 #TODO make importable by python script | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
10 | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
11 import skimage.io | 
| 
1
 
8ad39f493587
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit b2acc1845a25828181597fe5b6982fe116a7796d
 
imgteam 
parents: 
0 
diff
changeset
 | 
12 import skimage.measure | 
| 
0
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
13 | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
14 parser = argparse.ArgumentParser() | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
15 parser.add_argument('input_file', type=argparse.FileType('r'), help='input file') | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
16 parser.add_argument('mask_file', type=argparse.FileType('r'), help='mask file') | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
17 parser.add_argument('out_file', type=str, help='out file (PNG)') # file would be created immediately with argparse.FileType('w') s.t. file cannot be renamed on galaxy | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
18 parser.add_argument('--grey', dest='greyscale', action='store_true', help='image is greyscale') | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
19 parser.add_argument('--label', dest='label', action='store_true', help='plot label') | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
20 parser.add_argument('--label_color', dest='label_color', default='#FFFF00', help='label color') | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
21 parser.add_argument('--thickness', dest='thickness', default=0.3, type=float, help='thickness') | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
22 parser.add_argument('--stroke_color', dest='stroke_color', default='#ff0000', help='stroke color') | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
23 args = parser.parse_args() | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
24 img = skimage.io.imread(args.input_file.name) | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
25 label = skimage.io.imread(args.mask_file.name) | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
26 | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
27 fig = plt.figure() | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
28 ax = fig.add_axes([0, 0, 1, 1]) | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
29 ax.axis('off') | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
30 | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
31 if args.label: | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
32 for reg in skimage.measure.regionprops(label): | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
33 ax.text(reg.centroid[1], reg.centroid[0], str(reg.label), color=args.label_color) | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
34 | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
35 if args.greyscale: | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
36 plt.imshow(img, cmap=plt.cm.gray) | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
37 else: | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
38 plt.imshow(img) | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
39 plt.contour(label, linewidths=args.thickness, colors=args.stroke_color) | 
| 
 
63b2a10b1001
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 
imgteam 
parents:  
diff
changeset
 | 
40 | 
| 
1
 
8ad39f493587
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_segmentation_mask/ commit b2acc1845a25828181597fe5b6982fe116a7796d
 
imgteam 
parents: 
0 
diff
changeset
 | 
41 fig.canvas.print_png(args.out_file) | 
