Skip to content

Commit

Permalink
Added function to flatten a single image
Browse files Browse the repository at this point in the history
  • Loading branch information
tieneupin committed Sep 4, 2024
1 parent 1becae5 commit 23ea918
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/cryoemservices/clem/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
from enum import Enum
from pathlib import Path
from typing import Optional, Union
from typing import Literal, Optional, Union

import numpy as np
from tifffile import imwrite
Expand Down Expand Up @@ -339,3 +339,27 @@ def convert_to_rgb(
arr_new = np.stack(arr_list, axis=-1)

return arr_new


def flatten_image(
array: np.ndarray,
mode: Literal["min", "max", "mean"] = "mean",
) -> np.ndarray:

# Validate options
valid_modes = (
"min",
"max",
"mean",
)
if mode not in valid_modes:
raise ValueError(f"{mode} is not a valid image flattening mode")

# Flatten along first axis
axis = 0
if mode == "min":
return array.min(axis=axis)
if mode == "max":
return array.max(axis=axis)
if mode == "mean":
return array.mean(axis=axis)

0 comments on commit 23ea918

Please sign in to comment.