Skip to content

Commit 17aa4ca

Browse files
authored
feat: image metrics
1 parent e1f2fe7 commit 17aa4ca

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

src/aind_exaspim_image_compression/utils/img_util.py

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def plot_mips(img, output_path=None, vmax=None):
272272
Parameters
273273
----------
274274
img : numpy.ndarray
275-
Input 3D image to generate MIPs from.
275+
Input image to generate MIPs from.
276276
277277
Returns
278278
-------
@@ -283,7 +283,11 @@ def plot_mips(img, output_path=None, vmax=None):
283283
fig, axs = plt.subplots(1, 3, figsize=(10, 4))
284284
axs_names = ["XY", "XZ", "YZ"]
285285
for i in range(3):
286-
mip = np.max(img, axis=i)
286+
if len(img.shape) == 5:
287+
mip = np.max(img[0, 0, ...], axis=i)
288+
else:
289+
mip = np.max(img, axis=i)
290+
287291
axs[i].imshow(mip, vmax=vmax)
288292
axs[i].set_title(axs_names[i], fontsize=16)
289293
axs[i].set_xticks([])
@@ -456,7 +460,7 @@ def compute_cratio(img, codec, chunk_shape=(64, 64, 64)):
456460
Compression ratio = total uncompressed size / total compressed size
457461
458462
"""
459-
img = np.ascontiguousarray(img)
463+
img = np.ascontiguousarray(img, dtype=np.uint16)
460464
total_compressed_size = 0
461465
total_uncompressed_size = 0
462466

@@ -476,9 +480,49 @@ def compute_cratio(img, codec, chunk_shape=(64, 64, 64)):
476480
return round(total_uncompressed_size / total_compressed_size, 2)
477481

478482

483+
def compute_mae(img1, img2):
484+
"""
485+
Computes the mean absolute difference between two 3D images.
486+
487+
Parameters
488+
----------
489+
img1 : numpy.ndarray
490+
3D Image.
491+
img2 : numpy.ndarray
492+
3D Image.
493+
494+
Returns
495+
-------
496+
float
497+
Mean absolute difference between two 3D images.
498+
"""
499+
return np.mean(abs(img1 - img2))
500+
501+
502+
def compute_stable_lmax(img1, img2, p=99.99):
503+
"""
504+
Computes the stable l-inf norm between two 3D images.
505+
506+
Parameters
507+
----------
508+
img1 : numpy.ndarray
509+
3D Image.
510+
img2 : numpy.ndarray
511+
3D Image.
512+
p : float, optional
513+
Percentile used to compute stable l-inf norm. Default is 99.99.
514+
515+
Returns
516+
-------
517+
float
518+
Stable l-inf norm between two 3D images.
519+
"""
520+
return np.percentile(abs(img1 - img2), p)
521+
522+
479523
def compute_ssim3D(img1, img2, data_range=None, window_size=16):
480524
"""
481-
Compute structural similarity (SSIM) between two 3D images.
525+
Computes the structural similarity (SSIM) between two 3D images.
482526
483527
Parameters
484528
----------
@@ -496,7 +540,6 @@ def compute_ssim3D(img1, img2, data_range=None, window_size=16):
496540
-------
497541
float
498542
SSIM between the two input images.
499-
500543
"""
501544
if img1.shape != img2.shape:
502545
raise ValueError("Input images must have the same dimensions")
@@ -539,7 +582,6 @@ def get_nbs(voxel, shape):
539582
-------
540583
List[Tuple[int]]
541584
Voxel coordinates of the neighboring voxels.
542-
543585
"""
544586
x, y, z = voxel
545587
nbs = []
@@ -573,7 +615,6 @@ def is_inbounds(voxel, shape):
573615
bool
574616
Indication of whether the given voxel is within the bounds of the
575617
grid.
576-
577618
"""
578619
x, y, z = voxel
579620
height, width, depth = shape

0 commit comments

Comments
 (0)