-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.py
More file actions
80 lines (49 loc) · 2.2 KB
/
utils.py
File metadata and controls
80 lines (49 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import numpy as np
import cv2
def EuclideanDistance(detected_color, target_colors):
return np.linalg.norm(detected_color - target_colors, axis=1)
def ManhattanDistance(detected_color, target_colors):
return np.sum(np.abs(detected_color - target_colors), axis=1)
def CosineSimilarity(detected_color, target_colors):
return -np.dot(target_colors, detected_color) / (np.linalg.norm(detected_color) * np.linalg.norm(target_colors, axis=1))
def RGBWeightedDistance(detected_color, target_colors):
detected_color = np.array(detected_color, dtype=np.float64)
target_colors = np.array(target_colors, dtype=np.float64)
weights = np.array([0.299, 0.587, 0.114])
diff = detected_color - target_colors
return np.sqrt(np.sum(weights * diff ** 2, axis=1))
def RGBWeightedSimilarity(detected_color, target_colors):
detected_color = np.array(detected_color, dtype=np.float64)
target_colors = np.array(target_colors, dtype=np.float64)
weights = np.sqrt(np.array([0.299, 0.587, 0.114]))
weighted_detected = detected_color * weights
weighted_targets = target_colors * weights
dot_products = np.dot(weighted_targets, weighted_detected)
norm1 = np.linalg.norm(weighted_detected)
norm2 = np.linalg.norm(weighted_targets, axis=1)
denom = norm1 * norm2
denom = np.maximum(denom, 1e-10)
return -dot_products / denom
def HSVColorSimilarity(detected_color, target_colors):
detected_color = np.array(detected_color)
target_colors = np.array(target_colors)
h1, s1, _ = detected_color
h2 = target_colors[:, 0]
s2 = target_colors[:, 1]
h1_rad = np.radians(h1)
h2_rad = np.radians(h2)
v1_x = s1 * np.cos(h1_rad)
v1_y = s1 * np.sin(h1_rad)
v1 = np.array([v1_x, v1_y])
v2_x = s2 * np.cos(h2_rad)
v2_y = s2 * np.sin(h2_rad)
v2 = np.vstack([v2_x, v2_y])
dot_products = np.dot(v1, v2)
v1_norm = np.linalg.norm(v1)
v2_norms = np.linalg.norm(v2, axis=0)
denom = v1_norm * v2_norms
denom = np.maximum(denom, 1e-10)
similarities = dot_products / denom
return -similarities
def Blur(image, kernel_size):
return cv2.medianBlur(image.astype(np.uint8), kernel_size).astype(np.float32)