Skip to content

Commit

Permalink
Improve ODF background image normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
msorelli committed Oct 16, 2024
1 parent 5374eb8 commit 24244fe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
8 changes: 5 additions & 3 deletions foa3d/odf.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,16 @@ def generate_odf_background(bg_img, bg_mrtrix_mmap, vxl_side):
# get shape of new downsampled array
new_shape = bg_mrtrix_mmap.shape[:-1]

# normalize
# image normalization: get global minimum and maximum values
if bg_img.ndim == 3:
bg_img = normalize_image(bg_img)
min_glob = np.min(bg_img)
max_glob = np.max(bg_img)

# loop over z-slices, and resize them
for z in range(0, bg_img.shape[0], vxl_side):
if bg_img.ndim == 3:
tmp_slice = np.mean(bg_img[z:z + vxl_side, ...], axis=0)
tmp_slice = normalize_image(bg_img[z:z + vxl_side, ...], min_val=min_glob, max_val=max_glob)
tmp_slice = np.mean(tmp_slice, axis=0)
elif bg_img.ndim == 4:
tmp_slice = 255.0 * np.sum(np.abs(bg_img[z, ...]), axis=-1)
tmp_slice = np.where(tmp_slice <= 255.0, tmp_slice, 255.0)
Expand Down
14 changes: 11 additions & 3 deletions foa3d/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,14 +402,20 @@ def normalize_angle(angle, lower=0.0, upper=360.0, dtype=None):
return norm_angle


def normalize_image(img, max_out_val=255.0, dtype=np.uint8):
def normalize_image(img, min_val=None, max_val=None, max_out_val=255.0, dtype=np.uint8):
"""
Normalize image data.
Parameters
----------
img: numpy.ndarray
input image
min_val: float
minimum input value
max_val: float
maximum input value
max_out_val: float
maximum output value
Expand All @@ -424,8 +430,10 @@ def normalize_image(img, max_out_val=255.0, dtype=np.uint8):
"""

# get min and max values
min_val = np.min(img)
max_val = np.max(img)
if min_val is None:
min_val = np.min(img)
if max_val is None:
max_val = np.max(img)

# normalize
if max_val != 0:
Expand Down

0 comments on commit 24244fe

Please sign in to comment.