zarrnii.destripe
Functions for reducing stripe artifacts in microscopy/image volumes.
Destriping module for removing stripe artifacts from volumetric images.
Functions
zarrnii.destripe.phasecong(image, nscale=4, norient=6, min_wave_length=3, mult=2, sigma_on_f=0.55, d_theta_on_sigma=1.2, k=2.0, cut_off=0.4, g=10.0, epsilon=0.0001)
Compute phase congruency for detecting image features.
Python reimplementation of Kovesi's phase congruency algorithm. Phase congruency is a measure of feature significance based on local frequency and phase information, independent of image contrast.
Parameters
image : np.ndarray Square grayscale image (N, N). Float32/64 recommended. nscale : int Number of wavelet scales to use in the analysis. norient : int Number of filter orientations to use (divides 180 degrees). min_wave_length : int Wavelength of smallest scale filter in pixels. mult : int Scaling factor between successive filter wavelengths. sigma_on_f : float Ratio of standard deviation of Gaussian describing log Gabor filter's transfer function in frequency domain to filter center frequency. d_theta_on_sigma : float Ratio of angular interval between filter orientations and standard deviation of angular Gaussian function used to construct filters. k : float Number of standard deviations of noise energy above mean at which we set threshold for phase congruency. cut_off : float Threshold used to determine significance of frequency spread weighting. g : float Controls sharpness of the frequency spread weighting sigmoid function. epsilon : float Small constant to prevent division by zero.
Returns
tuple[np.ndarray, np.ndarray] phaseCongruency : np.ndarray Phase congruency values (N, N) as float32, representing feature strength. orientation_deg : np.ndarray Orientation in degrees (N, N) as float32, range 0-180, quantized by the winning orientation bin.
Raises
ValueError If image is not square 2D.
Source code in zarrnii/destripe.py
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 | |
zarrnii.destripe.destripe_block(block, bg_thresh=0.004, factor=16, diff_thresh=0.007, med_size_min=9, med_size_max=19, phase_size=512, ori_target_deg=90.0, ori_tol_deg=5.0)
De-stripe a single 2D block (typically a z-slice).
The input block is normalized, background is masked, and stripe-like artifacts are detected via phase congruency in a downsampled grid and then corrected. The result is rescaled back to the original intensity range and has the same shape as the input.
Parameters
block:
Input image block as a NumPy array. It is squeezed to 2D
(Y, X) before processing. Usually this is the array passed by
dask.map_blocks.
bg_thresh:
Background intensity threshold in the normalized [0, 1] domain.
Pixels with intensity lower than this value are treated as
background when building the background mask. Increasing this
value makes the background mask more aggressive (more pixels
are considered background); decreasing it is more conservative.
factor:
Integer down/upsampling factor for the internal processing grid.
The image is divided into a coarse grid scaled by this factor
for estimating and correcting stripe patterns. Larger values
reduce computational cost and capture broader stripe structure
but may miss very fine-scale artifacts; smaller values provide
finer sampling at higher computational cost.
diff_thresh:
Threshold applied to an internal difference map D used to
separate two regimes (e.g., D0/D1) when estimating stripe
contributions. Higher values make the split more selective
(fewer pixels classified as high-difference), while lower values
make it more inclusive.
med_size_min:
Minimum size (in pixels) of the median filter kernel applied per
tile during destriping. The actual kernel size used for a given
tile is deterministically selected between med_size_min and
med_size_max (for example, based on the channel index), and
odd sizes are enforced internally. Smaller values preserve more
fine detail but may leave residual stripe noise.
med_size_max:
Maximum size (in pixels) of the median filter kernel applied per
tile during destriping. Within the deterministically chosen range
between med_size_min and med_size_max, larger kernel
sizes yield stronger smoothing and more aggressive stripe removal,
at the risk of blurring small structures.
phase_size:
Size (in pixels) of the square region used for phase congruency
analysis. This controls the spatial extent over which oriented
features (such as stripes) are detected. Must be large enough to
capture several stripe periods; increasing it may improve
robustness for broad patterns but increases computation.
ori_target_deg:
Target stripe orientation in degrees for phase congruency
detection (e.g., 90° for vertical stripes in image coordinates).
Only features near this orientation are treated as stripe
artifacts.
ori_tol_deg:
Angular tolerance (in degrees) around ori_target_deg within
which features are considered stripe-like. A larger tolerance
captures a wider range of orientations (more aggressive
destriping), while a smaller tolerance focuses on a narrower
band of orientations.
Returns
np.ndarray
De-striped image block with the same shape as block,
rescaled to the original intensity range.
Notes
This function operates on in-memory NumPy arrays and is designed to
be mapped over a larger volume using dask.map_blocks.
Source code in zarrnii/destripe.py
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 323 324 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 | |
zarrnii.destripe.downsample_grid(img, factor)
Downsample a 2D image into an interleaved grid stack.
Divides the input image into a regular grid of non-overlapping tiles
based on the downsampling factor. Each tile is extracted by taking every
factor-th pixel in both dimensions starting from different offsets.
The function crops the image to ensure dimensions are multiples of
factor.
Parameters
img : np.ndarray
2D grayscale image with shape (H, W).
factor : int
Downsampling grid factor. The image is divided into factor**2
interleaved sub-grids.
Returns
np.ndarray
3D stack with shape (h_small, w_small, factor**2) where
h_small = H // factor and w_small = W // factor.
Each channel along the third axis corresponds to one interleaved
sub-grid defined by offsets (i, j) for i, j in range(factor).
Source code in zarrnii/destripe.py
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 | |
zarrnii.destripe.upsample_grid(I_stack, factor)
Reconstruct a high-resolution 2D image from a downsampled grid stack.
This function reverses :func:downsample_grid by "unshuffling" the
interleaved low-resolution tiles stored in I_stack back into their
original pixel positions in a single 2D image. The input I_stack is
expected to have been produced by downsample_grid(img, factor) on a
2D grayscale image img.
Parameters
I_stack : np.ndarray
3D stack of downsampled images with shape
(h_small, w_small, factor**2), where
h_small = floor(h / factor) and w_small = floor(w / factor).
The third axis indexes the factor**2 interleaved sub-grids,
corresponding to all combinations of row/column offsets
(i, j) in range(factor) used during downsampling.
factor : int
Downsampling / upsampling grid factor. Must be the same positive
integer that was used in :func:downsample_grid. The reconstructed
image will have spatial dimensions h = h_small * factor and
w = w_small * factor.
Returns
np.ndarray
Reconstructed 2D image of shape (h_small * factor, w_small * factor).
For each channel index idx corresponding to offsets
(i, j) in range(factor), the slice I_stack[:, :, idx] is
written into img_recon[i::factor, j::factor], reassembling the
original interleaved pixel grid.
Source code in zarrnii/destripe.py
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 | |
zarrnii.destripe.destripe(img, bg_thresh=0.004, factor=16, diff_thresh=0.007, med_size_min=9, med_size_max=19, phase_size=512, ori_target_deg=90.0, ori_tol_deg=5.0)
Reduce stripe artifacts in a volumetric image using a block-wise destriping algorithm.
This function applies :func:destripe_block independently to each Z-slice
(or to each Z-slice per leading index such as time or channel) of a Dask
array. It is designed for large 3D imaging data (e.g. light-sheet
microscopy volumes) stored as a stack of 2D planes, where striping arises
from illumination or acquisition artifacts with a dominant orientation.
The input must be a Dask array with 3 to 5 dimensions. The last three
dimensions are interpreted as (Z, Y, X). Any leading dimensions (e.g.
time T and/or channels C) are preserved and processed independently.
Chunking requirements
The destriping algorithm assumes that each block corresponds to a single
Z-slice with the full in-plane field of view. The following chunking
constraints are enforced (see :func:_has_allowed_chunking):
- The array must have between 3 and 5 dimensions.
- The last three axes must be
(Z, Y, X). - Z chunks must have size 1 along the Z axis (i.e. one slice per chunk).
- Y and X must each be a single chunk covering the full image extent
(
chunk[-2] == shape[-2]andchunk[-1] == shape[-1]). - Any leading axes (e.g. T, C) must also be chunked with size 1 along each of those axes.
If these conditions are not met, a :class:ValueError is raised.
Parameters
img:
Dask array containing the input image data. The last three dimensions
must be (Z, Y, X) with chunking as described above. The data type
is preserved in the output.
bg_thresh:
Threshold used to define a background mask. Typical values are small
positive fractions of the image dynamic range; pixels below this value
are considered background and are down-weighted in the destriping
process.
factor:
Down/upsampling grid factor used by the internal tiling scheme. Larger
values correspond to finer tiling during destriping and may increase
computation time.
diff_thresh:
Threshold applied to an internal difference image (D) to separate
low- and high-intensity components before stripe estimation.
med_size_min:
Minimum size (in pixels) of the median filter kernel used per tile.
The effective kernel size per tile is chosen within
[med_size_min, med_size_max].
med_size_max:
Maximum size (in pixels) of the median filter kernel used per tile.
phase_size:
Size (in pixels) of the square region used for phase congruency
analysis. Must be large enough to capture several stripe periods.
ori_target_deg:
Target stripe orientation in degrees. The default of 90.0 assumes
vertical stripes in the image coordinate system.
ori_tol_deg:
Angular tolerance (in degrees) around ori_target_deg within which
structures are considered stripe-like.
Returns
dask.array.Array
A Dask array of the same shape and data type as img, with stripe
artifacts reduced. The chunking pattern is preserved.
Raises
ValueError
If img does not have between 3 and 5 dimensions or if its chunking
does not satisfy the constraints described above.
Examples
Create a synthetic volume and apply destriping::
import dask.array as da
import numpy as np
# 3D volume with shape (Z, Y, X)
vol = np.random.rand(16, 512, 512).astype("float32")
# Chunk as one Z-slice per chunk, full XY
darr = da.from_array(vol, chunks=(1, 512, 512))
# Apply destriping lazily
darr_destriped = destripe(darr)
# Trigger computation
result = darr_destriped.compute()
Source code in zarrnii/destripe.py
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 | |