Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Hue & Saturation node #2774

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@

def with_lightness(img: np.ndarray, lightness: float) -> np.ndarray:
if lightness > 0:
return img + (1 - img) * lightness
assert lightness <= 1
res = img * (1 - lightness)
res += lightness
return res
elif lightness < 0:
assert lightness >= -1
return img * (1 + lightness)
else:
return img
Expand Down Expand Up @@ -63,7 +67,9 @@ def with_lightness(img: np.ndarray, lightness: float) -> np.ndarray:
gradient=["#000000", "#ffffff"],
),
],
outputs=[ImageOutput(image_type="Input0")],
outputs=[
ImageOutput(image_type="Input0", assume_normalized=True),
],
)
def hue_and_saturation_node(
img: np.ndarray,
Expand All @@ -88,24 +94,32 @@ def hue_and_saturation_node(
alpha = None
if c > 3:
alpha = img[:, :, 3]
img = img[:, :, :3]

h, l, s = cv2.split(cv2.cvtColor(img[:, :, :3], cv2.COLOR_BGR2HLS))
if hue != 0 or saturation != 0:
# Convert to HLS color space
h, l, s = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HLS))

# Adjust hue
if hue != 0:
h += hue # type: ignore
h[h >= 360] -= 360 # Wrap positive overflow
h[h < 0] += 360 # Wrap negative overflow
# Adjust hue
if hue != 0:
h += hue # type: ignore
h[h >= 360] -= 360 # Wrap positive overflow
h[h < 0] += 360 # Wrap negative overflow

# Adjust saturation
if saturation != 0:
saturation = 1 + saturation
s = np.clip(s * saturation, 0, 1) # type: ignore
# Adjust saturation
if saturation != 0:
factor = 1 + saturation
s *= factor # type: ignore
if factor > 1:
s = np.clip(s, 0, 1, out=s)

img = cv2.cvtColor(cv2.merge([h, l, s]), cv2.COLOR_HLS2BGR)
# we assume that this returns normalized values in Change Color Model,
# so it should be fine here as well
img = cv2.cvtColor(cv2.merge([h, l, s]), cv2.COLOR_HLS2BGR)

# Adjust lightness
img = with_lightness(img, lightness)
if lightness != 0:
img = with_lightness(img, lightness)

# Re-add alpha, if it exists
if alpha is not None:
Expand Down
Loading