bbox_to_slices#

Import packages#

import numpy as np
import porespy as ps
import scipy.ndimage as spim
import matplotlib.pyplot as plt
import skimage
ps.visualization.set_mpl_style()

Generate image for testing#

np.random.seed(0)
im = ps.generators.blobs([500, 500])
im3d = ps.generators.blobs([100, 100,100])

Visualize the images

fig, ax = plt.subplots(1, 2, figsize=[8, 4]);
ax[0].imshow(im);
ax[0].axis(False)
ax[0].set_title('2D image')
ax[1].imshow(im3d[25,...]);
ax[1].axis(False)
ax[1].set_title('3D image');
../../../_images/d87ce828431eda057982899506f5a44b89551fe776738a7bb599609c93f698d0.png

Demonstration of function#

Define some bounding boxes in 2D and 3D:

bbox3d = [0, 0, 0, 50, 50, 50]
bbox2d = [0, 0, 50, 50]

The bounding box as defined by most packages are given as lists without much context as to how the values should be used. The bbox_to_slices function returns a tuple of slice objects than can be used to directly index into a ND-array to retrieve the area defined by the bounding box:

box2d = ps.tools.bbox_to_slices(bbox = bbox2d)
box3d = ps.tools.bbox_to_slices(bbox = bbox3d)
fig, ax = plt.subplots(1, 2, figsize=[7, 7])
ax[0].imshow(im[box2d])
ax[0].axis(False)
ax[0].set_title('2D')

ax[1].imshow(im3d[box3d][25, ...])
ax[1].axis(False)
ax[1].set_title('3D')
Text(0.5, 1.0, '3D')
../../../_images/c13efc1ab9ab0912e5d784cce41f7aca35cec507f5e8d2490d90416e9a3692b3.png