Operations on volumetric data
The qim3d
library provides a set of methods for different operations on volumes.
qim3d.operations
Operations on volumes.
qim3d.operations.remove_background
remove_background(vol, median_filter_size=2, min_object_radius=3, background='dark', **median_kwargs)
Remove background from a volume using a qim3d filters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vol |
ndarray
|
The volume to remove background from. |
required |
median_filter_size |
int
|
The size of the median filter. Defaults to 2. |
2
|
min_object_radius |
int
|
The radius of the structuring element for the tophat filter. Defaults to 3. |
3
|
background |
'dark' or 'bright
|
The background type. Can be 'dark' or 'bright'. Defaults to 'dark'. |
'dark'
|
**median_kwargs |
Any
|
Additional keyword arguments for the Median filter. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
filtered_vol |
ndarray
|
The volume with background removed. |
Example
import qim3d
vol = qim3d.examples.cement_128x128x128
fig1 = qim3d.viz.slices_grid(vol, value_min=0, value_max=255, num_slices=5, display_figure=True)

vol_filtered = qim3d.operations.remove_background(vol,
min_object_radius=3,
background="bright")
fig2 = qim3d.viz.slices_grid(vol_filtered, value_min=0, value_max=255, num_slices=5, display_figure=True)

Source code in qim3d/operations/_common_operations_methods.py
qim3d.operations.fade_mask
Apply edge fading to a volume.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vol |
ndarray
|
The volume to apply edge fading to. |
required |
decay_rate |
float
|
The decay rate of the fading. Defaults to 10. |
10
|
ratio |
float
|
The ratio of the volume to fade. Defaults to 0.5. |
0.5
|
geometry |
spherical or cylindrical
|
The geometric shape of the fading. Can be 'spherical' or 'cylindrical'. Defaults to 'spherical'. |
'spherical'
|
invert |
bool
|
Flag for inverting the fading. Defaults to False. |
False
|
axis |
int
|
The axis along which to apply the fading. Defaults to 0. |
0
|
**kwargs |
Any
|
Additional keyword arguments for the edge fading. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
faded_vol |
ndarray
|
The volume with edge fading applied. |
Example
Image before edge fading has visible artifacts from the support. Which obscures the object of interest.
import qim3d
vol_faded = qim3d.operations.fade_mask(vol, decay_rate=4, ratio=0.45, geometric='cylindrical')
qim3d.viz.volumetrics(vol_faded)

