使用 pandas 探索和可视化区域属性#

此玩具示例展示了如何计算一系列 10 张图像中每个标记区域的大小。我们使用 2D 图像,然后使用 3D 图像。类斑点区域是合成生成的。随着体积分数(即,斑点覆盖的像素或体素的比例)的增加,斑点(区域)的数量减少,并且单个区域的大小(面积或体积)可以变得越来越大。面积(大小)值以 pandas 兼容的格式提供,这使得数据分析和可视化非常方便。

除了面积之外,还有许多其他区域属性可用。

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

from skimage import data, measure


fractions = np.linspace(0.05, 0.5, 10)

2D 图像#

images = [data.binary_blobs(volume_fraction=f) for f in fractions]

labeled_images = [measure.label(image) for image in images]

properties = ['label', 'area']

tables = [
    measure.regionprops_table(image, properties=properties) for image in labeled_images
]
tables = [pd.DataFrame(table) for table in tables]

for fraction, table in zip(fractions, tables):
    table['volume fraction'] = fraction

areas = pd.concat(tables, axis=0)

# Create custom grid of subplots
grid = plt.GridSpec(2, 2)
ax1 = plt.subplot(grid[0, 0])
ax2 = plt.subplot(grid[0, 1])
ax = plt.subplot(grid[1, :])
# Show image with lowest volume fraction
ax1.imshow(images[0], cmap='gray_r')
ax1.set_axis_off()
ax1.set_title(f'fraction {fractions[0]}')
# Show image with highest volume fraction
ax2.imshow(images[-1], cmap='gray_r')
ax2.set_axis_off()
ax2.set_title(f'fraction {fractions[-1]}')
# Plot area vs volume fraction
areas.plot(x='volume fraction', y='area', kind='scatter', ax=ax)
plt.show()
fraction 0.05, fraction 0.5

在散点图中,许多点似乎在低面积值处重叠。为了更好地了解分布情况,我们可能希望在可视化中添加一些“抖动”。为此,我们使用 seaborn.stripplot (来自 seaborn 库,用于统计数据可视化),并使用参数 jitter=True

fig, ax = plt.subplots()
sns.stripplot(x='volume fraction', y='area', data=areas, jitter=True, ax=ax)
# Fix floating point rendering
ax.set_xticklabels([f'{frac:.2f}' for frac in fractions])
plt.show()
plot regionprops table
/home/runner/work/scikit-image/scikit-image/doc/examples/segmentation/plot_regionprops_table.py:75: UserWarning:

set_ticklabels() should only be used with a fixed number of ticks, i.e. after set_ticks() or using a FixedLocator.

3D 图像#

在 3D 中进行相同的分析,我们发现了一个更加戏剧性的行为:当体积分数超过 ~0.25 时,斑点会合并成一个巨大的整体。这对应于统计物理和图论中的渗流阈值

images = [data.binary_blobs(length=128, n_dim=3, volume_fraction=f) for f in fractions]

labeled_images = [measure.label(image) for image in images]

properties = ['label', 'area']

tables = [
    measure.regionprops_table(image, properties=properties) for image in labeled_images
]
tables = [pd.DataFrame(table) for table in tables]

for fraction, table in zip(fractions, tables):
    table['volume fraction'] = fraction

blob_volumes = pd.concat(tables, axis=0)

fig, ax = plt.subplots()
sns.stripplot(x='volume fraction', y='area', data=blob_volumes, jitter=True, ax=ax)
ax.set_ylabel('blob size (3D)')
# Fix floating point rendering
ax.set_xticklabels([f'{frac:.2f}' for frac in fractions])
plt.show()
plot regionprops table
/home/runner/work/scikit-image/scikit-image/doc/examples/segmentation/plot_regionprops_table.py:107: UserWarning:

set_ticklabels() should only be used with a fixed number of ticks, i.e. after set_ticks() or using a FixedLocator.

脚本的总运行时间: (0 分钟 4.787 秒)

由 Sphinx-Gallery 生成的图库