-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b586bc
commit b51f024
Showing
4 changed files
with
104 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 61 additions & 25 deletions
86
backend/src/packages/chaiNNer_standard/image_adjustment/adjustments/threshold_adaptive.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,101 @@ | ||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
from typing import Dict | ||
|
||
import cv2 | ||
import numpy as np | ||
|
||
from nodes.impl.image_utils import to_uint8 | ||
from nodes.properties.inputs import ( | ||
AdaptiveMethodInput, | ||
AdaptiveThresholdInput, | ||
ImageInput, | ||
NumberInput, | ||
SliderInput, | ||
) | ||
from nodes.properties.inputs import EnumInput, ImageInput, NumberInput, SliderInput | ||
from nodes.properties.outputs import ImageOutput | ||
|
||
from .. import adjustments_group | ||
|
||
|
||
class AdaptiveThresholdType(Enum): | ||
BINARY = cv2.THRESH_BINARY | ||
BINARY_INV = cv2.THRESH_BINARY_INV | ||
|
||
|
||
_THRESHOLD_TYPE_LABELS: Dict[AdaptiveThresholdType, str] = { | ||
AdaptiveThresholdType.BINARY: "Binary", | ||
AdaptiveThresholdType.BINARY_INV: "Binary (Inverted)", | ||
} | ||
|
||
|
||
class AdaptiveMethod(Enum): | ||
MEAN = cv2.ADAPTIVE_THRESH_MEAN_C | ||
GAUSSIAN = cv2.ADAPTIVE_THRESH_GAUSSIAN_C | ||
|
||
|
||
_ADAPTIVE_METHOD_LABELS: Dict[AdaptiveMethod, str] = { | ||
AdaptiveMethod.MEAN: "Mean", | ||
AdaptiveMethod.GAUSSIAN: "Gaussian", | ||
} | ||
|
||
|
||
@adjustments_group.register( | ||
schema_id="chainner:image:threshold_adaptive", | ||
name="Threshold (Adaptive)", | ||
description="Similar to regular threshold, but determines the threshold for a pixel based on a small region around it.", | ||
icon="MdAutoGraph", | ||
inputs=[ | ||
ImageInput(channels=1), | ||
EnumInput( | ||
AdaptiveThresholdType, | ||
"Threshold Type", | ||
default=AdaptiveThresholdType.BINARY, | ||
option_labels=_THRESHOLD_TYPE_LABELS, | ||
).with_id(3), | ||
SliderInput( | ||
"Maximum Value", | ||
maximum=100, | ||
default=100, | ||
precision=1, | ||
controls_step=1, | ||
).with_id(1), | ||
EnumInput( | ||
AdaptiveMethod, | ||
"Adaptive Method", | ||
default=AdaptiveMethod.MEAN, | ||
option_labels=_ADAPTIVE_METHOD_LABELS, | ||
).with_id(2), | ||
NumberInput("Block Radius", default=1, minimum=1).with_id(4), | ||
NumberInput( | ||
"Constant Subtraction", | ||
minimum=-100, | ||
maximum=100, | ||
default=0, | ||
precision=1, | ||
) | ||
.with_id(5) | ||
.with_docs( | ||
"A constant value that is subtracted from the automatically determined adaptive threshold.", | ||
"Assuming that **Threshold Type** is *Binary*, then higher values will result in more white pixels and lower values will result in more black pixels.", | ||
), | ||
AdaptiveMethodInput(), | ||
AdaptiveThresholdInput(), | ||
NumberInput("Block Radius", default=1, minimum=1), | ||
NumberInput("Mean Subtraction"), | ||
], | ||
outputs=[ImageOutput(image_type="Input0")], | ||
limited_to_8bpc=True, | ||
) | ||
def threshold_adaptive_node( | ||
img: np.ndarray, | ||
maxval: float, | ||
adaptive_method: int, | ||
thresh_type: int, | ||
threshold_type: AdaptiveThresholdType, | ||
max_value: float, | ||
adaptive_method: AdaptiveMethod, | ||
block_radius: int, | ||
c: int, | ||
c: float, | ||
) -> np.ndarray: | ||
"""Takes an image and applies an adaptive threshold to it""" | ||
|
||
# Adaptive threshold requires uint8 input | ||
img = to_uint8(img, normalized=True) | ||
|
||
real_maxval = maxval / 100 * 255 | ||
max_value = max_value / 100 * 255 | ||
|
||
result = cv2.adaptiveThreshold( | ||
return cv2.adaptiveThreshold( | ||
img, | ||
real_maxval, | ||
adaptive_method, | ||
thresh_type, | ||
max_value, | ||
adaptive_method.value, | ||
threshold_type.value, | ||
block_radius * 2 + 1, | ||
c, | ||
round(c / 100 * 255), | ||
) | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters