Mercurial > repos > imgteam > slice_image
changeset 0:aacfbd7c4af6 draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
author | imgteam |
---|---|
date | Sat, 09 Feb 2019 14:46:50 -0500 |
parents | |
children | 8856a7c85e4c |
files | slice_image.py slice_image.xml test-data/input.tiff test-data/input_148.tiff test-data/input_155.tiff test-data/input_187.tiff test-data/input_212.tiff test-data/input_267.tiff |
diffstat | 8 files changed, 139 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/slice_image.py Sat Feb 09 14:46:50 2019 -0500 @@ -0,0 +1,73 @@ +import argparse +import sys +import warnings +import numpy as np +import random +import os.path +import skimage.io +import skimage.util +import skimage.feature +from scipy.stats import entropy as scipy_entropy + +def slice_image(input_file, out_folder, label=None, label_out_folder=None, window_size=64, + stride=1, bg_thresh=1, limit_slices=False, n_thresh=5000, seed=None): + #TODO NOT Implemented:process labels + # --> label and label_out_folder useless so far + + # primarily for testing purposes: + if seed is not None: + random.seed(seed) + + img_raw = skimage.io.imread(input_file) + if len(img_raw.shape) == 2: + img_raw = np.expand_dims(img_raw, 3) + + with warnings.catch_warnings(): # ignore FutureWarning + warnings.simplefilter("ignore") + patches_raw = skimage.util.view_as_windows(img_raw, (window_size, window_size, img_raw.shape[2]), step=stride) + patches_raw = patches_raw.reshape([-1, window_size, window_size, img_raw.shape[2]]) + + new_path = os.path.join(out_folder, "%d.tiff") + + #samples for thresholding the amount of slices + sample = random.sample(range(patches_raw.shape[0]), n_thresh) + + for i in range(0, patches_raw.shape[0]): + # TODO improve + sum_image = np.sum(patches_raw[i], 2)/img_raw.shape[2] + total_entr = np.var(sum_image.reshape([-1])) + + if bg_thresh > 0: + sum_image = skimage.util.img_as_uint(sum_image) + g = skimage.feature.greycomatrix(sum_image, [1,2], [0, np.pi/2], nnormed=True, symmetric=True) + hom = np.var(skimage.feature.greycoprops(g, prop='homogeneity')) + if hom > bg_thresh: #0.0005 + continue + + if limit_slices == True: + if i in sample: + res = skimage.util.img_as_uint(patches_raw[i]) #Attention: precision loss + skimage.io.imsave(new_path % i, res, plugin='tifffile') + else: + res = skimage.util.img_as_uint(patches_raw[i]) #Attention: precision loss + skimage.io.imsave(new_path % i, res, plugin='tifffile') + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('input_file', type=argparse.FileType('r'), help='input file') + parser.add_argument('out_folder', help='out folder') + parser.add_argument('--label', dest='label_file', default=None, help='auxiliary label file to split in the same way') + parser.add_argument('--label_out_folder', dest='label_out_folder', default=None, help='label out folder') + parser.add_argument('--stride', dest='stride', type=int, default=1, help='applied stride') + parser.add_argument('--window_size', dest='window_size', type=int, default=64, help='size of resulting patches') + parser.add_argument('--bg_thresh', dest='bg_thresh', type=float, default=0, help='skip patches without information using a treshold') + parser.add_argument('--limit_slices', dest='limit_slices', type=bool, default=False, help='limit amount of slices') + parser.add_argument('--n_thresh', dest='n_thresh', type=int, default=5000, help='amount of slices') + parser.add_argument('--seed', dest='seed', type=int, default=None, help='seed for random choice of limited slices') + args = parser.parse_args() + + slice_image(args.input_file.name, args.out_folder, + label=args.label_file, label_out_folder=args.label_out_folder, + stride=args.stride, window_size=args.window_size, bg_thresh=args.bg_thresh, + limit_slices=args.limit_slices, n_thresh=args.n_thresh, seed=args.seed)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/slice_image.xml Sat Feb 09 14:46:50 2019 -0500 @@ -0,0 +1,66 @@ +<tool id="ip_slice_image" name="Slice Image" version="0.2"> + <description>into smaller patches</description> + <requirements> + <requirement type="package" version="0.14.2">scikit-image</requirement> + <requirement type="package" version="1.15.4">numpy</requirement> + <!-- <requirement type="package" version="1.1.0">scipy</requirement> --> + </requirements> + <command> + <![CDATA[ + mkdir ./out && + python '$__tool_directory__/slice_image.py' + '$input_file' ./out + --stride $stride --window_size $patch_size --bg_thresh $bg_thresh + --limit_slices $limit_slices + #if $limit_slices: + --n_thresh $n_thresh + #end if + #if $control_rng: + --seed $seed + #end if + && ls -l ./out + ]]> + </command> + <inputs> + <param name="input_file" type="data" format="tiff,png,jpg,bmp" label="Input image"/> + <!--<param name="out_folder" type="text" label="Output folder for image slices"/> + <param name="label_file" type="data" format="tiff,png,jpg,bmp" label="Auxiliary label file to split in the same way"/>--> + <param name="stride" type="integer" optional="true" value="16"/> + <param name="patch_size" type="integer" optional="true" value="64" label="Size of resulting (quadratic-shaped) slices"/> + <param name="bg_thresh" type="float" optional="true" value="0" label="Threshold to not be considered background"/> + <param name="limit_slices" type="boolean" checked="true" label="Should the number of slices be limited?"/> + <param name="n_thresh" type="integer" optional="true" label="Maximum amount of slices; only needed if limit_slices is selected"/> + <param name="control_rng" type="boolean" checked="false" label="Boolean that controls if a certain seed should be selected for the random number generator"/> + <param name="seed" type="integer" value="1" + label="Seed chosen for the random number generator of the random choice of limited slices. Only required if cotrol_rng is selected"/> + </inputs> + <outputs> + <collection name="slices" type="list" label="Slices obtained from input"> + <discover_datasets directory="out" format="tiff" pattern="__name__"/> + </collection> + </outputs> + <tests> + <test> + <param name="input_file" value="input.tiff"/> + <param name="limit_slices" value="true"/> + <param name="n_thresh" value="5"/> + <param name="control_rng" value="true"/> + <param name="seed" value="17"/> + <output_collection name="slices" type="list"> + <element name="148.tiff" file="input_148.tiff" ftype="tiff" compare="sim_size"/> + <element name="155.tiff" file="input_155.tiff" ftype="tiff" compare="sim_size"/> + <element name="187.tiff" file="input_187.tiff" ftype="tiff" compare="sim_size"/> + <element name="212.tiff" file="input_212.tiff" ftype="tiff" compare="sim_size"/> + <element name="267.tiff" file="input_267.tiff" ftype="tiff" compare="sim_size"/> + </output_collection> + </test> + </tests> + <help> + **What it does** + + Slices image into multiple smaller patches. + </help> + <citations> + <citation type="doi">10.1016/j.jbiotec.2017.07.019</citation> + </citations> +</tool>