Launch binder

Extract texture features

This example shows how to extract texture features from the tissue image.

Textures features give give a measure of how the image intensity at different distances and angles varies by calculating a grey-level co-occurrence matrix (GLCM). The GLCM includes the number of times that grey-level \(j\) occurs at a distance \(d\) and at an angle \(\\theta\) from grey-level \(i\). From this data, different features (props) are calculated. See also skimage.feature.greycomatrix().

Texture features are calculated by using features = 'texture', which calls squidpy.im.ImageContainer.features_texture(). In addition to feature_name and channels, we can also specify the following features_kwargs:

  • distances - distances that are taken into account for finding repeating patterns.

  • angles - range on which values are binned. Default is the whole image range.

  • props - texture features that are extracted from the GLCM.

import scanpy as sc
import squidpy as sq

Let’s load the fluorescence Visium dataset and calculate texture features with default features_kwargs.

Note that for texture features it may make sense to compute them over a larger crop size to include more context, e.g., spot_scale = 2 or spit_scale = 4 which will extract crops with double or four times the radius than the original Visium spot size. For more details on the image cropping, see Crop images with ImageContainer.

# get spatial dataset including high-resolution tissue image
img = sq.datasets.visium_fluo_image_crop()
adata = sq.datasets.visium_fluo_adata_crop()

# calculate texture features and save in key "texture_features"
sq.im.calculate_image_features(
    adata,
    img,
    features="texture",
    key_added="texture_features",
    spot_scale=2,
    show_progress_bar=False,
)

The result is stored in adata.obsm['texture_features'].

