annotate mergeneighboursinlabelimage.py @ 1:3536648d0a93 draft default tip

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