Skip to content

Commit 006f2f1

Browse files
committed
add normalize_img function
1 parent 9831a7a commit 006f2f1

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

pyraws/utils/img_processing_utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,31 @@ def is_2D_image(img: np.ndarray) -> bool:
3434
bool: True if an image has just two dimensions. False otherwise.
3535
"""
3636
return img.shape == 2
37+
38+
39+
def normalize_img(
40+
img: np.ndarray,
41+
min: float,
42+
max: float,
43+
a: float = 0,
44+
b: float = 1,
45+
tol: float = 1e-6,
46+
) -> np.ndarray:
47+
"""Normalizes an image from range [min, max] to range [a, b].
48+
49+
Args:
50+
img (np.ndarray): image to normalize.
51+
min (float): minimum of the previous range.
52+
max (float): maximum of the previous range.
53+
a (float, optional): minimum of the new range. Defaults to 0.
54+
b (float, optional): maximum of the new range. Defaults to 1.
55+
tol (float, optional): tolerance. Defaults to 1e-6.
56+
57+
Returns:
58+
np.ndarray: normalized image.
59+
"""
60+
assert max != min
61+
img_norm = ((b - a) * ((img - min) / (max - min))) + a
62+
assert np.count_nonzero(img_norm < a - tol) <= 0
63+
assert np.count_nonzero(img_norm > b + tol) <= 0
64+
return img_norm

pyraws/utils/keypoint_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from pyraws.utils.img_processing_utils import (
22
is_2D_image,
33
three_channel_to_grayscale_img,
4+
normalize_img,
45
)
56
import numpy as np
67
import cv2
@@ -37,8 +38,8 @@ def get_matched_keypoints_sift(
3738

3839
sift = cv2.SIFT_create()
3940

40-
img_1 = (img_1 - np.min(img_1)) * max_val_grayscale / np.max(img_1)
41-
img_2 = (img_2 - np.min(img_2)) * max_val_grayscale / np.max(img_2)
41+
img_1 = normalize_img(img_1, np.min(img_1), np.max(img_1), 0, max_val_grayscale)
42+
img_2 = normalize_img(img_2, np.min(img_2), np.max(img_2), 0, max_val_grayscale)
4243
img_1 = img_1.astype(np.uint8)
4344
img_2 = img_2.astype(np.uint8)
4445
# Find keypoints and descriptors.

0 commit comments

Comments
 (0)