|
| 1 | +<docs lang="markdown"> |
| 2 | +ImJoy plugin to create numbered label images. |
| 3 | +</docs> |
| 4 | + |
| 5 | +<config lang="json"> |
| 6 | +{ |
| 7 | + "name": "CreateNumberedLabels", |
| 8 | + "type": "native-python", |
| 9 | + "version": "0.0.1", |
| 10 | + "description": "Created numbered label images.", |
| 11 | + "tags": ["stable","dev"], |
| 12 | + "ui": [ |
| 13 | + "<span style='font-weight: 550; color:#666362'>Path LABELS</span>: {id: 'path_labels', type: 'string', placeholder: 'paste-path-to-labels'}", |
| 14 | + "<span style='font-weight: 550; color:#666362'>File string</span>: {id: 'file_ident', type: 'string', placeholder: 'mask__'}", |
| 15 | + "<span style='font-weight: 550; color:#666362'>Img extension</span>: {id: 'img_ext', type: 'string', placeholder: '.png'}" |
| 16 | + ], |
| 17 | + "cover": "", |
| 18 | + "inputs": null, |
| 19 | + "outputs": null, |
| 20 | + "flags": [], |
| 21 | + "icon": "extension", |
| 22 | + "api_version": "0.1.7", |
| 23 | + "env": "", |
| 24 | + "permissions": [], |
| 25 | + "requirements": { |
| 26 | + "dev": [""], |
| 27 | + "stable":["pip: -U git+https://github.com/fish-quant/fq-segmentation@master"] |
| 28 | + }, |
| 29 | + "dependencies": [] |
| 30 | +} |
| 31 | +</config> |
| 32 | + |
| 33 | +<script lang="python"> |
| 34 | +from imjoy import api |
| 35 | +import numpy as np |
| 36 | +import matplotlib.pyplot as plt |
| 37 | +from skimage.io import imread |
| 38 | +from skimage.measure import regionprops |
| 39 | +import segwrap |
| 40 | + |
| 41 | +from pathlib import Path |
| 42 | + |
| 43 | +class ImJoyPlugin(): |
| 44 | + |
| 45 | + async def setup(self): |
| 46 | + api.log('>>> Plugin CreateNumberedLabels initialized') |
| 47 | + api.log(f" * plugin version: {await api.getConfig('_version')}") |
| 48 | + api.log(f' * segwrap version: {segwrap.__version__}') |
| 49 | + |
| 50 | + def run(self, ctx): |
| 51 | + |
| 52 | + api.log('>>> Plugin CreateNumberedLabels running. Called with parameters:') |
| 53 | + api.log(ctx.config) |
| 54 | + |
| 55 | + # >> Prepare INPUT PARAMETERS |
| 56 | + file_ident = ctx.config.file_ident |
| 57 | + img_ext = ctx.config.img_ext |
| 58 | + |
| 59 | + # Path containing data, and to save results |
| 60 | + path_labels = ctx.config.path_labels |
| 61 | + |
| 62 | + # Get path for testing |
| 63 | + if api.TAG == 'dev': |
| 64 | + if path_labels == 'paste-path-to-labels': |
| 65 | + path_labels = r'D:\Documents\data-test\fish-quant\fq-imjoy-dev\analysis\segmentation-results' |
| 66 | + |
| 67 | + # Path containing the data |
| 68 | + if path_labels == 'paste-path-to-labels': |
| 69 | + api.alert('Path containing labels has to be specified.') |
| 70 | + return |
| 71 | + |
| 72 | + path_labels = Path(path_labels) |
| 73 | + if not path_labels.is_dir(): |
| 74 | + api.alert('Path containing labels does not exist.') |
| 75 | + return |
| 76 | + |
| 77 | + # Path to save data |
| 78 | + path_save = path_labels / 'labels_numbered' |
| 79 | + if not path_save.is_dir(): |
| 80 | + path_save.mkdir(parents=True) |
| 81 | + |
| 82 | + # >> Create random lookup table for Matplotlib |
| 83 | + # From: https://gist.github.com/jgomezdans/402500 |
| 84 | + vals = np.linspace(0, 1, 256) |
| 85 | + np.random.shuffle(vals) |
| 86 | + test = plt.cm.jet(vals) |
| 87 | + test[0 ,:] = [0, 0, 0, 0] # Set to zero |
| 88 | + cmap_random = plt.cm.colors.ListedColormap(test) |
| 89 | + |
| 90 | + # >> Loop over files and create label images |
| 91 | + api.showStatus('Create numbered label images') |
| 92 | + |
| 93 | + for f_labels in path_labels.glob(f'*{file_ident}*{img_ext}'): |
| 94 | + api.log(f'Processing label image {f_labels}') |
| 95 | + |
| 96 | + # >> Read label image and analyse objects |
| 97 | + img_labels = imread(str(f_labels)) |
| 98 | + props = regionprops(img_labels) |
| 99 | + |
| 100 | + # >> Create plots |
| 101 | + fig, ax = plt.subplots(1, 1, facecolor='white') |
| 102 | + fig.set_size_inches((6, 6)) |
| 103 | + ax.imshow(img_labels, cmap=cmap_random) |
| 104 | + ax.get_xaxis().set_visible(False) |
| 105 | + ax.get_yaxis().set_visible(False) |
| 106 | + |
| 107 | + for prop in props: |
| 108 | + ax.text(prop.centroid[1], |
| 109 | + prop.centroid[0], |
| 110 | + f'{prop.label}', |
| 111 | + fontsize=5, weight='bold', |
| 112 | + verticalalignment='center', horizontalalignment='center') |
| 113 | + |
| 114 | + name_save = path_save / f'{f_labels.stem}__numbered.png' |
| 115 | + plt.savefig(name_save, dpi=300) |
| 116 | + |
| 117 | + plt.close() |
| 118 | + |
| 119 | + api.showStatus('Preprocessing finished.') |
| 120 | + |
| 121 | +api.export(ImJoyPlugin()) |
| 122 | +</script> |
0 commit comments