Source code in qim3d/operations/_common_operations_methods.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
qim3d.operations.overlay_rgb_images
Overlay an RGB foreground onto an RGB background using alpha blending.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
background |
ndarray
|
The background RGB image. |
required |
foreground |
ndarray
|
The foreground RGB image (usually masks). |
required |
alpha |
float
|
The alpha value for blending. Defaults to 0.5. |
0.5
|
hide_black |
bool
|
If True, black pixels will have alpha value 0, so the black won't be visible. Used for segmentation where we don't care about background. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
composite |
ndarray
|
The composite RGB image with overlaid foreground. |
Raises:
Type | Description |
---|---|
ValueError
|
If input images have different shapes. |
Note
- The function performs alpha blending to overlay the foreground onto the background.
- It ensures that the background and foreground have the same first two dimensions (image size matches).
- It can handle greyscale images, values from 0 to 1, raw values which are negative or bigger than 255.
- It calculates the maximum projection of the foreground and blends them onto the background.
Source code in qim3d/operations/_common_operations_methods.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 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 |
|
qim3d.operations.pad
Pads the input 3D volume.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
volume |
ndarray
|
The input 3D volume (shape: n, h, w). |
required |
x_axis |
float
|
Amount of pixels to pad the x-dimension. Must be an integer or a half-integer (e.g., 5, 5.5). The padding is symmetric and applied to both sides of the volume. Defaults to 0. |
0
|
y_axis |
float
|
Amount of pixels to pad the y-dimension. Must be an integer or a half-integer. Defaults to 0. |
0
|
z_axis |
float
|
Amount of pixels to pad the z-dimension. Must be an integer or a half-integer. Defaults to 0. |
0
|
Returns:
Type | Description |
---|---|
ndarray
|
numpy.ndarray: The padded volume. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the input volume is not 3D. |
AssertionError
|
If any padding value is negative. |
Example
(100, 100, 100) (100, 120, 120)Source code in qim3d/operations/_volume_operations.py
qim3d.operations.pad_to
Pads the input 3D volume to a certain shape.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
volume |
ndarray
|
The input 3D volume (shape: n, h, w). |
required |
shape |
tuple[int, int, int]
|
The shape to pad the volume to. |
required |
Returns:
Name | Type | Description |
---|---|---|
padded_volume |
ndarray
|
The padded volume. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the input shape is not 3D. |
AssertionError
|
If the input volume is not 3D. |
AssertionError
|
If the shape tuple is not integers. |
AssertionError
|
If the padded shape is not larger than the original shape. |
Example
(100, 100, 100) (110, 110, 110)Source code in qim3d/operations/_volume_operations.py
qim3d.operations.trim
Removes all empty slices (i.e., slices that contain all zeros) along the x, y, and z axes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
volume |
ndarray
|
The 3D input volume (shape: n, h, w). |
required |
Returns:
Name | Type | Description |
---|---|---|
trimmed_volume |
ndarray
|
The transformed volume with empty slices removed along all axes. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the input shape is not 3D. |
Example
(100, 100, 100) (80, 80, 80)Source code in qim3d/operations/_volume_operations.py
qim3d.operations.shear3d
shear3d(volume, x_shift_y=0, x_shift_z=0, y_shift_x=0, y_shift_z=0, z_shift_x=0, z_shift_y=0, order=1)
Applies a shear transformation to a 3D volume using pixel-based shifts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
volume |
ndarray
|
The input 3D volume (shape: n, h, w). |
required |
x_shift_y |
int
|
Maximum pixel shift in the x-direction, applied progressively along the y-axis. |
0
|
x_shift_z |
int
|
Maximum pixel shift in the x-direction, applied progressively along the z-axis. |
0
|
y_shift_x |
int
|
Maximum pixel shift in the y-direction, applied progressively along the x-axis. |
0
|
y_shift_z |
int
|
Maximum pixel shift in the y-direction, applied progressively along the z-axis. |
0
|
z_shift_x |
int
|
Maximum pixel shift in the z-direction, applied progressively along the x-axis. |
0
|
z_shift_y |
int
|
Maximum pixel shift in the z-direction, applied progressively along the y-axis. |
0
|
order |
int
|
Order of interpolation. Order=0 (nearest-neighbor) keeps voxel values unchanged. Defaults to 1. |
1
|
Returns:
Name | Type | Description |
---|---|---|
sheared_volume |
ndarray
|
The transformed volume. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the input shape is not 3D. |
AssertionError
|
If the order is not integer and in the range of 0-5. |
AssertionError
|
If the shift values are not integer. |
Example
import qim3d
import numpy as np
# Generate box for shearing
vol = np.zeros((60,100,100))
vol[:, 20:80, 20:80] = 1
qim3d.viz.slicer(vol, slice_axis=1)

# Shear the volume by 20% factor in x-direction along z-axis
factor = 0.2
shift = int(vol.shape[0]*factor)
sheared_vol = qim3d.operations.shear3d(vol, x_shift_z=shift, order=1)
qim3d.viz.slicer(sheared_vol, slice_axis=1)

