-
Notifications
You must be signed in to change notification settings - Fork 0
/
LOMO.py
363 lines (276 loc) · 10.5 KB
/
LOMO.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
'''
********************=========0=========******************
belong to bigChuang Project: Person Re-ID
please take this python file to the Project directory
reference from: ......
liangzia,2018,5,6 finally
warning: make sure python version =3.x, and make sure you have installed numpy,opencv3,cv2(python)
********************===================******************
how to use it
###########___________1___________##########
加载这两个库
import numpy as np
import cv2
加载本文件
import LOMO
设置图片路径
path=
读取,特征提取
img=cv2.imread(path)
lomo=LOMO.LOMO(img)
print(lomo,lomo.shape)
如果是多个文件:
额外加载
import os
path='./data/VIPeR/cam_a'
img_list=os.listdir(path)
for img_name in img_list:
img_path=os.path.join(path,img_name)
img=cv2.imread(img_path)
#img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
lomo=LOMO.LOMO(img)
print(lomo)
###########_________2__________############
para:参数解释
img:输入图像,一张,因此读取的时候如若是照片流则需要用for循环,这里和MATLAB版本的LOMO有所不同
c_list: MSR中的方差的列表或元祖
low_clip:MSRCP中使用的剪裁尺寸,下同
high_clip:
R_list=SILTP算法的参数,下同
tau=
blocksize= size of the sub-window for histogram counting.
block_step= sliding step for the sub-windows.
hsv_bin_size number of bins for HSV channels.
'''
import numpy as np
import cv2
'''
inverse_nomalize:
这个实现的功能是反归一化,由于神经网络读取数据时已经对数据进行了归一化的操作,因此需要进行反归一化,这样才能将数据传递给LOMO算法
本函数只对三维张量(RGB的彩色图像之类)有效
liangzia,2018,8,19
'''
def inverse_nomalize(img,mean,variance):
for i in range(3):
img[:,:,i]=(img[:,:,i]*variance[i]+mean[i])*255
x,y,z=img.shape
for j in range(x):
for k in range(y):
img[j,k,i]=abs(img[j,k,i])
#print('*'*20)
#print(img)
return img
'''
image retinex algothrim
it has:
SSR, MSR, MSRCR, MSRCP, and so on.
'''
def Retinex_SingleScale(img,c):
#print(img)
wait=np.abs(cv2.GaussianBlur(img,(5,5),c))
#print('000'*30)
#print(wait)
retinex=np.log10(img)-np.log10(wait)
#print(retinex)
return retinex
def Retinex_MultiScale(img,c_list):
retinex=np.zeros_like(img)
for c in c_list:
retinex+=Retinex_SingleScale(img,c)
retinex=retinex/len(c_list)
return retinex
def Retinex_MSRCR_ColorRestoration(img,alpha,belta):
img_sum=np.sum(img,axis=2,keepdims=True)
return belta*(np.log10(alpha*img)-np.log10(img_sum))
def Retinex_SimplistColorBalance(img,lowclip,highclip):
total=img.shape[0]*img.shape[1]
for i in range(img.shape[2]):
unique,counts = np.unique(img[:,:,i],return_counts=True)
#unique函数的作用是找到张量中不同元素的值,将其赋予unique(从小到大排序),然后将索引赋予count
current=0
for u,c in zip(unique,counts):
if float(current)/total<lowclip:
low_val=u
if float(current)/total<highclip:
high_val=u
current+=c
img[:,:,i]=np.maximum(np.minimum(img[:,:,i],high_val),low_val)
return img
def Retinex_MSRCR(img,c_list,G,b,alpha,belta,low_clip,high_clip):
img=np.float(img)+1.0
img_retinex=Retinex_MultiScale(img,c_list,)
img_color=Retinex_MSRCR_ColorRestoration(img,alpha,belta)
img_msrcr=G*(img_retinex*img_color+b)
img_msrcr=np.uint8(np.minimum(np.maximum(img_msrcr,0),255))
img_msrcr=Retinex_SimplistColorBalance(img_msrcr,low_clip,high_clip)
return img_msrcr
def Retinex_AutomatedMSRCR(img, sigma_list):
img = np.float(img) + 1.0
img_retinex = Retinex_MultiScale(img, sigma_list)
for i in range(img_retinex.shape[2]):
unique, count = np.unique(np.int32(img_retinex[:, :, i] * 100), return_counts=True)
for u, c in zip(unique, count):
if u == 0:
zero_count = c
break
low_val = unique[0] / 100.0
high_val = unique[-1] / 100.0
for u, c in zip(unique, count):
if u < 0 and c < zero_count * 0.05:
low_val = u / 100.0
if u > 0 and c < zero_count * 0.05:
high_val = u / 100.0
break
img_retinex[:, :, i] = np.maximum(np.minimum(img_retinex[:, :, i], high_val), low_val)
img_retinex[:, :, i] = (img_retinex[:, :, i] - np.min(img_retinex[:, :, i])) / \
(np.max(img_retinex[:, :, i]) - np.min(img_retinex[:, :, i])) \
* 255
img_retinex = np.uint8(img_retinex)
return img_retinex
def Retinex_MSRCP(img, sigma_list, low_clip, high_clip):
img = img + 1.0
intensity = np.sum(img, 2) / img.shape[2]
retinex = Retinex_MultiScale(intensity, sigma_list)
intensity = np.expand_dims(intensity, 2)
retinex = np.expand_dims(retinex, 2)
intensity1 = Retinex_SimplistColorBalance(retinex, low_clip, high_clip)
intensity1 = (intensity1 - np.min(intensity1)) / \
(np.max(intensity1) - np.min(intensity1)) * \
255.0 + 1.0
img_msrcp = np.zeros_like(img)
for y in range(img_msrcp.shape[0]):
for x in range(img_msrcp.shape[1]):
B = np.max(img[y, x])
A = np.minimum(256.0 / B, intensity1[y, x, 0] / intensity[y, x, 0])
img_msrcp[y, x, 0] = A * img[y, x, 0]
img_msrcp[y, x, 1] = A * img[y, x, 1]
img_msrcp[y, x, 2] = A * img[y, x, 2]
img_msrcp = np.uint8(img_msrcp - 1.0)
return img_msrcp
'''
SILTP algothrim(CVPR2010)
LBP对噪声敏感,LTP对光照敏感,SILTP是二者的改进版
'''
def SILTP(img,R,tau):
if len(img.shape)>2: # 在这里img如果是彩图就应该是3维张量,如果是2维张量就是灰度图
img=np.array(img,dtype=np.uint8)
#print(1111111111)
#print(img.shape)
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_pad=np.pad(img,R,'edge') #pad 函数是填充函数,'edge'代表边缘填充,填充多少取决于R
R_=-1*R
img_u=img_pad[:2*R_,R:R_]
img_d=img_pad[2*R:,R:R_]
img_l=img_pad[R:R_,2*R:]
img_r=img_pad[R:R_,:2*R_]
up_limit=(1+tau)*img
low_limit=(1-tau)*img
siltp=((img_u<low_limit)+(img_u>up_limit)*2)+((img_d<low_limit)+(img_d>up_limit)*2)*3+\
((img_r<low_limit)+(img_r>up_limit)*2)*(9)+((img_l<low_limit)+(img_l>up_limit)*2)*27
return siltp
'''
LOMO algothrim
'''
def jointHistogram(img,boundary,bin_size):
interval=(boundary[1]-boundary[0]+1)/bin_size
if len(img.shape)>2:
histsize=bin_size**(img.shape[2])
img_bin=np.zeros([img.shape[0],img.shape[1]],np.int32)
for i in range(img.shape[2]):
img_bin=img_bin+np.floor((img[:,:,i]-boundary[0])/interval)/(bin_size**i)
else:
histsize=bin_size
img_bin=(img-boundary[0])/interval
unique,counts=np.unique(img_bin,return_counts=True)
unique=unique.astype(np.int64)
histogram=np.zeros([histsize])
for u,c in zip(unique,counts):
histogram[int(u)]=int(c)
return histogram
def averagePooling(img):
if img.shape[0]%2 !=0:
img=img[:-1]
if img.shape[1]%2 !=0:
img=img[:,:-1]
img_pool=img[::2]+img[1::2]
img_pool=img_pool[:,::2]+img_pool[:,1::2]
img_pool=img_pool/4
return img_pool
def LOMO(img,c_list=[5,20],low_clip=0.1,high_clip=0.9,
R_list=[3,5],tau=0.3,hsv_bin_size=8,blocksize=32,block_step=16):
ss,xx,yy,zz=img.shape
#print('the data is {0}*{1}*{2}*{3}'.format(ss,xx,yy,zz))
img_cp=np.zeros((yy,zz,xx))
for i in range(xx):
img_cp[:,:,i]=img[0,i,:,:]
mean=[0.485, 0.456, 0.406]
variance=[0.229, 0.224, 0.225]
img_cp=inverse_nomalize(img_cp,mean,variance)
img=img_cp
#print(img)
#print(img_cp.shape)
img_retinex=Retinex_MSRCP(img_cp,c_list,low_clip,high_clip)
#print('===================================================')
#print(img_retinex.shape)
siltp_feat=np.array([])
hsv_feat=np.array([])
#for pool in range(3):
row_num=int((img_cp.shape[0]-(blocksize-block_step))/block_step)
col_num=int((img_cp.shape[1]-(blocksize-block_step))/block_step)
#liangzia=0
for row in range(row_num):
for col in range(col_num):
img_block=img_cp[
row*block_step:row*block_step+blocksize,
col*block_step:col*block_step+blocksize
]
siltp_hist=np.array([])
for R in R_list:
siltpp=SILTP(img_block,R,tau)
unique,count=np.unique(siltpp,return_counts=True)
siltp_hist_r=np.zeros([3**4])
for u,c in zip(unique,count):
siltp_hist_r[u]=c
siltp_hist=np.concatenate([siltp_hist,siltp_hist_r],0)
img_block2=img_retinex[
row * block_step:row * block_step + blocksize,
col * block_step:col * block_step + blocksize
]
img_block_copy=np.array(img_block2,dtype=np.uint8)
#print(img_block_copy.shape)
img_hsv=cv2.cvtColor(img_block_copy,cv2.COLOR_BGR2HSV)
#print('--------------------------------------{}'.format(liangzia))
#liangzia+=1
#print(img_hsv.shape)
hsv_hist=jointHistogram(
img_hsv,
[0,255],
hsv_bin_size
)
#print('hsv shape is:',hsv_hist.shape)
if col==0:
siltp_feat_col=siltp_hist
hsv_feat_col=hsv_hist
else:
siltp_feat_col=np.maximum(siltp_feat_col,siltp_hist)
hsv_feat_col=np.maximum(hsv_feat_col,hsv_hist)
siltp_feat=np.concatenate([siltp_feat,siltp_feat_col],0)
hsv_feat=np.concatenate([hsv_feat,hsv_feat_col],0)
img=averagePooling(img)
img_retinex=averagePooling(img_retinex)
siltp_feat=np.log(siltp_feat+1.0)
siltp_feat[:int(siltp_feat.shape[0]/2)]/=np.linalg.norm(siltp_feat[:int(siltp_feat.shape[0]/2)])
siltp_feat[int(siltp_feat.shape[0] / 2):] /= np.linalg.norm(siltp_feat[int(siltp_feat.shape[0] / 2):])
hsv_feat=np.log(hsv_feat+1.)
hsv_feat/=np.linalg.norm(hsv_feat)
lomo=np.concatenate([siltp_feat,hsv_feat],0)
'''
print(lomo)
liangzi,=lomo.shape
papri=0
for i in range(liangzi):
if lomo[i]==0.0:
papri+=1
print(papri)
'''
return lomo