Parametric maps#

This notebook demonstrates how parametric maps can be made. In such parametric images, pixel intensity corresponds to measurements of the objects, for example area.

import numpy as np
import pyclesperanto_prototype as cle
from skimage.io import imread, imsave
from skimage.measure import regionprops, regionprops_table
from skimage.util import map_array
from napari_simpleitk_image_processing import label_statistics

Starting point for drawing parametric maps is always a label image.

binary = cle.artificial_objects_2d()
labels = cle.voronoi_otsu_labeling(binary, spot_sigma=5)

cle.imshow(labels, labels=True)
../_images/aad956d098c88c1cd750a899811ea8945e4086fb81c5154d06c1e4988c7fae97.png

Parametric maps using scikit-image#

You can also compute your own measurement for each object and then visualize it in a parametric map image. Consider using scikit-image’s regionprops() for computing the measurements.

statistics_table = regionprops_table(cle.pull(labels), properties=('label', 'area',))

Area map#

remapped = map_array(
        cle.pull(labels),
        statistics_table['label'],
        statistics_table['area'],
        )

cle.imshow(remapped, colorbar=True, color_map="jet")
../_images/7cb6659a1576604736201f7c7e27d390ea48adab71f37bbc7b9c9cd79cc45750.png

Parametric maps in pyclesperanto#

Alternatively, we can use the function cle.replace_intensities for making these parametric maps. It expects a label image and a list of measurements ordered by label, starting with a measurement corresponding to background (index 0). In the list of measurements, no label should be missing. Consider to relabel the label image sequentially if necessary.

statistics = regionprops(cle.pull(labels))
# we're appending a [0] for the "measurement" of the background and attach all values behind
area = [0] + [s.area for s in statistics]

area_map2 = cle.replace_intensities(labels, area)

cle.imshow(area_map2, colorbar=True, color_map="jet")
../_images/7cb6659a1576604736201f7c7e27d390ea48adab71f37bbc7b9c9cd79cc45750.png

Pixel count map#

pyclesperanto comes with some maps built-in. For example the pixel count map derived from a label image expresses area or volume of objects in colour.

pixel_count_map = cle.label_pixel_count_map(labels)

