Mercurial > repos > thomaswollmann > mergeneighboursinlabelimage
annotate mergeneighboursinlabelimage.py @ 0:66f7e94db2e6 draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
author | thomaswollmann |
---|---|
date | Tue, 08 Jan 2019 08:54:16 -0500 |
parents | |
children | 3536648d0a93 |
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 #!/usr/bin/python |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
2 |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
3 import argparse |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
4 import sys |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
5 import skimage.io |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
6 import skimage.util |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
7 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
|
8 import scipy.spatial.distance |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
9 import numpy as np |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
10 import warnings |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
11 |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
12 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
|
13 props = regionprops(img) |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
14 found = False |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
15 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
|
16 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
|
17 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
|
18 if i==q: |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
19 continue |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
20 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
|
21 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
|
22 if iq_dist <= dist: |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
23 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
|
24 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
|
25 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
|
26 found = True |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
27 if found: |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
28 merge_n(img, dist) |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
29 return img |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
30 |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
31 if __name__ == "__main__": |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
32 parser = argparse.ArgumentParser() |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
33 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
|
34 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
|
35 parser.add_argument( |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
36 '-c', |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
37 dest='cluster_merge', |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
38 type=int, |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
39 required=False, |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
40 default=50, |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
41 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
|
42 ) |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
43 args = parser.parse_args() |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
44 |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
45 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
|
46 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
|
47 with warnings.catch_warnings(): |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
48 warnings.simplefilter("ignore") |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
49 res = skimage.util.img_as_uint(res) |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
50 skimage.io.imsave("tmp.tiff", res) |
66f7e94db2e6
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/\mergeneighboursinlabelimage commit d0759a055484abd42bba38b2fa46f2a7b8f96259
thomaswollmann
parents:
diff
changeset
|
51 os.rename("tmp.tiff", args.output_file.name) |