annotate voronoi_tessellation.py @ 1:e7fdea8385f0 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit c045f067a57e8308308cf6329060c7ccd3fc372f
author imgteam
date Thu, 04 Apr 2024 15:26:55 +0000
parents 8b74843c136e
children 412d7478ec92
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
1 import argparse
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
2
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
3 import numpy as np
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
4 import scipy.ndimage as ndi
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
5 import skimage.io
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
6 from skimage.segmentation import watershed
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
7
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
8
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
9 if __name__ == '__main__':
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
10
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
11 parser = argparse.ArgumentParser()
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
12 parser.add_argument('input')
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
13 parser.add_argument('output')
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
14 args = parser.parse_args()
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
15
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
16 im = skimage.io.imread(args.input)
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
17 im = im.squeeze()
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
18 assert im.ndim == 2
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
19
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
20 distances = np.full(im.shape, np.inf)
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
21 for label in np.unique(im):
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
22 if label == 0:
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
23 continue
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
24
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
25 label_distances = ndi.distance_transform_edt(im != label)
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
26 distances = np.min((distances, label_distances), axis=0)
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
27
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
28 result = watershed(
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
29 image=distances,
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
30 markers=im,
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
31 )
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
32
8b74843c136e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/voronoi_tesselation commit 43f036ed5341c36a1e076f9df9f47a3743ef4e6f
imgteam
parents:
diff changeset
33 skimage.io.imsave(args.output, result)