cle.imshow(pixel_count_map, color_map='jet', colorbar=True)
/Users/haase/code/pyclesperanto_prototype/pyclesperanto_prototype/_tier9/_statistics_of_labelled_pixels.py:283: RuntimeWarning: invalid value encountered in true_divide
  region_props['mean_max_distance_to_centroid_ratio'] = region_props['max_distance_to_centroid'] / region_props[
/Users/haase/code/pyclesperanto_prototype/pyclesperanto_prototype/_tier9/_statistics_of_labelled_pixels.py:285: RuntimeWarning: invalid value encountered in true_divide
  region_props['mean_max_distance_to_mass_center_ratio'] = region_props['max_distance_to_mass_center'] / region_props[
../_images/7cb6659a1576604736201f7c7e27d390ea48adab71f37bbc7b9c9cd79cc45750.png

Parametric maps using SimpleITK-based measurements#

Furthermore, also SimpleITK comes with quantitative measurements for labeled images. For convenience reasons, we will use the scriptable napari plugin napari-simpleitk-image-processing for deriving measurements.

statistics_sitk = label_statistics(labels, labels, size=True, shape=True, perimeter=True, intensity=False)

print(statistics_sitk.keys())
dict_keys(['label', 'elongation', 'feret_diameter', 'flatness', 'roundness', 'equivalent_ellipsoid_diameter_0', 'equivalent_ellipsoid_diameter_1', 'equivalent_spherical_perimeter', 'equivalent_spherical_radius', 'number_of_pixels', 'number_of_pixels_on_border', 'perimeter', 'perimeter_on_border', 'perimeter_on_border_ratio'])

Number of pixels map#

# we're appending a [0] for the "measurement" of the background and attach all values behind
number_of_pixels = [0] + statistics_sitk['number_of_pixels']

number_of_pixels_map = cle.replace_intensities(labels, number_of_pixels)

cle.imshow(number_of_pixels_map, colorbar=True, color_map="jet")
../_images/7cb6659a1576604736201f7c7e27d390ea48adab71f37bbc7b9c9cd79cc45750.png

Extension ratio map#

The extension ratio is a shape descriptor derived from the maximum distance of pixels to their object’s centroid divided by the average distance of pixels to the centroid. It alllows to differentiate elongated from roundish objects.

extension_ratio_map = cle.extension_ratio_map(labels)

cle.imshow(extension_ratio_map, 
           color_map='jet', 
           colorbar=True, 
           min_display_intensity=1.5, 
           max_display_intensity=2.5)
../_images/588e3bf24b39937fa183db437b4cacf7e0892bad3bf002da0874ef7b9c455b2e.png

Mean / minimum / maximum / standard-deviation intensity map#

If we use additionally an intensity image, we can also produce parametric images showing intensity measurements.

blobs = cle.scale(imread('../../data/blobs.tif'), factor_x=2, factor_y=2, auto_size=True)

cle.imshow(blobs)
../_images/f9977ffbee3204901aed047a5146e3a29f3c91ed42c01d308674949ea8dff329.png
mean_intensity_map = cle.label_mean_intensity_map(blobs, labels)
cle.imshow(mean_intensity_map, 
           color_map='jet', 
           colorbar=True,
           min_display_intensity=100, 
           max_display_intensity=250)
../_images/b1594a1ff57cef6ec4f01267cbe9fa468c5e703bb8a849163f128fa31eada23f.png
maximum_intensity_map = cle.maximum_intensity_map(blobs, labels)
cle.imshow(maximum_intensity_map, 
           color_map='jet', 
           colorbar=True, 
           min_display_intensity=200, 
           max_display_intensity=270)
../_images/7f0f05094a6a2396a6c42b08d7c23306c38e3720448f19b57221b7e972a34a24.png
stddev_intensity_map = cle.standard_deviation_intensity_map(blobs, labels)
cle.imshow(stddev_intensity_map, color_map='jet', colorbar=True)
../_images/466e9d00f653a791c929289278b66b3cd5c9b3929205e12d98e367ae9e23cc59.png

Aspect ratio map#

# we're appending a [0] for the "measurement" of the background and attach all values behind
aspect_ratio = [0]

for s in statistics:
    if s.minor_axis_length:
        aspect_ratio.append(s.major_axis_length / s.minor_axis_length)
    else:
        aspect_ratio.append(0) # note: an aspect ratio of 0 is an "invalid" value
aspect_ratio_map = cle.replace_intensities(labels, aspect_ratio)

cle.imshow(aspect_ratio_map, colorbar=True, color_map="jet")
../_images/0f983e015430facd5e0dbea8e2df3c1b64e387c9de6de849ef11c563abe2c410.png

Elongation map#

# we're appending a [0] for the "measurement" of the background and attach all values behind
eccentricity = [0] + [s.eccentricity for s in statistics]

eccentricity_map = cle.replace_intensities(labels, eccentricity)

cle.imshow(eccentricity_map, colorbar=True, color_map="jet")
../_images/44a6080c6c7b3a58b853e5e2ccbe8e2cf703437c879a5b5cdb4fe9cf413d3f1c.png

Extent map#

# we're appending a [0] for the "measurement" of the background and attach all values behind
extent = [0] + [s.extent for s in statistics]

extent_map = cle.replace_intensities(labels, extent)

cle.imshow(extent_map, colorbar=True, color_map="jet")
../_images/08b4fd4e70721fbf04fd4001ab7550d269e5f591b2a2840a9e3d0e11d627b2ce.png

Feret diameter map#

# we're appending a [0] for the "measurement" of the background and attach all values behind
feret_diameter = [0] + [s.feret_diameter_max for s in statistics]

feret_diameter_map = cle.replace_intensities(labels, feret_diameter)

cle.imshow(feret_diameter_map, colorbar=True, color_map="jet")
../_images/da9cf0cb58a505e8c51d833bf3d494db0019fe8a235dce5609ae4ae68e9d867a.png

Orientation map#

# we're appending a [0] for the "measurement" of the background and attach all values behind
orientation = [0] + [s.orientation for s in statistics]

orientation_map = cle.replace_intensities(labels, orientation)

cle.imshow(orientation_map, colorbar=True, color_map="jet")
../_images/552466e7185560b7d1c6a4e5aca66758d6b5928edcc0dfdc6f0b0a542f4e890d.png

Perimeter map#

# we're appending a [0] for the "measurement" of the background and attach all values behind
perimeter_ski = [0] + [s.perimeter for s in statistics]

perimeter_ski_map = cle.replace_intensities(labels, perimeter_ski)

cle.imshow(perimeter_ski_map, colorbar=True, color_map="jet")
../_images/428edba63db70daf8d8cc6e29d231d2bb5aec8a2ef62184b8aa3239946e96f24.png

Solidity map#

# we're appending a [0] for the "measurement" of the background and attach all values behind
solidity = [0] + [s.solidity for s in statistics]

solidity_map = cle.replace_intensities(labels, solidity)

cle.imshow(solidity_map, colorbar=True, color_map="jet")
../_images/2e549400472345a3dace1616ea73c9aba08e4beda950b134ed94fe5ad52e02f6.png

Elongation map#

# we're appending a [0] for the "measurement" of the background and attach all values behind
elongation = [0] + statistics_sitk['elongation']

elongation_map = cle.replace_intensities(labels, elongation)

cle.imshow(elongation_map, colorbar=True, color_map="jet")
../_images/0f983e015430facd5e0dbea8e2df3c1b64e387c9de6de849ef11c563abe2c410.png

Flatness map#

# we're appending a [0] for the "measurement" of the background and attach all values behind
flatness = [0] + statistics_sitk['flatness']

flatness_map = cle.replace_intensities(labels, flatness)

cle.imshow(flatness_map, colorbar=True, color_map="jet")
../_images/0f983e015430facd5e0dbea8e2df3c1b64e387c9de6de849ef11c563abe2c410.png

Roundness map#

# we're appending a [0] for the "measurement" of the background and attach all values behind
roundness = [0] + statistics_sitk['roundness']

roundness_map = cle.replace_intensities(labels, roundness)

cle.imshow(roundness_map, colorbar=True, color_map="jet")
../_images/f6fdcd3134d52a6a81fe83094bcbec2e63188f492172b62b1d7421215854b460.png

Perimeter map#

# we're appending a [0] for the "measurement" of the background and attach all values behind
perimeter_sitk = [0] + statistics_sitk['perimeter']

perimeter_sitk_map = cle.replace_intensities(labels, perimeter_sitk)

cle.imshow(perimeter_sitk_map, colorbar=True, color_map="jet")
../_images/0c800b05c3399ea15d94b339e2ff7f014cd6a266e3ce0490bcbe0d54224f15e9.png

Quality assurance#

If you generate a parametric image with the “label” column, the parametric image should actually be equal to the label input image.

# we're appending a [0] for the "measurement" of the background and attach all values behind
label = [0] + statistics_sitk['label']

label_map = cle.replace_intensities(labels, label)

cle.imshow(label_map, labels=True)
../_images/aad956d098c88c1cd750a899811ea8945e4086fb81c5154d06c1e4988c7fae97.png
label_difference = labels - label_map

cle.imshow(label_difference)
../_images/6ffee9f29a5ce5ca2277c63282377ec0ff6b56c3475e37d2d6351ad31f6ee537.png
label_difference.min(), label_difference.max()
(0.0, 0.0)

When comparing perimeter measurements from scikit-image and SimpleITK, we could see small differences. Here we can visualize which objects are affected and to what degree.

perimeter_difference = perimeter_sitk_map - perimeter_ski_map

cle.imshow(perimeter_difference, colorbar=True, color_map="jet")
../_images/e8f1e7f026f32e49fa16b2540cf13dcebe573bbe48fa106ccddcdc72502cf6b9.png