-
Notifications
You must be signed in to change notification settings - Fork 0
/
HOG.py
168 lines (142 loc) · 6.41 KB
/
HOG.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
#/usr/bin/python
import cv2
import numpy as np
import math
def Sobel_op(img):
rows, cols = img.shape
cp_img = np.empty((rows,cols))
grad_img = np.empty((rows,cols))
for i in range(rows-1):
for j in range(cols-1):
if(i != 0 and j != 0 ):
Gx = (img[i-1,j-1]+(2*img[i,j-1])+img[i+1,j-1])-(img[i-1,j+1]+(2*img[i,j+1])+img[i+1,j+1])
Gy = (img[i-1,j-1]+(2*img[i-1,j])+img[i-1,j+1])-(img[i+1,j-1]+(2*img[i+1,j])+img[i+1,j+1])
else:
Gx = 0
Gy = 0
cp_img[i,j] = math.sqrt((Gx*Gx)+(Gy*Gy))
grad_img[i,j] = abs(math.degrees(math.atan2(Gy,Gx)))
return cp_img, grad_img
def visualise_hog(hog_, img_rows, img_cols, ker_siz ,ker_row, ker_cols):
rows , cols = hog_.shape
blank = np.zeros((img_cols, img_rows))
ghost = np.zeros((img_rows,img_cols))
for i in range(ker_row):
for l in range(ker_cols):
sum = 0
for k in range(cols):
sum += hog_[(i*ker_cols)+l,k]
for j in range(cols):
prob_ho = (hog_[(i*ker_cols)+l,j]/sum)*100
if (prob_ho > 10):
if(j == 0):
cv2.line(blank,((i*ker_siz)+0,(l*ker_siz)+4),((i*ker_siz)+8,(l*ker_siz)+4),prob_ho)
elif(j == 1):
cv2.line(blank,((i*ker_siz)+1,(l*ker_siz)+3),((i*ker_siz)+7,(l*ker_siz)+3),prob_ho)
elif(j == 2):
cv2.line(blank,((i*ker_siz)+2,(l*ker_siz)+2),((i*ker_siz)+6,(l*ker_siz)+6),prob_ho)
elif(j == 3):
cv2.line(blank,((i*ker_siz)+3,(l*ker_siz)+1),((i*ker_siz)+5,(l*ker_siz)+7),prob_ho)
elif(j == 4):
cv2.line(blank,((i*ker_siz)+4,(l*ker_siz)+0),((i*ker_siz)+4,(l*ker_siz)+8),prob_ho)
elif(j == 5):
cv2.line(blank,((i*ker_siz)+5,(l*ker_siz)+1),((i*ker_siz)+3,(l*ker_siz)+7),prob_ho)
elif(j == 6):
cv2.line(blank,((i*ker_siz)+6,(l*ker_siz)+2),((i*ker_siz)+2,(l*ker_siz)+6),prob_ho)
elif(j == 7):
cv2.line(blank,((i*ker_siz)+7,(l*ker_siz)+3),((i*ker_siz)+1,(l*ker_siz)+5),prob_ho)
elif(j == 8):
cv2.line(blank,((i*ker_siz)+8,(l*ker_siz)+4),((i*ker_siz)+0,(l*ker_siz)+4),prob_ho)
for i in range(img_rows):
for j in range(img_cols):
ghost[i,j] = blank[j,i]
return ghost
def HOG(edges,grad_img):
rows , cols = edges.shape
ker_siz = 8
H_rows = int(rows/ker_siz)
H_cols = int(cols/ker_siz)
Hog_val = np.zeros(((H_rows*H_cols),9))
vis_hog = np.zeros((rows,cols))
for i in range(H_rows):
for j in range(H_cols):
value_0 = 0
value_20 = 0
value_40 = 0
value_60 = 0
value_80 = 0
value_100 = 0
value_120 = 0
value_140 = 0
value_160 = 0
for n in range(ker_siz):
for m in range(ker_siz):
grad_value = grad_img[(i*ker_siz)+n,(j*ker_siz)+m]
edges_value = edges[(i*ker_siz)+n,(j*ker_siz)+m]
if ( 0 <= grad_value < 20 ):
value = grad_value - 0
prob_value = (value/20)* edges_value
value_20 += prob_value
value_0 += edges_value-prob_value
elif (20 <= grad_value < 40):
value = grad_value - 20
prob_value = (value/20)* edges_value
value_40 += prob_value
value_20 += edges_value-prob_value
elif (40 <= grad_value < 60):
value = grad_value - 40
prob_value = (value/20)* edges_value
value_60 += prob_value
value_40 += edges_value-prob_value
elif (60 <= grad_value < 80):
value = grad_value - 60
prob_value = (value/20)* edges_value
value_80 += prob_value
value_60 += edges_value-prob_value
elif (80 <= grad_value < 100):
value = grad_value - 80
prob_value = (value/20)* edges_value
value_100 += prob_value
value_80 += edges_value-prob_value
elif (100 <= grad_value < 120):
value = grad_value - 100
prob_value = (value/20)* edges_value
value_120 += prob_value
value_100 += edges_value-prob_value
elif (120 <= grad_value < 140):
value = grad_value - 120
prob_value = (value/20)* edges_value
value_140 += prob_value
value_120 += edges_value-prob_value
elif (140 <= grad_value < 160):
value = grad_value - 140
prob_value = (value/20)* edges_value
value_160 += prob_value
value_140 += edges_value-prob_value
elif (160 <= grad_value < 180):
value = grad_value - 160
prob_value = (value/20)* edges_value
value_0 += prob_value
value_160 += edges_value-prob_value
Hog_val[(i*H_cols)+j,0] = value_0
Hog_val[(i*H_cols)+j,1] = value_20
Hog_val[(i*H_cols)+j,2] = value_40
Hog_val[(i*H_cols)+j,3] = value_60
Hog_val[(i*H_cols)+j,4] = value_80
Hog_val[(i*H_cols)+j,5] = value_100
Hog_val[(i*H_cols)+j,6] = value_120
Hog_val[(i*H_cols)+j,7] = value_140
Hog_val[(i*H_cols)+j,8] = value_160
vis_hog = visualise_hog(Hog_val,rows,cols,ker_siz,H_rows,H_cols)
return Hog_val, vis_hog
def main():
img = cv2.imread("Bikesgray.jpg",0)
edges, img_grad = Sobel_op(img)
ret, thresh = cv2.threshold(edges,120,255,cv2.THRESH_BINARY)
H,v_h = HOG(edges,img_grad)
cv2.imshow("th",thresh)
cv2.imshow("H_v", v_h)
cv2.waitKey(0)
cv2.destoryAllWindows()
if __name__ == "__main__":
main()