Skip to content

Commit

Permalink
Added anti-aliasing to Threshold node (#2209)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Sep 13, 2023
1 parent 79de6d3 commit 37d1f27
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion backend/src/packages/chaiNNer_standard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
Dependency(
display_name="ChaiNNer Extensions",
pypi_name="chainner_ext",
version="0.3.0",
version="0.3.1",
size_estimate=2.0 * MB,
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

import cv2
import numpy as np
from chainner_ext import binary_threshold

from nodes.groups import if_enum_group
from nodes.properties.inputs import EnumInput, ImageInput, SliderInput
from nodes.properties.inputs import BoolInput, EnumInput, ImageInput, SliderInput
from nodes.properties.outputs import ImageOutput

from .. import adjustments_group
Expand Down Expand Up @@ -59,6 +60,10 @@ class ThresholdType(Enum):
controls_step=1,
).with_id(2),
),
BoolInput("Anti-aliasing", default=False).with_docs(
"Enables sub-pixel precision. Bilinear interpolation is used to fill in values in between pixels.",
"Conceptually, the option is equivalent to first upscaling the image by a factor of X (with linear interpolation), thresholding it, and then downscaling it by a factor of X (where X is 20 or more).",
),
],
outputs=[ImageOutput(image_type="Input0")],
see_also=[
Expand All @@ -71,10 +76,26 @@ def threshold_node(
threshold: float,
thresh_type: ThresholdType,
max_value: float,
anti_aliasing: bool,
) -> np.ndarray:
threshold /= 100
max_value /= 100

_, result = cv2.threshold(img, threshold, max_value, thresh_type.value)
if not anti_aliasing:
_, result = cv2.threshold(img, threshold, max_value, thresh_type.value)
return result

binary = binary_threshold(img, threshold, True)
if thresh_type == ThresholdType.BINARY_INV:
binary = 1 - binary

return result
if thresh_type == ThresholdType.BINARY or thresh_type == ThresholdType.BINARY_INV:
if max_value < 1:
binary *= max_value
return binary
elif thresh_type == ThresholdType.TRUNC:
return binary * threshold + img * (1 - binary)
elif thresh_type == ThresholdType.TO_ZERO:
return binary * img
elif thresh_type == ThresholdType.TO_ZERO_INV:
return (1 - binary) * img

0 comments on commit 37d1f27

Please sign in to comment.