Plot and render
Visualization of volumetric data.
qim3d.viz.export_rotation
export_rotation(
path,
volume,
degrees=360,
n_frames=180,
fps=30,
image_size=(256, 256),
colormap='magma',
camera_height=2.0,
camera_distance='auto',
camera_focus='center',
show=False,
)
Export a rotation animation of volume.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path to save the output. The path should end with .gif, .avi, .mp4 or .webm. If no file extension is specified, .gif is automatically added. |
required |
volume
|
ndarray
|
Volume to create .gif of. |
required |
degrees
|
int
|
The amount of degrees for the volume to rotate. Defaults to 360. |
360
|
n_frames
|
int
|
The amount of frames to generate. Defaults to 180. |
180
|
fps
|
int
|
The amount of frames per second in the resulting animation. This determines the speed of the rotation of the volume. Defaults to 30. |
30
|
image_size
|
tuple of ints or None
|
Pixel size (width, height) of each frame. If None, the plotter's default size is used. Defaults to (256, 256). |
(256, 256)
|
colormap
|
str
|
Determines color map of volume. Defaults to 'magma'. |
'magma'
|
camera_height
|
float
|
Determines the height of the camera rotating around the volume. The float value represents a multiple of the height of the z-axis. Defaults to 2.0. |
2.0
|
camera_distance
|
int or string
|
Determines the distance of the camera from the center point. If 'auto' is used, it will be auto calculated. Otherwise a float value representing voxel distance is expected. Defaults to 'auto'. |
'auto'
|
camera_focus
|
list or str
|
Determines the voxel that the camera rotates around. Using 'center' will default to the center of the volume. Otherwise a list of three integers is expected. Defaults to 'center'. |
'center'
|
show
|
bool
|
If True, the resulting animation will be shown in the Jupyter notebook. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the camera focus argument is incorrectly used. |
TypeError
|
If the camera_distance argument is incorrectly used. |
ValueError
|
If the path contains an unrecognized file extension. |
Example
Creation of .gif file with default parameters of a generated volume.
Example
Creation of a .webm file with specified parameters of a generated volume in the shape of a tube.
import qim3d
vol = qim3d.generate.volume(shape='tube')
qim3d.viz.export_rotation('test.webm', vol,
degrees = 360,
n_frames = 120,
fps = 30,
image_size = (512,512),
camera_height = 3.0,
camera_distance = 'auto',
camera_focus = 'center',
show = True)
Source code in qim3d/viz/_data_exploration.py
2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 | |
qim3d.viz.circles
Plots the blobs found on a slice of the volume.
This function takes in a 3D volume and a list of blobs (detected features) and plots the blobs on a specified slice of the volume. If no slice is specified, it defaults to the middle slice of the volume.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blobs
|
ndarray
|
An array-like object of blobs, where each blob is represented
as a 4-tuple (p, r, c, radius). Usually the result of |
required |
vol
|
ndarray
|
The 3D volume on which to plot the blobs. |
required |
alpha
|
float
|
The transparency of the blobs. Defaults to 0.5. |
0.5
|
color
|
str
|
The color of the blobs. Defaults to "#ff9900". |
'#ff9900'
|
**kwargs
|
Any
|
Arbitrary keyword arguments for the |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
slicer_obj |
interactive
|
An interactive widget for visualizing the blobs. |
Example
import qim3d
import qim3d.detection
# Get data
vol = qim3d.examples.cement_128x128x128
# Detect blobs, and get binary mask
blobs, _ = qim3d.detection.blobs(
vol,
min_sigma=1,
max_sigma=8,
threshold=0.001,
overlap=0.1,
background="bright"
)
# Visualize detected blobs with circles method
qim3d.viz.circles(blobs, vol, alpha=0.8, color='blue')
Source code in qim3d/viz/_detection.py
qim3d.viz.local_thickness
local_thickness(
image,
image_lt,
max_projection=False,
axis=0,
slice_index=None,
show=False,
figsize=(15, 5),
)
Visualizes the local thickness of a 2D or 3D image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
ndarray
|
2D or 3D NumPy array representing the image/volume. |
required |
image_lt
|
ndarray
|
2D or 3D NumPy array representing the local thickness of the input image/volume. |
required |
max_projection
|
bool
|
If True, displays the maximum projection of the local thickness. Only used for 3D images. Defaults to False. |
False
|
axis
|
int
|
The axis along which to visualize the local thickness. Unused for 2D images. Defaults to 0. |
0
|
slice_index
|
int or float
|
The initial slice to be visualized. The slice index can afterwards be changed. If value is an integer, it will be the index of the slice to be visualized. If value is a float between 0 and 1, it will be multiplied by the number of slices and rounded to the nearest integer. If None, the middle slice will be used for 3D images. Unused for 2D images. Defaults to None. |
None
|
show
|
bool
|
If True, displays the plot (i.e. calls plt.show()). Defaults to False. |
False
|
figsize
|
tuple
|
The size of the figure. Defaults to (15, 5). |
(15, 5)
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the slice index is not an integer or a float between 0 and 1. |
Returns:
| Name | Type | Description |
|---|---|---|
local_thickness |
interactive or Figure
|
If the input is 3D, returns an interactive widget. Otherwise, returns a matplotlib figure. |
Example
import qim3d
fly = qim3d.examples.fly_150x256x256
lt_fly = qim3d.processing.local_thickness(fly)
qim3d.viz.local_thickness(fly, lt_fly, axis=0)
Source code in qim3d/viz/_local_thickness.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 | |
qim3d.viz.vectors
vectors(
volume,
vectors,
axis=0,
volume_colormap='grey',
min_value=None,
max_value=None,
slice_index=None,
grid_size=10,
interactive=True,
figsize=(10, 5),
show=False,
)
Visualizes the orientation of the structures in a 3D volume using the eigenvectors of the structure tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
ndarray
|
The 3D volume to be sliced. |
required |
vectors
|
ndarray
|
The eigenvectors of the structure tensor. |
required |
axis
|
int
|
The axis along which to visualize the orientation. Defaults to 0. |
0
|
volume_colormap
|
str
|
Defines colormap for display of the volume |
'grey'
|
min_value
|
float
|
Together with max_value define the data range the colormap covers. By default colormap covers the full range. Defaults to None. |
None
|
max_value
|
float
|
Together with min_value define the data range the colormap covers. By default colormap covers the full range. Defaults to None |
None
|
slice_index
|
int or float or None
|
The initial slice to be visualized. The slice index can afterwards be changed. If value is an integer, it will be the index of the slice to be visualized. If value is a float between 0 and 1, it will be multiplied by the number of slices and rounded to the nearest integer. If None, the middle slice will be used. Defaults to None. |
None
|
grid_size
|
int
|
The size of the grid. Defaults to 10. |
10
|
interactive
|
bool
|
If True, returns an interactive widget. Defaults to True. |
True
|
figsize
|
tuple
|
The size of the figure. Defaults to (15, 5). |
(10, 5)
|
show
|
bool
|
If True, displays the plot (i.e. calls plt.show()). Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the axis to slice along is not 0, 1, or 2. |
ValueError
|
If the slice index is not an integer or a float between 0 and 1. |
Returns:
| Name | Type | Description |
|---|---|---|
fig |
interactive or Figure
|
If |
Note
The orientation of the vectors is visualized using an HSV color map, where the saturation corresponds to the vector component
of the slicing direction (i.e. z-component when choosing visualization along axis = 0). Hence, if an orientation in the volume
is orthogonal to the slicing direction, the corresponding color of the visualization will be gray.
Example
import qim3d
vol = qim3d.examples.NT_128x128x128
val, vec = qim3d.processing.structure_tensor(vol)
# Visualize the structure tensor
qim3d.viz.vectors(vol, vec, axis = 2, interactive = True)
Source code in qim3d/viz/_structure_tensor.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 153 154 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 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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
qim3d.viz.vector_field_3d
vector_field_3d(
vec,
val,
select_eigen='smallest',
sampling_step=4,
max_cones=50000,
cone_size=1,
verbose=True,
colormap='Portland',
cmin=None,
cmax=None,
**kwargs,
)
Visualize 3D structure tensor eigenvectors as cones in Plotly.
Each cone represents an eigenvector whose direction and size indicate the dominant local orientation and the magnitude of the corresponding eigenvalue.
If sampling_step is greater than 1, each cone represents the average orientation and magnitude within that sampled region.
The function is designed to work directly with the outputs of qim3d.processing.structure_tensor() which is in ascendic order by default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
ndarray
|
Eigenvalues from the structure tensor, with shape |
required |
vec
|
ndarray
|
Eigenvectors from the structure tensor.
Shape depends on the
|
required |
select_eigen
|
Literal['smallest', 'largest', 'middle']
|
If vec has shape |
'smallest'
|
sampling_step
|
int
|
Grid spacing for sampling points.
Default is |
4
|
max_cones
|
int
|
Maximum number of cones to display. If more points are sampled,
only the locations with the highest eigenvalues are kept.
Default is |
50000
|
cone_size
|
float
|
Scaling factor for cone length, proportional to vector magnitude.
Default is |
1
|
verbose
|
bool
|
Whether to print progress and info messages.
Default is |
True
|
colormap
|
str
|
Name of the Plotly colorscale used for cones.
Default is |
'Portland'
|
cmin
|
float
|
Minimum value for color scale. If |
None
|
cmax
|
float
|
Maximum value for color scale. If |
None
|
**kwargs
|
Additional keyword arguments passed to |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If an invalid combination of |
Returns:
| Name | Type | Description |
|---|---|---|
fig |
Figure
|
Interactive 3D Plotly figure showing cone representations of local orientation vectors. |
Example
vol = qim3d.examples.fibers_150x150x150
val, vec = qim3d.processing.structure_tensor(vol, smallest = False)
qim3d.viz.vector_field_3d(vec, val, sampling_step=12, max_cones=5000, cone_size = 2, select_eigen="smallest")
Notes
Understanding the Structure Tensor
Each voxel is associated with three eigenvalues (λ₁, λ₂, λ₃) and corresponding eigenvectors, which describe how much intensity varies along each direction.
The relative magnitudes of the eigenvalues determine the type of local structure:
| Structure type | Eigenvalue pattern | Dominant orientation vector |
|---|---|---|
| Planar structure (surface) | Two large, one small (λ₁, λ₂ ≫ λ₃) | Eigenvector with smallest eigenvalue → surface normal |
| Linear structure (fiber) | One large, two small (λ₁ ≫ λ₂, λ₃) | Eigenvector with largest eigenvalue → line direction |
| Isotropic region (flat) | All similar (λ₁ ≈ λ₂ ≈ λ₃) | No dominant direction |
So based on what you are interested in visualizing, you may want to select different eigenvectors using the select_eigen parameter.
Source code in qim3d/viz/_structure_tensor.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 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 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 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 | |