mirror of
https://gitee.com/sui-feng-cb/AzurLaneAutoScript1
synced 2026-04-13 03:35:51 +08:00
Opt: Faster color_similarity_2d
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from PIL import ImageStat
|
from PIL import ImageStat
|
||||||
|
|
||||||
@@ -299,10 +300,11 @@ def color_similarity_2d(image, color):
|
|||||||
Returns:
|
Returns:
|
||||||
np.ndarray: uint8
|
np.ndarray: uint8
|
||||||
"""
|
"""
|
||||||
diff = np.array(image).astype(int) - color
|
r, g, b = cv2.split(cv2.subtract(image, (*color, 0)))
|
||||||
diff = 255 - np.max(np.maximum(diff, 0), axis=2) + np.min(np.minimum(diff, 0), axis=2)
|
positive = cv2.max(cv2.max(r, g), b)
|
||||||
image = np.clip(diff, 0, 255).astype(np.uint8)
|
r, g, b = cv2.split(cv2.subtract((*color, 0), image))
|
||||||
return image
|
negative = cv2.max(cv2.max(r, g), b)
|
||||||
|
return cv2.subtract(255, cv2.add(positive, negative))
|
||||||
|
|
||||||
|
|
||||||
def extract_letters(image, letter=(255, 255, 255), threshold=128):
|
def extract_letters(image, letter=(255, 255, 255), threshold=128):
|
||||||
@@ -316,10 +318,11 @@ def extract_letters(image, letter=(255, 255, 255), threshold=128):
|
|||||||
Returns:
|
Returns:
|
||||||
np.ndarray: Shape (height, width)
|
np.ndarray: Shape (height, width)
|
||||||
"""
|
"""
|
||||||
image = color_similarity_2d(np.array(image), color=letter)
|
r, g, b = cv2.split(cv2.subtract(image, (*letter, 0)))
|
||||||
image = (255.0 - image) * (255.0 / threshold)
|
positive = cv2.max(cv2.max(r, g), b)
|
||||||
image = np.clip(image, 0, 255).astype(np.uint8)
|
r, g, b = cv2.split(cv2.subtract((*letter, 0), image))
|
||||||
return image
|
negative = cv2.max(cv2.max(r, g), b)
|
||||||
|
return cv2.multiply(cv2.add(positive, negative), 255.0 / threshold)
|
||||||
|
|
||||||
|
|
||||||
def red_overlay_transparency(color1, color2, red=247):
|
def red_overlay_transparency(color1, color2, red=247):
|
||||||
|
|||||||
Reference in New Issue
Block a user