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