質問編集履歴

3

情報の詳細化

2017/08/16 09:09

投稿

bof
bof

スコア18

test CHANGED
File without changes
test CHANGED
@@ -299,3 +299,5 @@
299
299
  print("suceess percentage:", success/(success+fail))
300
300
 
301
301
  ```
302
+
303
+ 取り込んだ画像をpatchにしたものから特徴点(keypoint)を抽出する段階で、特徴点をpatchの中心に指定したいのですが,そこがうまくいってません。

2

情報の詳細化

2017/08/16 09:09

投稿

bof
bof

スコア18

test CHANGED
File without changes
test CHANGED
@@ -144,9 +144,7 @@
144
144
 
145
145
  #keypoints, descriptors= detector.detectAndCompute(patches, None)
146
146
 
147
- keypoints = cv2.KeyPoint(patches[x][1][1].pt,size=9, angele=-1, response=0, octave=0, class_id=-1)
147
+ keypoints = cv2.KeyPoint(patches[x][1][0],patches[x][0][1],size=9, angele=-1, response=0, octave=0, class_id=-1)
148
-
149
- #keypoints = [cv2.KeyPoint(patches[x][1][1], 1) for x in range(9604)]
150
148
 
151
149
  descriptors = detector.compute(patches[x], keypoints)
152
150
 

1

情報の詳細化

2017/08/16 09:04

投稿

bof
bof

スコア18

test CHANGED
File without changes
test CHANGED
@@ -27,3 +27,277 @@
27
27
  patch[x].dtypeはuint8なのですが、descriptorsがNonetypeになってしまいます。
28
28
 
29
29
  どのように修正すればよいのか教えていただければ幸いです。
30
+
31
+
32
+
33
+ ```ここに言語を入力
34
+
35
+ # -*- coding: utf-8 -*-
36
+
37
+ import os
38
+
39
+ import sys
40
+
41
+ import cv2
42
+
43
+ import numpy as np
44
+
45
+ from sklearn.feature_extraction import image
46
+
47
+
48
+
49
+ ## 画像データのクラスIDとパスを取得
50
+
51
+ #
52
+
53
+ # @param dir_path 検索ディレクトリ
54
+
55
+ # @return data_sets [クラスID, 画像データのパス]のリスト
56
+
57
+ def getDataSet(dir_path):
58
+
59
+ data_sets = []
60
+
61
+
62
+
63
+ sub_dirs = os.listdir(dir_path)
64
+
65
+ for classId in sub_dirs:
66
+
67
+ sub_dir_path = dir_path + '/' + classId
68
+
69
+ img_files = os.listdir(sub_dir_path)
70
+
71
+ for f in img_files:
72
+
73
+ data_sets.append([classId, sub_dir_path + '/' + f])
74
+
75
+
76
+
77
+ return data_sets
78
+
79
+
80
+
81
+ """
82
+
83
+ main
84
+
85
+ """
86
+
87
+ # 定数定義
88
+
89
+ GRAYSCALE = 0
90
+
91
+ # KAZE特徴量抽出器
92
+
93
+ detector = cv2.xfeatures2d.SIFT_create()
94
+
95
+
96
+
97
+ """
98
+
99
+ train
100
+
101
+ """
102
+
103
+ print("train start")
104
+
105
+ # 訓練データのパスを取得
106
+
107
+ train_set = getDataSet('train_img')
108
+
109
+ # 辞書サイズ
110
+
111
+ dictionarySize = 9
112
+
113
+ # Bag Of Visual Words分類器
114
+
115
+ bowTrainer = cv2.BOWKMeansTrainer(dictionarySize)
116
+
117
+ x=0
118
+
119
+ # 各画像を分析
120
+
121
+ for i, (classId, data_path) in enumerate(train_set):
122
+
123
+ # 進捗表示
124
+
125
+ sys.stdout.write(".")
126
+
127
+ # カラーで画像読み込み
128
+
129
+ color = cv2.imread(data_path, cv2.IMREAD_COLOR)
130
+
131
+ size = (100,100)
132
+
133
+ colora = cv2.resize(color,size)
134
+
135
+ patches = image.extract_patches_2d(colora, (3, 3))
136
+
137
+ patches = patches.astype(np.uint8)
138
+
139
+ #print(color.shape, patches.shape, patches.dtype)
140
+
141
+ # 特徴点とその特徴を計算
142
+
143
+ while x<9604:
144
+
145
+ #keypoints, descriptors= detector.detectAndCompute(patches, None)
146
+
147
+ keypoints = cv2.KeyPoint(patches[x][1][1].pt,size=9, angele=-1, response=0, octave=0, class_id=-1)
148
+
149
+ #keypoints = [cv2.KeyPoint(patches[x][1][1], 1) for x in range(9604)]
150
+
151
+ descriptors = detector.compute(patches[x], keypoints)
152
+
153
+ #print(patches[x].dtype, keypoints)
154
+
155
+ x=x+1
156
+
157
+ #descriptors = detector.compute(patches, keypoints)
158
+
159
+ # intからfloat32に変換
160
+
161
+ descriptors = descriptors.astype(np.float32)
162
+
163
+ # 特徴ベクトルをBag Of Visual Words分類器にセット
164
+
165
+ bowTrainer.add(descriptors)
166
+
167
+
168
+
169
+ # Bag Of Visual Words分類器で特徴ベクトルを分類
170
+
171
+ codebook = bowTrainer.cluster()
172
+
173
+ # 訓練完了
174
+
175
+ print("train finish")
176
+
177
+
178
+
179
+ """
180
+
181
+ test
182
+
183
+ """
184
+
185
+ print("test start")
186
+
187
+ # テストデータのパス取得
188
+
189
+ test_set = getDataSet("test_img")
190
+
191
+
192
+
193
+ # KNNを使って総当たりでマッチング
194
+
195
+ matcher = cv2.BFMatcher()
196
+
197
+
198
+
199
+ # Bag Of Visual Words抽出器
200
+
201
+ bowExtractor = cv2.BOWImgDescriptorExtractor(detector, matcher)
202
+
203
+ # トレーニング結果をセット
204
+
205
+ bowExtractor.setVocabulary(codebook)
206
+
207
+
208
+
209
+ success = 0
210
+
211
+ fail = 0
212
+
213
+
214
+
215
+ # 正しく学習できたか検証する
216
+
217
+ for i, (classId, data_path) in enumerate(test_set):
218
+
219
+ # グレースケールで読み込み
220
+
221
+ gray = cv2.imread(data_path, cv2.IMREAD_COLOR)
222
+
223
+ # 特徴点と特徴ベクトルを計算
224
+
225
+ print(gray.dtype)
226
+
227
+ size = (100,100)
228
+
229
+ graya = cv2.resize(gray,size)
230
+
231
+ patches = image.extract_patches_2d(graya, (3, 3))
232
+
233
+ print(patches.shape)
234
+
235
+ while x<9604:
236
+
237
+ keypoints, descriptors= detector.detectAndCompute(patches[x], None)
238
+
239
+ # intからfloat32に変換 特徴量
240
+
241
+ descriptors = descriptors.astype(np.float32)
242
+
243
+ # Bag Of Visual Wordsの計算 ヒストグラム
244
+
245
+ bowDescriptors = bowExtractor.compute(patches[x], keypoints)
246
+
247
+
248
+
249
+ # 結果表示
250
+
251
+ className = {"0": "airplane",
252
+
253
+ "1": "ferry",
254
+
255
+ "2": "laptop"}
256
+
257
+
258
+
259
+ actual = "???"
260
+
261
+ if bowDescriptors[0][0] > bowDescriptors[0][1] and bowDescriptors[0][0] > bowDescriptors[0][2]:
262
+
263
+ actual = className["0"]
264
+
265
+ elif bowDescriptors[0][0] < bowDescriptors[0][1] and bowDescriptors[0][2] < bowDescriptors[0][1]:
266
+
267
+ actual = className["1"]
268
+
269
+ else:
270
+
271
+ actual = className["2"]
272
+
273
+
274
+
275
+
276
+
277
+ result = ""
278
+
279
+ if actual == "???":
280
+
281
+ result = " => unknown."
282
+
283
+ elif className[classId] == actual:
284
+
285
+ result = " => success!!"
286
+
287
+ success = success + 1
288
+
289
+ else:
290
+
291
+ result = " => fail"
292
+
293
+ fail = fail + 1
294
+
295
+
296
+
297
+ print("expected: ", className[classId], ", actual: ", actual, result)
298
+
299
+
300
+
301
+ print("suceess percentage:", success/(success+fail))
302
+
303
+ ```