-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhog_extraction.py
178 lines (134 loc) · 3.78 KB
/
hog_extraction.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
import numpy as np # linear algebra
import json
from matplotlib import pyplot as plt
from skimage import color
from skimage.feature import hog
import pickle
# Load Ship Dataset
try:
pickle_in = open("dataset.pickle", "rb")
dataset = pickle.load(pickle_in)
pickle_in.detach()
except:
f = open(r'input/shipsnet.json')
dataset = json.load(f)
f.close()
pickle_out = open("dataset.pickle", "wb")
pickle.dump(dataset, pickle_out)
pickle_out.close()
# f = open(r'input/shipsnet.json')
# dataset = json.load(f)
# f.close()
dataset.keys()
print(dataset.keys())
data = np.array(dataset['data']).astype('uint8')
data.shape
print(data.shape)
img_length = 80
data = data.reshape(-1,3,img_length,img_length).transpose([0,2,3,1])
data.shape
labels = np.array(dataset['labels']).reshape(len(dataset['labels']),1)
print(labels)
fig = plt.figure(figsize=(8, 8))
fig.suptitle('Image of Ships in Dataset', fontsize=20)
plt.subplot(2, 2, 1)
plt.imshow(data[0])
plt.subplot(2, 2, 2)
plt.imshow(data[1])
plt.subplot(2, 2, 3)
plt.imshow(data[2])
plt.subplot(2, 2, 4)
plt.imshow(data[3])
plt.show()
fig = plt.figure(figsize=(8, 8))
fig.suptitle('Image of Non-Ships in Dataset', fontsize=20)
plt.subplot(2, 2, 1)
plt.imshow(data[1001])
plt.subplot(2, 2, 2)
plt.imshow(data[2828])
plt.subplot(2, 2, 3)
plt.imshow(data[2813])
plt.subplot(2, 2, 4)
plt.imshow(data[3000])
plt.show()
data_gray = [ color.rgb2gray(i) for i in data]
fig = plt.figure(figsize=(8, 8))
fig.suptitle('Grayscale of Ship Images in Dataset', fontsize=20)
plt.subplot(2, 2, 1)
plt.imshow(data_gray[0])
plt.subplot(2, 2, 2)
plt.imshow(data_gray[1])
plt.subplot(2, 2, 3)
plt.imshow(data_gray[2])
plt.subplot(2, 2, 4)
plt.imshow(data_gray[3])
plt.show()
fig = plt.figure(figsize=(8, 8))
fig.suptitle('Grayscale of Non-Ship Images in Dataset', fontsize=20)
plt.subplot(2, 2, 1)
plt.imshow(data_gray[1001])
plt.subplot(2, 2, 2)
plt.imshow(data_gray[2828])
plt.subplot(2, 2, 3)
plt.imshow(data_gray[2813])
plt.subplot(2, 2, 4)
plt.imshow(data_gray[3000])
plt.show()
ppc = 16
hog_images = []
hog_features = []
for image in data_gray:
fd,hog_image = hog(image, orientations=16, pixels_per_cell=(ppc,ppc),cells_per_block=(4, 4),block_norm= 'L2',visualise=True)
hog_images.append(hog_image)
hog_features.append(fd)
fig = plt.figure(figsize=(8, 8))
fig.suptitle('HOG Features (O=16) in Ship Images in Dataset', fontsize=20)
plt.subplot(2, 2, 1)
plt.imshow(hog_images[0])
plt.subplot(2, 2, 2)
plt.imshow(hog_images[1])
plt.subplot(2, 2, 3)
plt.imshow(hog_images[2])
plt.subplot(2, 2, 4)
plt.imshow(hog_images[3])
plt.show()
fig = plt.figure(figsize=(8, 8))
fig.suptitle('HOG Features (O=16) in Grayscale Non-ship Images in Dataset', fontsize=20)
plt.subplot(2, 2, 1)
plt.imshow(hog_images[1001])
plt.subplot(2, 2, 2)
plt.imshow(hog_images[2828])
plt.subplot(2, 2, 3)
plt.imshow(hog_images[2813])
plt.subplot(2, 2, 4)
plt.imshow(hog_images[3000])
plt.show()
ppc = 16
hog_images = []
hog_features = []
for image in data_gray:
fd,hog_image = hog(image, orientations=8, pixels_per_cell=(ppc,ppc),cells_per_block=(4, 4),block_norm= 'L2',visualise=True)
hog_images.append(hog_image)
hog_features.append(fd)
fig = plt.figure(figsize=(8, 8))
fig.suptitle('HOG Features (O=8) in Ship Images in Dataset', fontsize=20)
plt.subplot(2, 2, 1)
plt.imshow(hog_images[0])
plt.subplot(2, 2, 2)
plt.imshow(hog_images[1])
plt.subplot(2, 2, 3)
plt.imshow(hog_images[2])
plt.subplot(2, 2, 4)
plt.imshow(hog_images[3])
plt.show()
fig = plt.figure(figsize=(8, 8))
fig.suptitle('HOG Features (O=8) in Grayscale Non-ship Images in Dataset', fontsize=20)
plt.subplot(2, 2, 1)
plt.imshow(hog_images[1001])
plt.subplot(2, 2, 2)
plt.imshow(hog_images[2828])
plt.subplot(2, 2, 3)
plt.imshow(hog_images[2813])
plt.subplot(2, 2, 4)
plt.imshow(hog_images[3000])
plt.show()