# HG changeset patch # User thomaswollmann # Date 1546955656 18000 # Node ID 66f7e94db2e63115161697e2a63489a07f3c04a2 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259 diff -r 000000000000 -r 66f7e94db2e6 mergeneighboursinlabelimage.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mergeneighboursinlabelimage.py Tue Jan 08 08:54:16 2019 -0500 @@ -0,0 +1,51 @@ +#!/usr/bin/python + +import argparse +import sys +import skimage.io +import skimage.util +from skimage.measure import regionprops +import scipy.spatial.distance +import numpy as np +import warnings + +def merge_n(img, dist=50): + props = regionprops(img) + found = False + for i in range(0, len(props)): + i_coords = props[i].coords + for q in range(0, len(props)): + if i==q: + continue + q_coords = props[q].coords + iq_dist = np.min(scipy.spatial.distance.cdist(i_coords, q_coords, 'euclidean')) + if iq_dist <= dist: + props[q].label = props[i].label + for a_point in range(0, q_coords.shape[0]): + img[q_coords[a_point, 0], q_coords[a_point, 1]] = props[i].label + found = True + if found: + merge_n(img, dist) + return img + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file') + parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)') + parser.add_argument( + '-c', + dest='cluster_merge', + type=int, + required=False, + default=50, + help='Distance in pixel of clusters which are merged', + ) + args = parser.parse_args() + + label_image = skimage.io.imread(args.input_file.name) + label_image = merge_n(label_image, args.cluster_merge) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + res = skimage.util.img_as_uint(res) + skimage.io.imsave("tmp.tiff", res) + os.rename("tmp.tiff", args.output_file.name) diff -r 000000000000 -r 66f7e94db2e6 mergeneighboursinlabelimage.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mergeneighboursinlabelimage.xml Tue Jan 08 08:54:16 2019 -0500 @@ -0,0 +1,23 @@ + + Merge Neighbours in Label Image + + scikit-image + numpy + scipy + + + + + + + + + + + This tools merges nearby objects in a label image using the minimum pixel distance. + + 10.7717/peerj.453 + +