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
qim3d.features.area
Compute the surface area of an object from a volume or mesh using the Pygel3D library.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
ndarray or Manifold
|
Input |
required |
mask |
ndarray or None
|
Boolean mask to apply for a region of interest in the volume. Must match the shape of the input volume. Defaults to None. |
None
|
threshold |
(float, str)
|
Threshold value for binarization of the input volume. If 'otsu', Otsu's method is used. Defaults to 'otsu'. |
'otsu'
|
Returns:
Name | Type | Description |
---|---|---|
area |
float
|
The surface area of the object. |
Raises:
Type | Description |
---|---|
ValueError
|
If the mask shape does not match the volume shape. |
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
Compute the volume of an object from a volume or mesh using the Pygel3D library.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
ndarray or Manifold
|
Input |
required |
mask |
ndarray or None
|
Boolean mask to apply for a region of interest in the volume. Must match the shape of the input volume. Defaults to None. |
None
|
threshold |
(float, str)
|
Threshold value for binarization of the input volume. If 'otsu', Otsu's method is used. Defaults to 'otsu'. |
'otsu'
|
Returns:
Name | Type | Description |
---|---|---|
volume |
float
|
The volume of the object. |
Raises:
Type | Description |
---|---|
ValueError
|
If the mask shape does not match the volume shape. |
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
Compute the size (maximum side length of the bounding box enclosing the object) of an object from a volume or mesh.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
ndarray or Manifold
|
Input |
required |
mask |
ndarray or None
|
Boolean mask to apply for a region of interest in the volume. Must match the shape of the input volume. Defaults to None. |
None
|
threshold |
(float, str)
|
Threshold value for binarization of the input volume. If 'otsu', Otsu's method is used. Defaults to 'otsu'. |
'otsu'
|
Returns:
Name | Type | Description |
---|---|---|
size |
float
|
The size of the object, defined as the maximum side length of the bounding box enclosing the object. |
Raises:
Type | Description |
---|---|
ValueError
|
If the mask shape does not match the volume shape. |
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
Compute the sphericity of an object from a volume or mesh.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
ndarray or Manifold
|
Input |
required |
mask |
ndarray or None
|
Boolean mask to apply for a region of interest in the volume. Must match the shape of the input volume. Defaults to None. |
None
|
threshold |
(float, str)
|
Threshold value for binarization of the input volume. If 'otsu', Otsu's method is used. Defaults to 'otsu'. |
'otsu'
|
Returns:
Name | Type | Description |
---|---|---|
sphericity |
float
|
The sphericity of the object. Higher values indicate a more spherical shape. |
Raises:
Type | Description |
---|---|
ValueError
|
If the mask shape does not match the volume shape. |
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
qim3d.features.roughness
Compute the roughness (ratio between surface area and volume) of an object from a volume or mesh.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
ndarray or Manifold
|
Input |
required |
mask |
ndarray or None
|
Boolean mask to apply for a region of interest in the volume. Must match the shape of the input volume. Defaults to None. |
None
|
threshold |
(float, str)
|
Threshold value for binarization of the input volume. If 'otsu', Otsu's method is used. Defaults to 'otsu'. |
'otsu'
|
Returns:
Name | Type | Description |
---|---|---|
roughness |
float
|
The roughness of the object, defined as the ratio between surface area and volume. Higher roughness indicates a more complex surface. |
Raises:
Type | Description |
---|---|
ValueError
|
If the mask shape does not match the volume shape. |
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
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 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 |
|
qim3d.features.mean_std_intensity
Compute the mean and standard deviation of intensities in a volume.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
volume |
ndarray
|
Input |
required |
mask |
ndarray or None
|
Boolean mask to apply for a region of interest in the volume. Must match the shape of the input volume. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple[float, float]
|
Mean and standard deviation of intensities. |
Raises:
Type | Description |
---|---|
ValueError
|
If the mask shape does not match the volume shape. |
Note
- The background (intensities of 0) is excluded from the computation.
- If a mask is provided, it will only compute the mean and standard deviation for that region of interest.
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
