-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhist_compare_many.py
172 lines (142 loc) · 6.37 KB
/
hist_compare_many.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import time
from matplotlib import pyplot as plt
import os
import cv2
import numpy as np
import glob
from natsort import natsorted
import xlwt
def rgb2hsi(img_rgb):
rows = int(img_rgb.shape[0])
cols = int(img_rgb.shape[1])
B, G, R = cv2.split(img_rgb)
# Normalize to [0,1]
B = B / 255.0
G = G / 255.0
R = R / 255.0
img_hsi = img_rgb.copy()
H, S, I = cv2.split(img_hsi)
for i in range(rows):
for j in range(cols):
num = 0.5 * ((R[i, j] - G[i, j]) + (R[i, j] - B[i, j]))
den = np.sqrt((R[i, j] - G[i, j]) ** 2 + (R[i, j] - B[i, j]) * (G[i, j] - B[i, j]))
theta = float(np.arccos(num / den))
if den == 0:
H = 0
elif B[i, j] <= G[i, j]:
H = theta
else:
H = 2 * np.pi - theta
min_RGB = min(min(B[i, j], G[i, j]), R[i, j])
sum = B[i, j] + G[i, j] + R[i, j]
if sum == 0:
S = 0
else:
S = 1 - 3 * min_RGB / sum
H = H / (2 * np.pi)
I = sum / 3.0
# H = H * 360
# For ease of understanding, the results are often extended, that is, [0°, 360°], [0,100], [0,255]
img_hsi[i, j, 0] = H * 360
img_hsi[i, j, 1] = S * 100
img_hsi[i, j, 2] = I * 255
# Or in order to facilitate the calculation of the histogram, expanded to 0~255 (same as RGB)
# img_hsi[i, j, 0] = H * 255
# img_hsi[i, j, 1] = S * 255
# img_hsi[i, j, 2] = I * 255
return img_hsi
class Hisogram(object):
# def create_rgb_hist(self, image, color_type=1):
# """
# Get color space histogram
# """
# h, w, c = image.shape
# rgHist = np.zeros([16 * 16 * 16, 1], np.float32)
# print(rgHist)
# hsize = 256 / 16
# for row in range(0, h, 1):
# for col in range(0, w, 1):
# b = image[row, col, 0]
# g = image[row, col, 1]
# r = image[row, col, 2]
# index = np.int(b / hsize) * 16 * 16 + np.int(g / hsize) * 16 + np.int(r / hsize)
# rgHist[np.int(index), 0] = rgHist[np.int(index), 0] + 1
# return rgHist
def hist_compare(self, hist1, hist2):
"""
compare two histograms
"""
# hist1 = self.create_rgb_hist(image1)
# hist2 = self.create_rgb_hist(image2)
match1 = cv2.compareHist(hist1, hist2, cv2.HISTCMP_BHATTACHARYYA)
# match2 = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL)
# match3 = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CHISQR)
# match4 = cv2.compareHist(hist1, hist2, cv2.HISTCMP_INTERSECT)
# print("Bhattacharyya distance:%20s, Correlation:%20s, Chi-square:%s, HISTCMP_INTERSECT:%s " % (match1, match2, match3, match4))
# return match1, match2, match3, match4
return match1
# def hist_image(self, image):
# color = ("Hue", "Saturity", "Intensity")
# for i, color in enumerate(color):
# hist = cv2.calcHist([image], [i], None, [256], [0, 256]) # Calculate the histogram of RGB
# # hist = cv.calcHist([image], [0,1], None, [180,256], [0,180,0,256]) # Calculate the histogram of H-S
# print(hist)
if __name__ == '__main__':
base_dir = '../data/key_data/key_global_3'
# save_path = '../data/keyframe_data/'
game_list = os.listdir(base_dir)
for game in game_list:
event_list = os.listdir(os.path.join(base_dir, game))
for event in event_list:
print(event)
workbook = xlwt.Workbook(encoding='utf-8')
pic_list = natsorted(os.listdir(os.path.join(base_dir, game, event)))
img_count = 0
for pic in pic_list:
time_start = time.time()
# print(pic)
img_list = natsorted(glob.glob(os.path.join(base_dir, game, event, pic) + '/*.png'))
img_num = len(img_list)
end = img_num - 1
firstfilename = os.path.basename(img_list[0])
first_filename = firstfilename.replace("global_", "")
save_count = 1
worksheet = workbook.add_sheet('{}'.format(pic))
worksheet.write(0, 0, label=u'pic')
worksheet.write(0, 1, label=u'Bhattacharyya distance')
# worksheet.write(0, 2, label=u'Correlation')
# worksheet.write(0, 3, label=u'Chi-square')
# worksheet.write(0, 4, label=u'HISTCMP_INTERSECT')
hist_list = []
for img in img_list:
img_rgb = cv2.imread(img)
# img_hsi = rgb2hsi(img_rgb)
hsv1 = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2HSV)
hist_hi = cv2.calcHist([hsv1], [0], None, [360], [0, 359])
# hist_hi = cv2.calcHist([img_hsi], [0, 2], None, [25, 25], [0, 359, 0, 255])
hist_list.append(hist_hi)
time_end = time.time()
print('Time cost = %fs' % (time_end - time_start))
for index in range(0, end, 1):
hist_hi1 = hist_list[index]
hist_hi2 = hist_list[index + 1]
myHist = Hisogram()
# match1, match2, match3, match4 = myHist.hist_compare(hist_h1, hist_h2)
match1 = myHist.hist_compare(hist_hi1, hist_hi2)
img_name = os.path.basename(img_list[index + 1]).replace(".png", "")
flo_count = img_name.replace("global_", "")
worksheet.write(save_count, 0, flo_count)
worksheet.write(save_count, 1, match1)
# worksheet.write(save_count, 2, match2)
# worksheet.write(save_count, 3, match3)
# worksheet.write(save_count, 4, match4)
save_count += 1
img_count += 1
time_end = time.time()
print('Time cost = %fs' % (time_end - time_start))
# print(img_count)
xls_save_path = os.path.join('../data/excel', game)
isExists_2 = os.path.exists(xls_save_path)
if not isExists_2:
os.makedirs(xls_save_path)
workbook.save(xls_save_path + '/{}_hist.xls'.format(event))