-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarColor_Min_DistanceHSV.py
More file actions
93 lines (71 loc) · 3.46 KB
/
CarColor_Min_DistanceHSV.py
File metadata and controls
93 lines (71 loc) · 3.46 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
81
82
83
84
85
86
87
88
89
90
91
92
93
# https://www.quora.com/Why-use-an-HSV-image-for-color-detection-rather-than-an-RGB-image#:~:text=The%20reason%20we%20use%20HSV,relatively%20lesser%20than%20RGB%20values.
#The reason we use HSV colorspace for color detection/thresholding over RGB/BGR is that HSV is more robust
# towards external lighting changes. This means that in cases of minor changes in external lighting (such as pale shadows,etc. )
# Hue values vary relatively lesser than RGB values.
import numpy as np
from matplotlib import pyplot as plt
import cv2
#https://stackoverflow.com/questions/63066842/how-to-convert-hsv-to-rgb-in-python
import colorsys
# https://stackoverflow.com/questions/24852345/hsv-to-rgb-color-conversion
def hsv2rgb(h,s,v):
return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(h,s,v))
def ValorMaxHistogram(img):
# https://docs.opencv.org/3.4/d1/db7/tutorial_py_histogram_begins.html
hist = cv2.calcHist([img],[0],None,[256],[0,256]) # return 256* 1 array
#print("HIST")
#print(hist)
OcurrenciasMax=0
ValorMax=0
for i in range(len(hist)):
if i==0: continue
if hist[i] > OcurrenciasMax:
OcurrenciasMax=hist[i]
ValorMax=i
#print("return max valor " + str(ValorMax))
return OcurrenciasMax, ValorMax
def CarColorImg_Min_Distance(img, model, TabNames, License):
# convert from RGB to HSV format
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# https://stackoverflow.com/questions/17185151/how-to-obtain-a-single-channel-value-image-from-hsv-image-in-opencv-2-1
img0=img[:, :, 0]
counts, h=ValorMaxHistogram(img0)
img[:, :, 0]=np.where(img[:, :, 0]==h, 180, img[:, :, 0])
img1=img[:, :, 1]
counts, s=ValorMaxHistogram(img1)
img[:, :, 1]=np.where(img[:, :, 1]==s, 255, img[:, :, 1])
img2=img[:, :, 2]
counts, v=ValorMaxHistogram(img2)
img[:, :, 2]=np.where(img[:, :, 2]==v, 255, img[:, :, 2])
print(" Calculated H S V values h =" + str(h)+ " s = " + str(s) + " v = " + str(v))
# https://stackoverflow.com/questions/24852345/hsv-to-rgb-color-conversion
# dvide by max values of h, s, v getting decimal values
# that colorsys needs
# https://www.lifewire.com/what-is-hsv-in-design-1078068
cv2.imshow("ROI", img)
cv2.waitKey()
# Max value of hue h is 180 if opencv is used
# https://stackoverflow.com/questions/16685707/why-is-the-range-of-hue-0-180-in-opencv
h=h/180.0
s=s/255.0
v=v/255.0
#print("h =" + str(h)+ " s = " + str(s) + " v = " + str(v))
#RGB from 0 to 1 NO from 0 to 255
#r, g, b = colorsys.hsv_to_rgb(h, s, v)
# https://stackoverflow.com/questions/24852345/hsv-to-rgb-color-conversion
r, g, b = hsv2rgb(h,s,v)
print("Calculated RGB values R =" + str(r)+ " G = " + str(g) + " B = " + str(b))
x_test=[]
# normalization dont impruve
#x_test.append(r/255.0)
#x_test.append(g/255.0)
#x_test.append(b/255.0)
x_test.append(r)
x_test.append(g)
x_test.append(b)
X_test=[]
X_test.append(np.array(x_test))
Y_predict_test=model.predict(X_test)
#print(X_test)
#print(Y_predict)
return r, g, b, Y_predict_test