-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageFeatures.py
More file actions
155 lines (143 loc) · 4.4 KB
/
ImageFeatures.py
File metadata and controls
155 lines (143 loc) · 4.4 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'''
Created on 2014/01/27
@author: MOMO
'''
import cv2
import numpy as np
import math
from matplotlib import pyplot as plt
import Image
'''
@param path: image path
@return: image's brightness evaluated in its YUV space
'''
def get_brightness_feature(path):
img = cv2.imread(path)
YUV = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
sum = 0.0
count = 0.0
for i in range(0, len(YUV)):
for j in range(0, len(YUV[0])):
element = YUV[i][j]
count = count+1
sum = sum+element[0]
print "get image: "+ path+"'s brightness feature. (" + str(sum/count)+")"
return sum/count
'''
@param path: image path
@return: image's average saturation evaluated in its HSV space
'''
def get_saturation_feaure(path):
img = cv2.imread(path)
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
sum = 0.0
count = 0.0
for i in range(0, len(hsv)):
for j in range(0, len(hsv[0])):
element = hsv[i][j]
count = count+1
sum = sum+element[1]
print "get image: "+ path+"'s saturation feature. (" + str(sum/count)+")"
return sum/count
'''
@param path: image path
@return: image's colorfulness feature evaluated in its rgb space
'''
def get_colorfulness_feature(path):
imgBGR = cv2.imread(path)
img = np.array(imgBGR, dtype='int64')
rg = []
yb = []
for i in range(0, len(img)):
for j in range(0, len(img[0])):
element = img[i][j]
rg.append(float(element[2]-element[1]))
temp = float(element[2]+element[1])
yb.append(temp/2-element[0])
numrg = np.array(rg)
numyb = np.array(yb)
drg = np.std(numrg)
dyb = np.std(numyb)
arg = np.average(numrg)
ayb = np.average(numyb)
cf = math.sqrt(math.pow(drg, 2)+math.pow(dyb, 2))+0.3*math.sqrt(math.pow(arg,2)+math.pow(ayb,2))
print "get image: "+ path+"'s colorfulness feature. (" + str(cf)+")"
return cf
'''
@param path: image path
@return: image's naturalness feature evaluated in its HSL space
'''
def get_naturalness_feature(path):
img = cv2.imread(path)
hls = cv2.cvtColor(img,cv2.COLOR_BGR2HLS)
skin = []
grass = []
sky = []
count = 0.0
for i in range(0, len(hls)):
for j in range(0, len(hls[0])):
element = hls[i][j]
count = count + 1
if element[1]>=20 and element[1]<=80 and element[2]>0.1:
if element[0]>=25 and element[0]<=70:
skin.append(element[2])
elif element[0]>=95 and element[0]<=135:
grass.append(element[2])
elif element[0]>=185 and element[0]<=260:
sky.append(element[2])
else:
pass
if len(skin)==0:
askin = 0.0
else:
askin = np.average(np.array(skin))
if len(grass)==0:
agrass = 0.0
else:
agrass = np.average(np.array(grass))
if len(sky)==0:
asky = 0.0
else:
asky = np.average(np.array(sky))
n_skin = math.pow(math.e, -0.5*math.pow((askin-0.76)/0.52,2))
n_grass = math.pow(math.e, -0.5*math.pow((agrass-0.81)/0.53,2))
n_sky = math.pow(math.e, -0.5*math.pow((asky-0.43)/0.22,2))
n = (len(skin)/count)*n_skin+(len(grass)/count)*n_grass+(len(sky)/count)*n_sky
print "get image: "+ path+"'s naturalness feature. (" + str(n)+")"
return n
'''
@param path: image path
@return: image's contrast feature evaluated in its gray space
'''
def get_contrast_feature(path):
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
avg = np.average(np.array(gray))
sum = 0.0
count = 0.0
for i in range(0, len(gray)):
for j in range(0, len(gray[0])):
element = gray[i][j]
count = count+1
sum = sum + math.pow((element-avg), 2)
c = sum/(count-1)
print "get image: "+ path+"'s contrast feature. (" + str(c)+")"
return c
'''
@param path: image path
@return: image's sharpness feature
'''
def get_sharpness_feature(path):
kernel_size = 3
scale = 1
delta = 0
ddepth = cv2.CV_16S
img = cv2.imread(path)
img = cv2.GaussianBlur(img,(3,3),0)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray_lap = cv2.Laplacian(gray,ddepth,ksize = kernel_size,scale = scale,delta = delta)
s = np.max(np.array(gray_lap))
print "get image: "+ path+"'s sharpness feature. (" + str(s)+")"
return s
#test
#code