comparison color_deconvolution.py @ 2:612aa1478fe1 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
author imgteam
date Mon, 13 Nov 2023 22:10:42 +0000
parents 0976b373a321
children 5bd113d38acc
comparison
equal deleted inserted replaced
1:0976b373a321 2:612aa1478fe1
1 import argparse 1 import argparse
2 import sys 2 import sys
3 import warnings 3 import warnings
4
4 import numpy as np 5 import numpy as np
6 import skimage.color
5 import skimage.io 7 import skimage.io
6 import skimage.color
7 import skimage.util 8 import skimage.util
8 from sklearn.decomposition import PCA, NMF, FastICA, FactorAnalysis 9 from sklearn.decomposition import FactorAnalysis, FastICA, NMF, PCA
9 10
10 convOptions = { 11 convOptions = {
11 'hed2rgb' : lambda img_raw: skimage.color.hed2rgb(img_raw), 12 'hed2rgb': lambda img_raw: skimage.color.hed2rgb(img_raw),
12 'hsv2rgb' : lambda img_raw: skimage.color.hsv2rgb(img_raw), 13 'hsv2rgb': lambda img_raw: skimage.color.hsv2rgb(img_raw),
13 'lab2lch' : lambda img_raw: skimage.color.lab2lch(img_raw), 14 'lab2lch': lambda img_raw: skimage.color.lab2lch(img_raw),
14 'lab2rgb' : lambda img_raw: skimage.color.lab2rgb(img_raw), 15 'lab2rgb': lambda img_raw: skimage.color.lab2rgb(img_raw),
15 'lab2xyz' : lambda img_raw: skimage.color.lab2xyz(img_raw), 16 'lab2xyz': lambda img_raw: skimage.color.lab2xyz(img_raw),
16 'lch2lab' : lambda img_raw: skimage.color.lch2lab(img_raw), 17 'lch2lab': lambda img_raw: skimage.color.lch2lab(img_raw),
17 'luv2rgb' : lambda img_raw: skimage.color.luv2rgb(img_raw), 18 'luv2rgb': lambda img_raw: skimage.color.luv2rgb(img_raw),
18 'luv2xyz' : lambda img_raw: skimage.color.luv2xyz(img_raw), 19 'luv2xyz': lambda img_raw: skimage.color.luv2xyz(img_raw),
19 'rgb2hed' : lambda img_raw: skimage.color.rgb2hed(img_raw), 20 'rgb2hed': lambda img_raw: skimage.color.rgb2hed(img_raw),
20 'rgb2hsv' : lambda img_raw: skimage.color.rgb2hsv(img_raw), 21 'rgb2hsv': lambda img_raw: skimage.color.rgb2hsv(img_raw),
21 'rgb2lab' : lambda img_raw: skimage.color.rgb2lab(img_raw), 22 'rgb2lab': lambda img_raw: skimage.color.rgb2lab(img_raw),
22 'rgb2luv' : lambda img_raw: skimage.color.rgb2luv(img_raw), 23 'rgb2luv': lambda img_raw: skimage.color.rgb2luv(img_raw),
23 'rgb2rgbcie' : lambda img_raw: skimage.color.rgb2rgbcie(img_raw), 24 'rgb2rgbcie': lambda img_raw: skimage.color.rgb2rgbcie(img_raw),
24 'rgb2xyz' : lambda img_raw: skimage.color.rgb2xyz(img_raw), 25 'rgb2xyz': lambda img_raw: skimage.color.rgb2xyz(img_raw),
25 #'rgb2ycbcr' : lambda img_raw: skimage.color.rgb2ycbcr(img_raw), 26 'rgbcie2rgb': lambda img_raw: skimage.color.rgbcie2rgb(img_raw),
26 #'rgb2yiq' : lambda img_raw: skimage.color.rgb2yiq(img_raw), 27 'xyz2lab': lambda img_raw: skimage.color.xyz2lab(img_raw),
27 #'rgb2ypbpr' : lambda img_raw: skimage.color.rgb2ypbpr(img_raw), 28 'xyz2luv': lambda img_raw: skimage.color.xyz2luv(img_raw),
28 #'rgb2yuv' : lambda img_raw: skimage.color.rgb2yuv(img_raw), 29 'xyz2rgb': lambda img_raw: skimage.color.xyz2rgb(img_raw),
29 #'rgba2rgb' : lambda img_raw: skimage.color.rgba2rgb(img_raw), 30
30 'rgbcie2rgb' : lambda img_raw: skimage.color.rgbcie2rgb(img_raw), 31 'rgb_from_hed': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hed),
31 'xyz2lab' : lambda img_raw: skimage.color.xyz2lab(img_raw), 32 'rgb_from_hdx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hdx),
32 'xyz2luv' : lambda img_raw: skimage.color.xyz2luv(img_raw), 33 'rgb_from_fgx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_fgx),
33 'xyz2rgb' : lambda img_raw: skimage.color.xyz2rgb(img_raw), 34 'rgb_from_bex': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_bex),
34 #'ycbcr2rgb' : lambda img_raw: skimage.color.ycbcr2rgb(img_raw), 35 'rgb_from_rbd': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_rbd),
35 #'yiq2rgb' : lambda img_raw: skimage.color.yiq2rgb(img_raw), 36 'rgb_from_gdx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_gdx),
36 #'ypbpr2rgb' : lambda img_raw: skimage.color.ypbpr2rgb(img_raw), 37 'rgb_from_hax': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hax),
37 #'yuv2rgb' : lambda img_raw: skimage.color.yuv2rgb(img_raw), 38 'rgb_from_bro': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_bro),
38 39 'rgb_from_bpx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_bpx),
39 'rgb_from_hed' : lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hed), 40 'rgb_from_ahx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_ahx),
40 'rgb_from_hdx' : lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hdx), 41 'rgb_from_hpx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hpx),
41 'rgb_from_fgx' : lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_fgx), 42
42 'rgb_from_bex' : lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_bex), 43 'hed_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hed_from_rgb),
43 'rgb_from_rbd' : lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_rbd), 44 'hdx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hdx_from_rgb),
44 'rgb_from_gdx' : lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_gdx), 45 'fgx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.fgx_from_rgb),
45 'rgb_from_hax' : lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hax), 46 'bex_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bex_from_rgb),
46 'rgb_from_bro' : lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_bro), 47 'rbd_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.rbd_from_rgb),
47 'rgb_from_bpx' : lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_bpx), 48 'gdx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.gdx_from_rgb),
48 'rgb_from_ahx' : lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_ahx), 49 'hax_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hax_from_rgb),
49 'rgb_from_hpx' : lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hpx), 50 'bro_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bro_from_rgb),
50 51 'bpx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bpx_from_rgb),
51 'hed_from_rgb' : lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hed_from_rgb), 52 'ahx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.ahx_from_rgb),
52 'hdx_from_rgb' : lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hdx_from_rgb), 53 'hpx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hpx_from_rgb),
53 'fgx_from_rgb' : lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.fgx_from_rgb), 54
54 'bex_from_rgb' : lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bex_from_rgb), 55 'pca': lambda img_raw: np.reshape(PCA(n_components=3).fit_transform(np.reshape(img_raw, [-1, img_raw.shape[2]])),
55 'rbd_from_rgb' : lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.rbd_from_rgb), 56 [img_raw.shape[0], img_raw.shape[1], -1]),
56 'gdx_from_rgb' : lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.gdx_from_rgb), 57 'nmf': lambda img_raw: np.reshape(NMF(n_components=3, init='nndsvda').fit_transform(np.reshape(img_raw, [-1, img_raw.shape[2]])),
57 'hax_from_rgb' : lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hax_from_rgb), 58 [img_raw.shape[0], img_raw.shape[1], -1]),
58 'bro_from_rgb' : lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bro_from_rgb), 59 'ica': lambda img_raw: np.reshape(FastICA(n_components=3).fit_transform(np.reshape(img_raw, [-1, img_raw.shape[2]])),
59 'bpx_from_rgb' : lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bpx_from_rgb), 60 [img_raw.shape[0], img_raw.shape[1], -1]),
60 'ahx_from_rgb' : lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.ahx_from_rgb), 61 'fa': lambda img_raw: np.reshape(FactorAnalysis(n_components=3).fit_transform(np.reshape(img_raw, [-1, img_raw.shape[2]])),
61 'hpx_from_rgb' : lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hpx_from_rgb), 62 [img_raw.shape[0], img_raw.shape[1], -1])
62
63 'pca' : lambda img_raw: np.reshape(PCA(n_components=3).fit_transform(np.reshape(img_raw, [-1, img_raw.shape[2]])),
64 [img_raw.shape[0],img_raw.shape[1],-1]),
65 'nmf' : lambda img_raw: np.reshape(NMF(n_components=3, init='nndsvda').fit_transform(np.reshape(img_raw, [-1, img_raw.shape[2]])),
66 [img_raw.shape[0],img_raw.shape[1],-1]),
67 'ica' : lambda img_raw: np.reshape(FastICA(n_components=3).fit_transform(np.reshape(img_raw, [-1, img_raw.shape[2]])),
68 [img_raw.shape[0],img_raw.shape[1],-1]),
69 'fa' : lambda img_raw: np.reshape(FactorAnalysis(n_components=3).fit_transform(np.reshape(img_raw, [-1, img_raw.shape[2]])),
70 [img_raw.shape[0],img_raw.shape[1],-1])
71 } 63 }
72 64
73 parser = argparse.ArgumentParser() 65 parser = argparse.ArgumentParser()
74 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file') 66 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file')
75 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)') 67 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)')
76 parser.add_argument('conv_type', choices=convOptions.keys(), help='conversion type') 68 parser.add_argument('conv_type', choices=convOptions.keys(), help='conversion type')
77 args = parser.parse_args() 69 args = parser.parse_args()
78 70
79 img_in = skimage.io.imread(args.input_file.name)[:,:,0:3] 71 img_in = skimage.io.imread(args.input_file.name)[:, :, 0:3]
80 res = convOptions[args.conv_type](img_in) 72 res = convOptions[args.conv_type](img_in)
81 res[res<-1]=-1 73 res[res < -1] = -1
82 res[res>1]=1 74 res[res > +1] = +1
83 75
84 with warnings.catch_warnings(): 76 with warnings.catch_warnings():
85 warnings.simplefilter("ignore") 77 warnings.simplefilter('ignore')
86 res = skimage.util.img_as_uint(res) #Attention: precision loss 78 res = skimage.util.img_as_uint(res) # Attention: precision loss
87 skimage.io.imsave(args.out_file.name, res, plugin='tifffile') 79 skimage.io.imsave(args.out_file.name, res, plugin='tifffile')