/
feature_color.py
41 lines (33 loc) · 938 Bytes
/
feature_color.py
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
import cv2
import math
import sys
import numpy as np
def getColorFeature(img):
'''
Computes the color feature vector of the image
based on HSV histogram
'''
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(img_hsv)
hsvHist = [[[0 for _ in range(2)] for _ in range(2)] for _ in range(6)]
featurevec = []
hist = cv2.calcHist([img_hsv], [0, 1, 2], None, [6,2,2], [0, 180, 0, 256, 0, 256])
for i in range(6):
for j in range(2):
for k in range(2):
featurevec.append(hist[i][j][k])
feature = featurevec[1:]
M = max(feature)
m = min(feature)
feature = map(lambda x: x * 2, feature)
feature = (feature - M - m)/(M - m);
mean=np.mean(feature)
dev=np.std(feature)
feature = (feature - mean)/dev;
return feature
if __name__ == '__main__':
img = cv2.imread(sys.argv[1])
featureVector = getColorFeature(img)
print featureVector
cv2.waitKey(0)
cv2.destroyAllWindows()