Skip to content

Image Pipeline Playground

feat(backend/nodes): add YUV and YCrCb color space nodes

Mohammad Rajaeiff4073c862ab
diff --git a/backend/app/nodes/__init__.py b/backend/app/nodes/__init__.py
index 1710c0d..68ed176 100644
--- a/backend/app/nodes/__init__.py
+++ b/backend/app/nodes/__init__.py
@@ -16,6 +16,8 @@ from app.nodes.color import (
ToGrayNode,
ToHsvNode,
ToLabNode,
+ ToYCrCbNode,
+ ToYuvNode,
)
from app.nodes.filters import (
BilateralFilterNode,
@@ -54,6 +56,8 @@ def register_builtin_nodes() -> None:
ToGrayNode(),
ToHsvNode(),
ToLabNode(),
+ ToYuvNode(),
+ ToYCrCbNode(),
InvertNode(),
ClaheNode(),
BrightnessContrastNode(),
diff --git a/backend/app/nodes/color.py b/backend/app/nodes/color.py
index 471ed1c..cba7081 100644
--- a/backend/app/nodes/color.py
+++ b/backend/app/nodes/color.py
@@ -193,6 +193,66 @@ class ToLabNode(BaseNode):
return [f"{output_vars['image']} = cv2.cvtColor({input_vars['image']}, cv2.COLOR_BGR2LAB)"]
+class ToYuvNode(BaseNode):
+ type = "to_yuv"
+ label = "To YUV"
+ category = "color"
+ description = "Convert BGR to YUV color space."
+ ports = [image_in(), image_out()]
+ params = []
+
+ def execute(
+ self,
+ inputs: dict[str, np.ndarray | list[np.ndarray] | None],
+ params: dict[str, Any],
+ seed: int = 0,
+ ) -> dict[str, np.ndarray | list[np.ndarray]]:
+ image = require_image(inputs)
+ if len(image.shape) == 2:
+ image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
+ return {"image": cv2.cvtColor(image, cv2.COLOR_BGR2YUV)}
+
+ def emit_python(
+ self,
+ node_id: str,
+ params: dict[str, Any],
+ input_vars: dict[str, str],
+ output_vars: dict[str, str],
+ ) -> list[str]:
+ return [f"{output_vars['image']} = cv2.cvtColor({input_vars['image']}, cv2.COLOR_BGR2YUV)"]
+
+
+class ToYCrCbNode(BaseNode):
+ type = "to_ycrcb"
+ label = "To YCrCb"
+ category = "color"
+ description = "Convert BGR to YCrCb color space."
+ ports = [image_in(), image_out()]
+ params = []
+
+ def execute(
+ self,
+ inputs: dict[str, np.ndarray | list[np.ndarray] | None],
+ params: dict[str, Any],
+ seed: int = 0,
+ ) -> dict[str, np.ndarray | list[np.ndarray]]:
+ image = require_image(inputs)
+ if len(image.shape) == 2:
+ image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
+ return {"image": cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)}
+
+ def emit_python(
+ self,
+ node_id: str,
+ params: dict[str, Any],
+ input_vars: dict[str, str],
+ output_vars: dict[str, str],
+ ) -> list[str]:
+ return [
+ f"{output_vars['image']} = cv2.cvtColor({input_vars['image']}, cv2.COLOR_BGR2YCrCb)"
+ ]
+
+
class InvertNode(BaseNode):
type = "invert"
label = "Invert"
diff --git a/backend/tests/test_color_spaces.py b/backend/tests/test_color_spaces.py
new file mode 100644
index 0000000..8f28c80
--- /dev/null
+++ b/backend/tests/test_color_spaces.py
@@ -0,0 +1,31 @@
+"""Tests for additional color-space conversion nodes."""
+
+from __future__ import annotations
+
+import numpy as np
+
+from app.engine.registry import registry
+from app.nodes import register_builtin_nodes
+
+
+def test_yuv_ycrcb_registered() -> None:
+ register_builtin_nodes()
+ types = {meta.type for meta in registry.list_metadata()}
+ assert "to_yuv" in types
+ assert "to_ycrcb" in types
+
+
+def test_to_yuv_shape() -> None:
+ register_builtin_nodes()
+ image = np.zeros((16, 16, 3), dtype=np.uint8)
+ image[:, :] = (40, 80, 160)
+ result = registry.get("to_yuv").execute({"image": image}, {}, seed=0)["image"]
+ assert result.shape == image.shape
+
+
+def test_to_ycrcb_shape() -> None:
+ register_builtin_nodes()
+ image = np.zeros((16, 16, 3), dtype=np.uint8)
+ image[:, :] = (40, 80, 160)
+ result = registry.get("to_ycrcb").execute({"image": image}, {}, seed=0)["image"]
+ assert result.shape == image.shape
diff --git a/backend/tests/test_nodes.py b/backend/tests/test_nodes.py
index 31751a2..82744ed 100644
--- a/backend/tests/test_nodes.py
+++ b/backend/tests/test_nodes.py
@@ -17,6 +17,8 @@ def test_builtin_nodes_registered() -> None:
"to_gray",
"to_hsv",
"to_lab",
+ "to_yuv",
+ "to_ycrcb",
"invert",
"clahe",
"brightness_contrast",