adata.obsm["texture_features"].head()
texture_ch-0_contrast_dist-1_angle-0.00 texture_ch-0_contrast_dist-1_angle-0.79 texture_ch-0_contrast_dist-1_angle-1.57 texture_ch-0_contrast_dist-1_angle-2.36 texture_ch-0_dissimilarity_dist-1_angle-0.00 texture_ch-0_dissimilarity_dist-1_angle-0.79 texture_ch-0_dissimilarity_dist-1_angle-1.57 texture_ch-0_dissimilarity_dist-1_angle-2.36 texture_ch-0_homogeneity_dist-1_angle-0.00 texture_ch-0_homogeneity_dist-1_angle-0.79 texture_ch-0_homogeneity_dist-1_angle-1.57 texture_ch-0_homogeneity_dist-1_angle-2.36 texture_ch-0_correlation_dist-1_angle-0.00 texture_ch-0_correlation_dist-1_angle-0.79 texture_ch-0_correlation_dist-1_angle-1.57 texture_ch-0_correlation_dist-1_angle-2.36 texture_ch-0_ASM_dist-1_angle-0.00 texture_ch-0_ASM_dist-1_angle-0.79 texture_ch-0_ASM_dist-1_angle-1.57 texture_ch-0_ASM_dist-1_angle-2.36 texture_ch-1_contrast_dist-1_angle-0.00 texture_ch-1_contrast_dist-1_angle-0.79 texture_ch-1_contrast_dist-1_angle-1.57 texture_ch-1_contrast_dist-1_angle-2.36 texture_ch-1_dissimilarity_dist-1_angle-0.00 texture_ch-1_dissimilarity_dist-1_angle-0.79 texture_ch-1_dissimilarity_dist-1_angle-1.57 texture_ch-1_dissimilarity_dist-1_angle-2.36 texture_ch-1_homogeneity_dist-1_angle-0.00 texture_ch-1_homogeneity_dist-1_angle-0.79 texture_ch-1_homogeneity_dist-1_angle-1.57 texture_ch-1_homogeneity_dist-1_angle-2.36 texture_ch-1_correlation_dist-1_angle-0.00 texture_ch-1_correlation_dist-1_angle-0.79 texture_ch-1_correlation_dist-1_angle-1.57 texture_ch-1_correlation_dist-1_angle-2.36 texture_ch-1_ASM_dist-1_angle-0.00 texture_ch-1_ASM_dist-1_angle-0.79 texture_ch-1_ASM_dist-1_angle-1.57 texture_ch-1_ASM_dist-1_angle-2.36 texture_ch-2_contrast_dist-1_angle-0.00 texture_ch-2_contrast_dist-1_angle-0.79 texture_ch-2_contrast_dist-1_angle-1.57 texture_ch-2_contrast_dist-1_angle-2.36 texture_ch-2_dissimilarity_dist-1_angle-0.00 texture_ch-2_dissimilarity_dist-1_angle-0.79 texture_ch-2_dissimilarity_dist-1_angle-1.57 texture_ch-2_dissimilarity_dist-1_angle-2.36 texture_ch-2_homogeneity_dist-1_angle-0.00 texture_ch-2_homogeneity_dist-1_angle-0.79 texture_ch-2_homogeneity_dist-1_angle-1.57 texture_ch-2_homogeneity_dist-1_angle-2.36 texture_ch-2_correlation_dist-1_angle-0.00 texture_ch-2_correlation_dist-1_angle-0.79 texture_ch-2_correlation_dist-1_angle-1.57 texture_ch-2_correlation_dist-1_angle-2.36 texture_ch-2_ASM_dist-1_angle-0.00 texture_ch-2_ASM_dist-1_angle-0.79 texture_ch-2_ASM_dist-1_angle-1.57 texture_ch-2_ASM_dist-1_angle-2.36
AAACGAGACGGTTGAT-1 42.783204 79.464035 41.904014 82.624826 1.983783 2.753093 1.973759 2.743151 0.753973 0.725217 0.753458 0.727817 0.989676 0.980799 0.989874 0.980027 0.257660 0.246727 0.257964 0.246895 1.074851 1.818339 1.128450 1.947829 0.555952 0.728388 0.566007 0.746670 0.760217 0.706197 0.757304 0.703203 0.988467 0.980503 0.987873 0.979115 0.126044 0.109685 0.125444 0.109163 3.719164 6.743719 4.043992 6.661864 1.106474 1.404873 1.103460 1.419202 0.567838 0.508652 0.570987 0.504941 0.883396 0.787901 0.872758 0.790485 0.040632 0.035577 0.041006 0.035397
AAAGGGATGTAGCAAG-1 82.756940 144.883230 76.546612 159.714604 3.349644 4.369327 3.171514 4.603538 0.692667 0.666414 0.696288 0.668449 0.989196 0.981083 0.990008 0.979096 0.184797 0.176518 0.184901 0.176277 10.298760 17.196961 9.024880 18.784308 1.472209 1.902758 1.400009 1.990153 0.643670 0.593335 0.644992 0.584962 0.995620 0.992694 0.996157 0.991982 0.043327 0.037119 0.043447 0.036352 5.188997 7.293595 4.433292 9.512128 1.300680 1.641199 1.269742 1.706421 0.533904 0.470301 0.538010 0.466651 0.938821 0.914061 0.947862 0.887927 0.016620 0.013672 0.016786 0.013555
AAATGGCATGTCTTGT-1 27.093979 48.276535 23.560334 49.362415 2.416785 3.209199 2.249740 3.271754 0.565910 0.525931 0.581019 0.517047 0.991710 0.985251 0.992796 0.984920 0.049270 0.044856 0.049755 0.044653 7.686629 15.158968 8.050239 14.615958 1.533212 2.103325 1.545864 2.088909 0.566223 0.501223 0.568412 0.501163 0.994528 0.989220 0.994271 0.989604 0.020876 0.017366 0.021290 0.017391 4.889899 8.797153 5.103539 8.590543 1.146626 1.475492 1.136618 1.472076 0.567434 0.503189 0.571515 0.503682 0.878716 0.781444 0.873200 0.786576 0.033804 0.028822 0.034247 0.028759
AAATGGTCAATGTGCC-1 24.198313 36.550901 18.040215 46.083141 2.222673 2.732854 1.925904 3.103483 0.645956 0.621034 0.661099 0.608436 0.995620 0.993373 0.996737 0.991662 0.115711 0.105768 0.116269 0.104839 0.291332 0.380815 0.289475 0.378077 0.289743 0.366076 0.287870 0.363701 0.855288 0.818436 0.856225 0.819587 0.699605 0.607720 0.701861 0.610545 0.311822 0.284697 0.312450 0.285439 2.076205 3.487123 2.163905 3.622057 1.033055 1.325574 1.032772 1.335698 0.575675 0.511713 0.578051 0.511233 0.988060 0.979815 0.987566 0.979121 0.016216 0.013678 0.016297 0.013659
AAATTAACGGGTAGCT-1 21.413928 39.826111 23.691475 47.908006 1.281552 1.779400 1.349581 1.883277 0.821503 0.798561 0.820337 0.797125 0.992404 0.985869 0.991574 0.983001 0.415389 0.398915 0.416979 0.398506 0.981407 1.711100 1.141055 1.991123 0.470454 0.601865 0.481864 0.636275 0.795344 0.751778 0.794664 0.745535 0.994745 0.990863 0.993890 0.989368 0.118716 0.103852 0.118967 0.102996 2.032095 3.256446 2.022826 3.435038 1.027862 1.291212 1.010441 1.315388 0.571921 0.511880 0.577136 0.507679 0.954380 0.926691 0.954430 0.922658 0.026097 0.022120 0.026564 0.022041


Use squidpy.pl.extract() to plot the texture features on the tissue image or have a look at our interactive visualization tutorial to learn how to use our interactive napari plugin. Here, we show the contrast feature for channels 0 and 1. The two stains, DAPI in channel 0, and GFAP in channel 1 show different regions of high contrast.

sc.pl.spatial(
    sq.pl.extract(adata, "texture_features"),
    color=[None, "texture_ch-0_contrast_dist-1_angle-0.00", "texture_ch-1_contrast_dist-1_angle-0.00"],
    bw=True,
)
texture_ch-0_contrast_dist-1_angle-0.00, texture_ch-1_contrast_dist-1_angle-0.00

Total running time of the script: ( 1 minutes 21.163 seconds)

Estimated memory usage: 676 MB