| Previous changeset 3:be70a57d7174 (2024-10-29) |
|
Commit message:
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e |
|
modified:
color_deconvolution.py color_deconvolution.xml test-data/galaxyIcon_noText.png test-data/galaxyIcon_noText.tiff |
|
added:
static/images/he.png static/images/he_deconv.png static/images/he_recomposed.png test-data/hdab1.tiff test-data/hdab1_deconv_hdab.tiff test-data/he1.tiff test-data/he1_axes_cyx.tiff test-data/he1_axes_yxz.tiff test-data/he1_deconv_he.tiff test-data/he1_deconv_hed.tiff test-data/he1_deconv_hed_recomposed.tiff test-data/he1_deconv_hed_recomposed1.tiff test-data/he1_hsv.tiff test-data/im_axes_yx.tif |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc color_deconvolution.py --- a/color_deconvolution.py Tue Oct 29 13:49:19 2024 +0000 +++ b/color_deconvolution.py Thu Mar 06 18:12:27 2025 +0000 |
| [ |
| @@ -2,13 +2,23 @@ import sys import warnings +import giatools.io import numpy as np import skimage.color import skimage.io import skimage.util +import tifffile from sklearn.decomposition import FactorAnalysis, FastICA, NMF, PCA +# Stain separation matrix for H&E color deconvolution, extracted from ImageJ/FIJI +rgb_from_he = np.array([ + [0.64431860, 0.7166757, 0.26688856], + [0.09283128, 0.9545457, 0.28324000], + [0.63595444, 0.0010000, 0.77172660], +]) + convOptions = { + # General color space conversion operations 'hed2rgb': lambda img_raw: skimage.color.hed2rgb(img_raw), 'hsv2rgb': lambda img_raw: skimage.color.hsv2rgb(img_raw), 'lab2lch': lambda img_raw: skimage.color.lab2lch(img_raw), @@ -28,6 +38,20 @@ 'xyz2luv': lambda img_raw: skimage.color.xyz2luv(img_raw), 'xyz2rgb': lambda img_raw: skimage.color.xyz2rgb(img_raw), + # Color deconvolution operations + 'hed_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hed_from_rgb), + 'hdx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hdx_from_rgb), + 'fgx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.fgx_from_rgb), + 'bex_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bex_from_rgb), + 'rbd_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.rbd_from_rgb), + 'gdx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.gdx_from_rgb), + 'hax_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hax_from_rgb), + 'bro_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bro_from_rgb), + 'bpx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bpx_from_rgb), + 'ahx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.ahx_from_rgb), + 'hpx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hpx_from_rgb), + + # Recomposition operations (reverse color deconvolution) 'rgb_from_hed': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hed), 'rgb_from_hdx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hdx), 'rgb_from_fgx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_fgx), @@ -40,18 +64,11 @@ 'rgb_from_ahx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_ahx), 'rgb_from_hpx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hpx), - 'hed_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hed_from_rgb), - 'hdx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hdx_from_rgb), - 'fgx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.fgx_from_rgb), - 'bex_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bex_from_rgb), - 'rbd_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.rbd_from_rgb), - 'gdx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.gdx_from_rgb), - 'hax_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hax_from_rgb), - 'bro_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bro_from_rgb), - 'bpx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bpx_from_rgb), - 'ahx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.ahx_from_rgb), - 'hpx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hpx_from_rgb), + # Custom color deconvolution and recomposition operations + 'rgb_from_he': lambda img_raw: skimage.color.combine_stains(img_raw, rgb_from_he), + 'he_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, np.linalg.inv(rgb_from_he)), + # Unsupervised machine learning-based operations 'pca': lambda img_raw: np.reshape(PCA(n_components=3).fit_transform(np.reshape(img_raw, [-1, img_raw.shape[2]])), [img_raw.shape[0], img_raw.shape[1], -1]), 'nmf': lambda img_raw: np.reshape(NMF(n_components=3, init='nndsvda').fit_transform(np.reshape(img_raw, [-1, img_raw.shape[2]])), @@ -66,14 +83,33 @@ parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file') parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)') parser.add_argument('conv_type', choices=convOptions.keys(), help='conversion type') +parser.add_argument('--isolate_channel', type=int, help='set all other channels to zero (1-3)', default=0) args = parser.parse_args() -img_in = skimage.io.imread(args.input_file.name)[:, :, 0:3] -res = convOptions[args.conv_type](img_in) -res[res < -1] = -1 -res[res > +1] = +1 +# Read and normalize the input image as TZYXC +img_in = giatools.io.imread(args.input_file.name) + +# Verify input image +assert img_in.shape[0] == 1, f'Image must have 1 frame (it has {img_in.shape[0]} frames)' +assert img_in.shape[1] == 1, f'Image must have 1 slice (it has {img_in.shape[1]} slices)' +assert img_in.shape[4] == 3, f'Image must have 3 channels (it has {img_in.shape[4]} channels)' + +# Normalize the image from TZYXC to YXC +img_in = img_in.squeeze() +assert img_in.ndim == 3 + +# Apply channel isolation +if args.isolate_channel: + for ch in range(3): + if ch + 1 != args.isolate_channel: + img_in[:, :, ch] = 0 + +result = convOptions[args.conv_type](img_in) + +# It is sufficient to store 32bit floating point data, the precision loss is tolerable +if result.dtype == np.float64: + result = result.astype(np.float32) with warnings.catch_warnings(): warnings.simplefilter('ignore') - res = skimage.util.img_as_uint(res) # Attention: precision loss - skimage.io.imsave(args.out_file.name, res, plugin='tifffile') + tifffile.imwrite(args.out_file.name, result) |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc color_deconvolution.xml --- a/color_deconvolution.xml Tue Oct 29 13:49:19 2024 +0000 +++ b/color_deconvolution.xml Thu Mar 06 18:12:27 2025 +0000 |
| [ |
| b'@@ -1,10 +1,10 @@\n-<tool id="ip_color_deconvolution" name="Perform color decomposition" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@"> \n+<tool id="ip_color_deconvolution" name="Perform color deconvolution or transformation" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@"> \n <description></description>\n <macros>\n <import>creators.xml</import>\n <import>tests.xml</import>\n- <token name="@TOOL_VERSION@">0.8</token>\n- <token name="@VERSION_SUFFIX@">2</token>\n+ <token name="@TOOL_VERSION@">0.9</token>\n+ <token name="@VERSION_SUFFIX@">0</token>\n </macros>\n <creator>\n <expand macro="creators/bmcv"/>\n@@ -21,78 +21,180 @@\n <requirement type="package" version="1.5.2">scikit-learn</requirement>\n <requirement type="package" version="2.1.2">numpy</requirement>\n <requirement type="package" version="2024.9.20">tifffile</requirement>\n+ <requirement type="package" version="0.3.1">giatools</requirement>\n </requirements>\n <command detect_errors="aggressive">\n <![CDATA[ \n- python \'$__tool_directory__/color_deconvolution.py\' \'$input\' \'$output\' \'$convtype\'\n+\n+ python \'$__tool_directory__/color_deconvolution.py\'\n+\n+ \'$input\'\n+ \'$output\'\n+ \'$convtype\'\n+\n+ #if str($isolate_channel) != \'\':\n+ --isolate_channel \'${isolate_channel}\'\n+ #end if\n+\n ]]>\n </command>\n <inputs>\n- <param name="input" type="data" format="tiff,png,jpg,bmp" label="Image file with 3 channels"/>\n+ <param name="input" type="data" format="tiff,png,jpg,bmp" label="Input image" help="The input image must have 3 axes and 3 channels. The channels must correspond to the last axis."/>\n <param name="convtype" type="select" label="Transformation type">\n- <option value="ica" selected="True">ica</option>\n- <option value="pca">pca</option>\n- <option value="nmf">nmf</option>\n- <option value="fa">fa</option>\n- <option value="xyz2rgb">xyz2rgb</option>\n- <option value="rgb_from_rbd">rgb_from_rbd</option>\n- <option value="rgb_from_hdx">rgb_from_hdx</option>\n- <option value="rgb2hsv">rgb2hsv</option>\n- <option value="rgb_from_bro">rgb_from_bro</option>\n- <option value="bpx_from_rgb">bpx_from_rgb</option>\n- <option value="hed_from_rgb">hed_from_rgb</option>\n- <option value="rgbcie2rgb">rgbcie2rgb</option>\n- <option value="hdx_from_rgb">hdx_from_rgb</option>\n- <option value="xyz2luv">xyz2luv</option>\n- <option value="rgb2lab">rgb2lab</option>\n- <option value="hpx_from_rgb">hpx_from_rgb</option>\n- <option value="rgb_from_fgx">rgb_from_fgx</option>\n- <option value="rgb_from_gdx">rgb_from_gdx</option>\n- <option value="lab2xyz">lab2xyz</option>\n- <option value="rgb_from_hpx">rgb_from_hpx</option>\n- <option value="lab2rgb">lab2rgb</option>\n- <option value="rgb2rgbcie">rgb2rgbcie</option>\n- <option value="bex_from_rgb">bex_from_rgb</option>\n- <option value="xyz2lab">xyz2lab</option>\n- <option value="rgb_from_bex">rgb_from_bex</option>\n- <option value="fgx_from_rgb">fgx_from_rgb</option>\n- <option value="rbd_from_rgb">rbd_from_rgb</option>\n- <option value="rgb2hed">rgb2hed</option>\n- <option value="hed2rgb">hed2rgb</option>\n- <option value="luv2rgb">luv2rgb</option>\n- <option value="luv2xyz">luv2xyz</option>\n- <option value="lch2lab">lch2lab</option>\n- <option value="rgb2luv">rgb2luv</option>\n- <option value="ahx_from_rgb">ahx_from_rgb</option>\n- <option value="rgb_from_hax">rgb_from_hax</option>\n- <option value="hax_from_rgb">hax_from_rgb</option>\n- <option value="rgb_from_bpx"'..b'" />\n+ <expand macro="tests/intensity_image_diff" name="output" value="he1_deconv_hed_recomposed.tiff" ftype="tiff"/>\n+ </test>\n+ <test>\n+ <!-- Test recomposition with isolated channel -->\n+ <param name="input" value="he1_deconv_hed.tiff" />\n+ <param name="convtype" value="hed2rgb" />\n+ <param name="isolate_channel" value="1" />\n+ <expand macro="tests/intensity_image_diff" name="output" value="he1_deconv_hed_recomposed1.tiff" ftype="tiff"/>\n+ </test>\n+ <test>\n+ <!-- Test deconvolution using custom stain matrix -->\n+ <param name="input" value="he1.tiff" />\n+ <param name="convtype" value="he_from_rgb" />\n+ <expand macro="tests/intensity_image_diff" name="output" value="he1_deconv_he.tiff" ftype="tiff"/>\n+ </test>\n+ <test>\n+ <!-- Test deconvolution using scikit-image (Hematoxylin + DAB) -->\n+ <param name="input" value="hdab1.tiff" />\n+ <param name="convtype" value="hdx_from_rgb" />\n+ <expand macro="tests/intensity_image_diff" name="output" value="hdab1_deconv_hdab.tiff" ftype="tiff"/>\n+ </test>\n+ <test>\n+ <!-- Test with image that has 3 axes but in unusual order (and 3 channels) -->\n+ <param name="input" value="he1_axes_cyx.tiff" />\n+ <param name="convtype" value="rgb2hsv" />\n+ <expand macro="tests/intensity_image_diff" name="output" value="he1_hsv.tiff" ftype="tiff"/>\n+ </test>\n+\n+ <!-- Tests with incompatible input files (should fail) -->\n \n- **What it does**\n+ <test expect_failure="true">\n+ <!-- Test with image that only has 2 axes instead of required 3 -->\n+ <param name="input" value="im_axes_yx.tif" />\n+ <param name="convtype" value="rgb2hsv" />\n+ </test>\n+ <test expect_failure="true">\n+ <!-- Test with image that has 3 axes but YXZ instead of YXC (and 3 slices) -->\n+ <param name="input" value="he1_axes_yxz.tiff" />\n+ <param name="convtype" value="rgb2hsv" />\n+ </test>\n+\n+ </tests>\n+ <help><![CDATA[\n+\n+**This tool converts the color space of an image.**\n+\n+Several color deconvolution techniques are also supported.\n+\n+Color Deconvolution Example\n+===========================\n+\n+In this example, we are going to perform color deconvolution of the following RGB image:\n \n- This tools performs several color deconvolution techniques.\n+.. image:: he.png\n+ :width: 434px\n+ :scale: 50%\n+\n+Using the option "Deconvolve RGB into H&E (Hematoxylin + Eosin)" for the **Transformation type** performs color deconvolution and yields a new image with three chnnales:\n+\n+.. image:: he_deconv.png\n+ :width: 1305px\n+ :scale: 50%\n+\n+The channels of the deconvolved image are the Hematoxylin intensities (Channel 1), the Eosin intensities (Channel 2), and the residuals (Channel 3). White image regions correspond to high intensities and black image regions correspond to low intensities.\n \n- </help>\n+For visual inspection of the color deconvolution results, it may be useful to recompose separate RGB images for the Hematoxylin, Eosin, and residual channels. To create such images, this tool must be run once for each channel of the deconvolved image (i.e. three times), using the following options:\n+\n+* **Input image:** The result of the color deconvolution (image shown in the figure above)\n+* **Transformation type:** Recompose RGB from H&E (Hematoxylin + Eosin)\n+\n+The **Isolate channel** field must be set to 1, 2, and 3 during the three runs, respectively. This will yield the following recomposed RGB images for better visualization of the color deconvolution results:\n+\n+.. image:: he_recomposed.png\n+ :width: 1305px\n+ :scale: 50%\n+\n+ ]]></help>\n <citations>\n <citation type="doi">10.7717/peerj.453</citation>\n <citation type="bibtex">@inproceedings{sklearn_api,\n' |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc static/images/he.png |
| b |
| Binary file static/images/he.png has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc static/images/he_deconv.png |
| b |
| Binary file static/images/he_deconv.png has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc static/images/he_recomposed.png |
| b |
| Binary file static/images/he_recomposed.png has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc test-data/galaxyIcon_noText.png |
| b |
| Binary file test-data/galaxyIcon_noText.png has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc test-data/galaxyIcon_noText.tiff |
| b |
| Binary file test-data/galaxyIcon_noText.tiff has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc test-data/hdab1.tiff |
| b |
| Binary file test-data/hdab1.tiff has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc test-data/hdab1_deconv_hdab.tiff |
| b |
| Binary file test-data/hdab1_deconv_hdab.tiff has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc test-data/he1.tiff |
| b |
| Binary file test-data/he1.tiff has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc test-data/he1_axes_cyx.tiff |
| b |
| Binary file test-data/he1_axes_cyx.tiff has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc test-data/he1_axes_yxz.tiff |
| b |
| Binary file test-data/he1_axes_yxz.tiff has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc test-data/he1_deconv_he.tiff |
| b |
| Binary file test-data/he1_deconv_he.tiff has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc test-data/he1_deconv_hed.tiff |
| b |
| Binary file test-data/he1_deconv_hed.tiff has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc test-data/he1_deconv_hed_recomposed.tiff |
| b |
| Binary file test-data/he1_deconv_hed_recomposed.tiff has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc test-data/he1_deconv_hed_recomposed1.tiff |
| b |
| Binary file test-data/he1_deconv_hed_recomposed1.tiff has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc test-data/he1_hsv.tiff |
| b |
| Binary file test-data/he1_hsv.tiff has changed |
| b |
| diff -r be70a57d7174 -r 5bd113d38acc test-data/im_axes_yx.tif |
| b |
| Binary file test-data/im_axes_yx.tif has changed |