Source code in qim3d/operations/_volume_operations.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 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 |
|
qim3d.operations.curve_warp
curve_warp(volume, x_amp=0, y_amp=0, x_periods=1.0, y_periods=1.0, x_offset=0.0, y_offset=0.0, order=1)
Applies an curve transformation along the z-axis using sine functions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
volume |
ndarray
|
The input 3D volume (shape: n, h, w). |
required |
x_amp |
float
|
Determines the amplitude (height) of the curve in the x-direction. Defaults to 0. |
0
|
y_amp |
float
|
Determines the amplitude (height) of the curve in the y-direction. Defautls to 0. |
0
|
x_periods |
float
|
Determines the amount of periods (amount of wave crests) along the x-direction. Defaults to 1.0. |
1.0
|
y_periods |
float
|
Determines the amount of periods (amount of wave crests) along the y-direction. Defaults to 1.0. |
1.0
|
x_offset |
float
|
Determines pixelwise curve offset in x-direction. Defaults to 0.0. |
0.0
|
y_offset |
float
|
Determines pixelwise curve offset in y-direction. Defaults to 0.0. |
0.0
|
order |
int
|
Order of spline interpolation. Order=0 (nearest-neighbor) will keep voxel values unchanged. Defaults to 1. |
1
|
Returns:
Name | Type | Description |
---|---|---|
warped_volume |
ndarray
|
The transformed volume. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the input shape is not 3D. |
AssertionError
|
If the order is not integer and in the range of 0-5. |
Example
import qim3d
import numpy as np
# Generate box for warping
vol = np.zeros((100,100,100))
vol[:,40:60, 40:60] = 1
qim3d.viz.slicer(vol, slice_axis=1)

# Warp the box along the x dimension
warped_volume = qim3d.operations.curve_warp(vol, x_amp=10, x_periods=4)
qim3d.viz.slicer(warped_volume, slice_axis=1)

Source code in qim3d/operations/_volume_operations.py
qim3d.operations.stretch
Stretches a volume by increasing the size of the volume in the input dimension with interpolation. The volume will therefore increase (or decrease if the stretch is negative) at the same rate as the volume, keeping its relative size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
volume |
ndarray
|
The input 3D volume (shape: n, h, w). |
required |
x_stretch |
int
|
Amount of pixels to stretch the x-dimension. The operation is symmetric, and will be effective on both sides of the volume. Defaults to 0. |
0
|
y_stretch |
int
|
Amount of pixels to stretch the x-dimension. The operation is symmetric, and will be effective on both sides of the volume. Defaults to 0. |
0
|
z_stretch |
int
|
Amount of pixels to stretch the x-dimension. The operation is symmetric, and will be effective on both sides of the volume. Defaults to 0. |
0
|
order |
int
|
Order of spline interpolation. Order=0 (nearest-neighbor) will keep voxel values unchanged. Defaults to 1. |
1
|
Returns:
Name | Type | Description |
---|---|---|
stretched_volume |
ndarray
|
The stretched volume. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the input shape is not 3D. |
AssertionError
|
If the order is not integer and in the range of 0-5. |
AssertionError
|
If the stretching inputs are not integer. |
Example
import qim3d
import numpy as np
# Generate box for stretching
vol = np.zeros((100,100,100))
vol[:,20:80, 20:80] = 1
qim3d.viz.slicer(vol)

# Stretch the box along the x dimension
stretched_volume = qim3d.operations.stretch(vol, x_stretch=20)
print(stretched_volume.shape)
qim3d.viz.slicer(stretched_volume)
# Squeeze the box along the y dimension
squeezed_volume = qim3d.operations.stretch(vol, x_stretch=-20)
print(squeezed_volume.shape)
qim3d.viz.slicer(squeezed_volume)
Source code in qim3d/operations/_volume_operations.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 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 |
|
qim3d.operations.center_twist
Applies a warping transformation that twists the volume around the center along the given axis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
volume |
ndarray
|
The input 3D volume (shape: n, h, w). |
required |
rotation_angle |
float
|
Amount of rotation from bottom of rotation axis to top. Defaults to 90. |
90
|
axis |
str
|
Axis for rotation. Should either take value 'x', 'y' or 'z'. Defaults to 'z'. |
'z'
|
order |
int
|
Order of spline interpolation. Order=0 (nearest-neighbor) will keep voxel values unchanged. Defaults to 1. |
1
|
Returns:
Name | Type | Description |
---|---|---|
twisted_volume |
ndarray
|
The center rotated volume. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the input shape is not 3D. |
AssertionError
|
If the order is not integer and in the range of 0-5. |
AssertionError
|
If the axis are not x, y or z |
Example
Source code in qim3d/operations/_volume_operations.py
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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
|