-
Notifications
You must be signed in to change notification settings - Fork 1
/
parameters.py
154 lines (127 loc) · 4.55 KB
/
parameters.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
import numpy as np
import cv2
from matplotlib import pyplot as plt
import matplotlib.cm as cm
def setHoG(img):
height = img.shape[0]
width = img.shape[1]
cell_size = (width/8, width/8) # 8,8
block_size = (width/4, width/4) # 16,16
block_stride = (width/8,width/8) # 8, 8
nbins = 9
hog = cv2.HOGDescriptor(_winSize=(width,height),
_blockSize=(block_size[1],block_size[0]),
_blockStride=(block_stride[1],block_stride[0]),
_cellSize=(cell_size[1],cell_size[0]),
_nbins=nbins, _winSigma=-1)
return hog
def getParameters(img, hog):
width = img.shape[1]
parameters = hog.compute(img, (width/8,width/8), (0,0))
return parameters
"""
height = img.shape[0]
width = img.shape[1]
cell_size = (width/8, width/8) # 8,8
block_size = (width/4, width/4) # 16,16
block_stride = (width/8,width/8) # 8, 8
nbins = 9
n_cells = (img.shape[0] // cell_size[0], img.shape[1] // cell_size[1])
par = hog.compute(img)
print par
print par.shape
print n_cells
parameters = hog.compute(img)\
.reshape(n_cells[1] - block_size[1] + 1,
n_cells[0] - block_size[0] + 1,
block_size[0], block_size[1], nbins) \
.transpose((1, 0, 2, 3, 4))
print parameters
#return parameters
"""
def showHoG(img, hog_feats):
height = img.shape[0]
width = img.shape[1]
cell_size = (width/8, width/8) # 8,8
block_size = (width/4, width/4) # 16,16
block_stride = (width/8,width/8) # 8, 8
nbins = 9
cellsXDir = width/cell_size[0]
cellsYDir = height/cell_size[0]
totalnCells = cellsXDir * cellsYDir
blocksXDir = cellsXDir-1
blocksYDir = cellsYDir-1
gradientStrenghts = np.zeros((cellsYDir,cellsXDir, nbins))
cellUpdateCOunter = np.zeros((cellsYDir, cellsXDir))
index = 0
for x in range(blocksXDir):
for y in range(blocksYDir):
for c in range(0,4):
cellx = x
celly = y
if c == 1:
celly += 1
if c == 2:
cellx += 1
if c == 3:
celly += 1
cellx += 1
for b in range(0,nbins):
gs = hog_feats[index]
index += 1
gradientStrenghts[celly][cellx][b] += gs
cellUpdateCOunter[celly][cellx] += 1
for cellx in range(cellsXDir):
for celly in range(cellsYDir):
updates = cellUpdateCOunter[celly][cellx]
for b in range(0,nbins):
thatBin = gradientStrenghts[celly][cellx][b]
print "before average", thatBin
gradientStrenghts[celly][cellx][b] = thatBin/updates
print "after average", gradientStrenghts[celly][cellx][b]
print gradientStrenghts
#fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)
#ax1.axis('off')
#ax1.imshow(img, cmap=cm.Greys)
#ax1.set_title('Input img')
#ax1.set_adjustable('box-forced')
#ax2.axis('off')
#ax2.imshow(gradientStrenghts[:,:,:-1])
#ax2.set_title('hog')
#ax2.set_adjustable('box-forced')
#plt.show()
plt.imshow(img, cmap=cm.gray)
plt.show()
bin = 5
plt.pcolor(gradientStrenghts[:,:,bin])
plt.gca().invert_yaxis()
plt.gca().set_aspect('equal', adjustable='box')
plt.colorbar()
#plt.figure()
#plt.imshow(img, cmap='gray')
plt.show()
"""
n_cells = (img.shape[0] // cell_size[0], img.shape[1] // cell_size[1])
gradients = np.zeros((n_cells[0], n_cells[1], nbins))
# count cells (border cells appear less often across overlapping groups)
cell_count = np.full((n_cells[0], n_cells[1], 1), 0, dtype=int)
for off_y in range(block_size[0]):
for off_x in range(block_size[1]):
gradients[off_y:n_cells[0] - block_size[0] + off_y + 1,
off_x:n_cells[1] - block_size[1] + off_x + 1] += \
hog_feats[:, :, off_y, off_x, :]
cell_count[off_y:n_cells[0] - block_size[0] + off_y + 1,
off_x:n_cells[1] - block_size[1] + off_x + 1] += 1
# Average gradients
gradients /= cell_count
# Preview
plt.figure()
plt.imshow(img, cmap='gray')
plt.show()
bin = 5 # angle is 360 / nbins * direction
plt.pcolor(gradients[:, :, bin])
plt.gca().invert_yaxis()
plt.gca().set_aspect('equal', adjustable='box')
plt.colorbar()
plt.show()
"""