2.6.8.15. 查找物体的边界框

此示例演示如何提取最大物体的边界框

plot find object
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
rng = np.random.default_rng(27446968)
n = 10
l = 256
im = np.zeros((l, l))
points = l * rng.random((2, n**2))
im[(points[0]).astype(int), (points[1]).astype(int)] = 1
im = sp.ndimage.gaussian_filter(im, sigma=l / (4.0 * n))
mask = im > im.mean()
label_im, nb_labels = sp.ndimage.label(mask)
# Find the largest connected component
sizes = sp.ndimage.sum(mask, label_im, range(nb_labels + 1))
mask_size = sizes < 1000
remove_pixel = mask_size[label_im]
label_im[remove_pixel] = 0
labels = np.unique(label_im)
label_im = np.searchsorted(labels, label_im)
# Now that we have only one connected component, extract it's bounding box
slice_x, slice_y = sp.ndimage.find_objects(label_im == 4)[0]
roi = im[slice_x, slice_y]
plt.figure(figsize=(4, 2))
plt.axes((0, 0, 1, 1))
plt.imshow(roi)
plt.axis("off")
plt.show()

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

由 Sphinx-Gallery 生成的图库