备注
前往结尾 下载完整示例代码。或通过 Binder 在浏览器中运行此示例。
使用不同的估计器测量周长#
在这个例子中,我们展示了测量周长的误差,比较了经典近似和Crofton近似。为此,我们估计了一个对象(正方形或圆盘)及其旋转版本的周长,随着旋转角度的增加。
import matplotlib.pyplot as plt
import numpy as np
from skimage.measure import perimeter, perimeter_crofton
from skimage.transform import rotate
# scale parameter can be used to increase the grid size. The resulting curves
# should be smoothed with higher scales
scale = 10
# Construct two objects, a square and a disk
square = np.zeros((100 * scale, 100 * scale))
square[40 * scale : 60 * scale, 40 * scale : 60 * scale] = 1
[X, Y] = np.meshgrid(np.linspace(0, 100 * scale), np.linspace(0, 100 * scale))
R = 20 * scale
disk = (X - 50 * scale) ** 2 + (Y - 50 * scale) ** 2 <= R**2
fig, axes = plt.subplots(1, 2, figsize=(8, 5))
ax = axes.flatten()
dX = X[0, 1] - X[0, 0]
true_perimeters = [80 * scale, 2 * np.pi * R / dX]
# For each type of object, estimate its perimeter as the object is rotated,
# according to different approximations
for index, obj in enumerate([square, disk]):
# `neighborhood` value can be 4 or 8 for the classic perimeter estimator
for n in [4, 8]:
p = []
angles = range(90)
for i in angles:
rotated = rotate(obj, i, order=0)
p.append(perimeter(rotated, n))
ax[index].plot(angles, p)
# `directions` value can be 2 or 4 for the Crofton estimator
for d in [2, 4]:
p = []
angles = np.arange(0, 90, 2)
for i in angles:
rotated = rotate(obj, i, order=0)
p.append(perimeter_crofton(rotated, d))
ax[index].plot(angles, p)
ax[index].axhline(true_perimeters[index], linestyle='--', color='k')
ax[index].set_xlabel('Rotation angle')
ax[index].legend(
[
'N4 perimeter',
'N8 perimeter',
'Crofton 2 directions',
'Crofton 4 directions',
'Ground truth',
],
loc='best',
)
ax[index].set_ylabel('Perimeter of the rotated object')
ax[0].set_title('Square')
ax[1].set_title('Disk')
plt.tight_layout()
plt.show()
脚本的总运行时间: (0 分钟 17.770 秒)