annotate mergeneighboursinlabelimage.py @ 1:c6a6ab7c9940 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit b2acc1845a25828181597fe5b6982fe116a7796d
author imgteam
date Mon, 22 Jul 2019 05:03:33 -0400
parents 07ac39c0e949
children 9cbddb13fb9e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
1 import argparse
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
2 import sys
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
3 import skimage.io
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
4 import skimage.util
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
5 from skimage.measure import regionprops
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
6 import scipy.spatial.distance
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
7 import numpy as np
1
c6a6ab7c9940 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents: 0
diff changeset
8 import warnings
0
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
9
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
10 def merge_n(img, dist=50):
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
11 props = regionprops(img)
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
12 found = False
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
13 for i in range(0, len(props)):
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
14 i_coords = props[i].coords
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
15 for q in range(0, len(props)):
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
16 if i==q:
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
17 continue
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
18 q_coords = props[q].coords
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
19 iq_dist = np.min(scipy.spatial.distance.cdist(i_coords, q_coords, 'euclidean'))
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
20 if iq_dist <= dist:
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
21 props[q].label = props[i].label
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
22 for a_point in range(0, q_coords.shape[0]):
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
23 img[q_coords[a_point, 0], q_coords[a_point, 1]] = props[i].label
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
24 found = True
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
25 if found:
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
26 merge_n(img, dist)
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
27 return img
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
28
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
29 if __name__ == "__main__":
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
30 parser = argparse.ArgumentParser()
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
31 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file')
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
32 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)')
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
33 parser.add_argument(
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
34 '-c',
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
35 dest='cluster_merge',
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
36 type=int,
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
37 required=False,
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
38 default=50,
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
39 help='Distance in pixel of clusters which are merged',
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
40 )
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
41 args = parser.parse_args()
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
42
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
43 label_image = skimage.io.imread(args.input_file.name)
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
44 label_image = merge_n(label_image, args.cluster_merge)
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
45 with warnings.catch_warnings():
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
46 warnings.simplefilter("ignore")
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
47 res = skimage.util.img_as_uint(label_image)
07ac39c0e949 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
48 skimage.io.imsave(args.out_file.name, res, plugin="tifffile")