Feature extraction
The qim3d library provides a set of methods for feature extraction on volumetric data.
General usage
- All features assume a single connected object in the input.
- All feature functions accept either a 3D volume or a mesh as input.
- If a volume is provided, it is typically binarized (using the provided threshold or Otsu's method by default) and converted to a mesh before feature extraction.
- A mask can be provided to restrict feature extraction to a specific region of interest in the volume.
- If a mesh is provided, the threshold and/or mask arguments are ignored.
Efficient feature extraction
Before extracting multiple features, convert your input volume to a mesh using qim3d.mesh.from_volume for best performance. This avoids repeated volume-to-mesh conversions under the hood during feature extraction.
qim3d.features.area
Calculates the total surface area of a 3D object.
This function quantifies the surface size of a structure, which is a fundamental metric in morphometry and material science. It accepts either a raw 3D volume (which is automatically meshed using Marching Cubes) or a pre-computed PyGEL3D mesh.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object
|
ndarray or Manifold
|
The input data. Can be a 3D NumPy array (volume) or a |
required |
mask
|
ndarray
|
A boolean mask defining a Region of Interest (ROI) within the volume. Must have the same shape as |
None
|
threshold
|
float or str
|
The intensity value used to binarize the volume if a NumPy array is provided.
|
'otsu'
|
Returns:
| Name | Type | Description |
|---|---|---|
area |
float
|
The computed surface area in square units (determined by the input resolution). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
Compute area from a np.ndarray volume:
Example
Compute area from a pygel3d.hmesh.Manifold mesh:
Source code in qim3d/features/_common_features_methods.py
qim3d.features.volume
Computes the enclosed physical volume of a 3D object.
This function quantifies the amount of space occupied by a structure. It is robust and versatile, handling both raw 3D image arrays (voxels) and pre-generated meshes. If a voxel array is provided, it is automatically converted into a mesh using the specified threshold before the volume is calculated, ensuring sub-voxel accuracy compared to simple voxel counting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object
|
ndarray or Manifold
|
The input data. Can be a 3D NumPy array (volume) or a |
required |
mask
|
ndarray
|
A boolean mask defining a Region of Interest (ROI). Only the volume within this mask is included. Must have the same shape as |
None
|
threshold
|
float or str
|
The intensity value used to binarize the volume if a NumPy array is provided.
|
'otsu'
|
Returns:
| Name | Type | Description |
|---|---|---|
volume |
float
|
The calculated volume in cubic units (determined by the input resolution). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
import qim3d
import numpy as np
# Generate a synthetic object
synthetic_object = qim3d.generate.volume(noise_scale=0.01, final_shape=(100, 100, 100))
# Create a mask for the bottom right corner
mask = np.zeros_like(synthetic_object, dtype=bool)
mask[50:100, 50:100, 50:100] = True
# Compute the volume of the object within the region of interest defined by the mask
volume = qim3d.features.volume(synthetic_object, threshold=50, mask=mask)
print(volume)
Source code in qim3d/features/_common_features_methods.py
qim3d.features.size
Calculates the maximum dimension (size) of the object's bounding box.
This function determines the largest spatial extent of the object along the Cartesian axes (X, Y, Z). It computes the Axis-Aligned Bounding Box (AABB) enclosing the structure and returns the length of its longest side. This metric is useful for characterizing the overall scale, fitting constraints, or the maximum caliper diameter along orthogonal axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object
|
ndarray or Manifold
|
The input data. Can be a 3D NumPy array (volume) or a |
required |
mask
|
ndarray
|
A boolean mask defining a Region of Interest (ROI). Only the data within this mask is considered. Must have the same shape as |
None
|
threshold
|
float or str
|
The intensity value used to binarize the volume if a NumPy array is provided.
|
'otsu'
|
Returns:
| Name | Type | Description |
|---|---|---|
size |
float
|
The length of the largest side of the enclosing bounding box. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
import qim3d
# Generate a synthetic object
synthetic_object = qim3d.generate.volume(
final_shape=(100,30,30),
noise_scale=0.008,
shape="cylinder"
)
# Compute size of the object
size = qim3d.features.size(synthetic_object)
print(f"Size: {size}")
# Visualize the synthetic object
qim3d.viz.volumetric(synthetic_object)
Source code in qim3d/features/_common_features_methods.py
qim3d.features.sphericity
Computes the sphericity (compactness) of a 3D object.
Sphericity is a measure of how closely the shape of an object resembles a perfect sphere. It is defined as the ratio of the surface area of a sphere (with the same volume as the given object) to the actual surface area of the object. This morphological descriptor is widely used in particle analysis, geology, and biology to quantify roundness and regularity.
- 1.0: Indicates a perfect sphere.
- < 1.0: Indicates irregular, elongated, or rough shapes (e.g., a cube has a sphericity of approx. 0.806).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object
|
ndarray or Manifold
|
The input data. Can be a 3D NumPy array (volume) or a |
required |
mask
|
ndarray
|
A boolean mask defining a Region of Interest (ROI). Only the data within this mask is considered. Must have the same shape as |
None
|
threshold
|
float or str
|
The intensity value used to binarize the volume if a NumPy array is provided.
|
'otsu'
|
Returns:
| Name | Type | Description |
|---|---|---|
sphericity |
float
|
The calculated sphericity value (unitless, max 1.0). Returns |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
import qim3d
# Generate a synthetic object
synthetic_object = qim3d.generate.volume(noise_scale=0.005)
# Compute the sphericity of the object
sphericity = qim3d.features.sphericity(synthetic_object)
print(f"Sphericity: {sphericity:.4f}")
# Visualize the synthetic object
qim3d.viz.volumetric(synthetic_object)
Example
import qim3d
# Generate a synthetic object
synthetic_object = qim3d.generate.volume(noise_scale=0.008)
# Manipulate the object
synthetic_object = qim3d.operations.stretch(synthetic_object, z_stretch=50)
synthetic_object = qim3d.operations.curve_warp(synthetic_object, x_amp=10, x_periods=4)
# Compute the sphericity of the object
sphericity = qim3d.features.sphericity(synthetic_object)
print(f"Sphericity: {sphericity:.4f}")
# Visualize the synthetic object
qim3d.viz.volumetric(synthetic_object)
Source code in qim3d/features/_common_features_methods.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
qim3d.features.roughness
Computes the roughness (Surface-Area-to-Volume ratio) of a 3D object.
This feature quantifies the morphological complexity of an object. Unlike a simple measure of size, this ratio indicates how "folded", "spiky", or "porous" a structure is relative to its mass. A perfect sphere has the lowest possible surface-to-volume ratio (smoothest), while highly complex shapes like fractals, trabecular bone, or dendrites will have a much higher value. This metric is often used to analyze texture and surface irregularity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object
|
ndarray or Manifold
|
The input data. Can be a 3D NumPy array (volume) or a |
required |
mask
|
ndarray
|
A boolean mask defining a Region of Interest (ROI). Only the data within this mask is considered. Must have the same shape as |
None
|
threshold
|
float or str
|
The intensity value used to binarize the volume if a NumPy array is provided.
|
'otsu'
|
Returns:
| Name | Type | Description |
|---|---|---|
roughness |
float
|
The calculated ratio of Surface Area / Volume. Returns |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
import qim3d
# Generate a synthetic object
synthetic_object = qim3d.generate.volume(
base_shape=(128,128,128),
noise_scale=0.019,
)
# Compute the roughness of the object
roughness = qim3d.features.roughness(synthetic_object)
print(f"Roughness: {roughness:.4f}")
# Visualize the synthetic object
qim3d.viz.volumetric(synthetic_object)
Example
import qim3d
# Generate a synthetic object
synthetic_object = qim3d.generate.volume(
base_shape=(128,128,128),
noise_scale=0.08,
decay_rate=18,
gamma=0.9,
)
# Compute the roughness of the object
roughness = qim3d.features.roughness(synthetic_object)
print(f"Roughness: {roughness:.4f}")
# Visualize the synthetic object
qim3d.viz.volumetric(synthetic_object)
Source code in qim3d/features/_common_features_methods.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | |
qim3d.features.mean_std_intensity
Calculates the mean (average) and standard deviation of voxel intensities within a volume.
This function computes global image statistics, which are fundamental for tasks like data normalization (z-score), contrast analysis, and quality control. By default, it ignores zero-valued voxels (background) to ensure the statistics reflect the actual object or signal rather than the empty space around it. You can further restrict the calculation to a specific Region of Interest (ROI) using a binary mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
ndarray
|
The input 3D image stack (voxel data). |
required |
mask
|
ndarray
|
A boolean mask defining a specific region to analyze. Only voxels where |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
stats |
tuple[float, float]
|
A tuple containing
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Note
This function automatically filters out background values (intensities equal to 0) before calculation.
Example
import qim3d
# Load a sample object
shell_object = qim3d.examples.shell_225x128x128
# Compute mean and standard deviation of intensities in the object
mean_intensity, std_intensity = qim3d.features.mean_std_intensity(shell_object)
print(f"Mean intensity: {mean_intensity:.4f}")
print(f"Standard deviation of intensity: {std_intensity:.4f}")
# Visualize slices of the object
qim3d.viz.slices_grid(shell_object, color_bar=True, color_bar_style="large")
Standard deviation of intensity: 45.8481