質問編集履歴
3
情報の詳細化
title
CHANGED
File without changes
|
body
CHANGED
@@ -148,4 +148,5 @@
|
|
148
148
|
print("expected: ", className[classId], ", actual: ", actual, result)
|
149
149
|
|
150
150
|
print("suceess percentage:", success/(success+fail))
|
151
|
-
```
|
151
|
+
```
|
152
|
+
取り込んだ画像をpatchにしたものから特徴点(keypoint)を抽出する段階で、特徴点をpatchの中心に指定したいのですが,そこがうまくいってません。
|
2
情報の詳細化
title
CHANGED
File without changes
|
body
CHANGED
@@ -71,8 +71,7 @@
|
|
71
71
|
# 特徴点とその特徴を計算
|
72
72
|
while x<9604:
|
73
73
|
#keypoints, descriptors= detector.detectAndCompute(patches, None)
|
74
|
-
keypoints = cv2.KeyPoint(patches[x][1][1]
|
74
|
+
keypoints = cv2.KeyPoint(patches[x][1][0],patches[x][0][1],size=9, angele=-1, response=0, octave=0, class_id=-1)
|
75
|
-
#keypoints = [cv2.KeyPoint(patches[x][1][1], 1) for x in range(9604)]
|
76
75
|
descriptors = detector.compute(patches[x], keypoints)
|
77
76
|
#print(patches[x].dtype, keypoints)
|
78
77
|
x=x+1
|
1
情報の詳細化
title
CHANGED
File without changes
|
body
CHANGED
@@ -12,4 +12,141 @@
|
|
12
12
|
keypoints, descriptors= detector.detectAndCompute(patches[x], None)
|
13
13
|
```
|
14
14
|
patch[x].dtypeはuint8なのですが、descriptorsがNonetypeになってしまいます。
|
15
|
-
どのように修正すればよいのか教えていただければ幸いです。
|
15
|
+
どのように修正すればよいのか教えていただければ幸いです。
|
16
|
+
|
17
|
+
```ここに言語を入力
|
18
|
+
# -*- coding: utf-8 -*-
|
19
|
+
import os
|
20
|
+
import sys
|
21
|
+
import cv2
|
22
|
+
import numpy as np
|
23
|
+
from sklearn.feature_extraction import image
|
24
|
+
|
25
|
+
## 画像データのクラスIDとパスを取得
|
26
|
+
#
|
27
|
+
# @param dir_path 検索ディレクトリ
|
28
|
+
# @return data_sets [クラスID, 画像データのパス]のリスト
|
29
|
+
def getDataSet(dir_path):
|
30
|
+
data_sets = []
|
31
|
+
|
32
|
+
sub_dirs = os.listdir(dir_path)
|
33
|
+
for classId in sub_dirs:
|
34
|
+
sub_dir_path = dir_path + '/' + classId
|
35
|
+
img_files = os.listdir(sub_dir_path)
|
36
|
+
for f in img_files:
|
37
|
+
data_sets.append([classId, sub_dir_path + '/' + f])
|
38
|
+
|
39
|
+
return data_sets
|
40
|
+
|
41
|
+
"""
|
42
|
+
main
|
43
|
+
"""
|
44
|
+
# 定数定義
|
45
|
+
GRAYSCALE = 0
|
46
|
+
# KAZE特徴量抽出器
|
47
|
+
detector = cv2.xfeatures2d.SIFT_create()
|
48
|
+
|
49
|
+
"""
|
50
|
+
train
|
51
|
+
"""
|
52
|
+
print("train start")
|
53
|
+
# 訓練データのパスを取得
|
54
|
+
train_set = getDataSet('train_img')
|
55
|
+
# 辞書サイズ
|
56
|
+
dictionarySize = 9
|
57
|
+
# Bag Of Visual Words分類器
|
58
|
+
bowTrainer = cv2.BOWKMeansTrainer(dictionarySize)
|
59
|
+
x=0
|
60
|
+
# 各画像を分析
|
61
|
+
for i, (classId, data_path) in enumerate(train_set):
|
62
|
+
# 進捗表示
|
63
|
+
sys.stdout.write(".")
|
64
|
+
# カラーで画像読み込み
|
65
|
+
color = cv2.imread(data_path, cv2.IMREAD_COLOR)
|
66
|
+
size = (100,100)
|
67
|
+
colora = cv2.resize(color,size)
|
68
|
+
patches = image.extract_patches_2d(colora, (3, 3))
|
69
|
+
patches = patches.astype(np.uint8)
|
70
|
+
#print(color.shape, patches.shape, patches.dtype)
|
71
|
+
# 特徴点とその特徴を計算
|
72
|
+
while x<9604:
|
73
|
+
#keypoints, descriptors= detector.detectAndCompute(patches, None)
|
74
|
+
keypoints = cv2.KeyPoint(patches[x][1][1].pt,size=9, angele=-1, response=0, octave=0, class_id=-1)
|
75
|
+
#keypoints = [cv2.KeyPoint(patches[x][1][1], 1) for x in range(9604)]
|
76
|
+
descriptors = detector.compute(patches[x], keypoints)
|
77
|
+
#print(patches[x].dtype, keypoints)
|
78
|
+
x=x+1
|
79
|
+
#descriptors = detector.compute(patches, keypoints)
|
80
|
+
# intからfloat32に変換
|
81
|
+
descriptors = descriptors.astype(np.float32)
|
82
|
+
# 特徴ベクトルをBag Of Visual Words分類器にセット
|
83
|
+
bowTrainer.add(descriptors)
|
84
|
+
|
85
|
+
# Bag Of Visual Words分類器で特徴ベクトルを分類
|
86
|
+
codebook = bowTrainer.cluster()
|
87
|
+
# 訓練完了
|
88
|
+
print("train finish")
|
89
|
+
|
90
|
+
"""
|
91
|
+
test
|
92
|
+
"""
|
93
|
+
print("test start")
|
94
|
+
# テストデータのパス取得
|
95
|
+
test_set = getDataSet("test_img")
|
96
|
+
|
97
|
+
# KNNを使って総当たりでマッチング
|
98
|
+
matcher = cv2.BFMatcher()
|
99
|
+
|
100
|
+
# Bag Of Visual Words抽出器
|
101
|
+
bowExtractor = cv2.BOWImgDescriptorExtractor(detector, matcher)
|
102
|
+
# トレーニング結果をセット
|
103
|
+
bowExtractor.setVocabulary(codebook)
|
104
|
+
|
105
|
+
success = 0
|
106
|
+
fail = 0
|
107
|
+
|
108
|
+
# 正しく学習できたか検証する
|
109
|
+
for i, (classId, data_path) in enumerate(test_set):
|
110
|
+
# グレースケールで読み込み
|
111
|
+
gray = cv2.imread(data_path, cv2.IMREAD_COLOR)
|
112
|
+
# 特徴点と特徴ベクトルを計算
|
113
|
+
print(gray.dtype)
|
114
|
+
size = (100,100)
|
115
|
+
graya = cv2.resize(gray,size)
|
116
|
+
patches = image.extract_patches_2d(graya, (3, 3))
|
117
|
+
print(patches.shape)
|
118
|
+
while x<9604:
|
119
|
+
keypoints, descriptors= detector.detectAndCompute(patches[x], None)
|
120
|
+
# intからfloat32に変換 特徴量
|
121
|
+
descriptors = descriptors.astype(np.float32)
|
122
|
+
# Bag Of Visual Wordsの計算 ヒストグラム
|
123
|
+
bowDescriptors = bowExtractor.compute(patches[x], keypoints)
|
124
|
+
|
125
|
+
# 結果表示
|
126
|
+
className = {"0": "airplane",
|
127
|
+
"1": "ferry",
|
128
|
+
"2": "laptop"}
|
129
|
+
|
130
|
+
actual = "???"
|
131
|
+
if bowDescriptors[0][0] > bowDescriptors[0][1] and bowDescriptors[0][0] > bowDescriptors[0][2]:
|
132
|
+
actual = className["0"]
|
133
|
+
elif bowDescriptors[0][0] < bowDescriptors[0][1] and bowDescriptors[0][2] < bowDescriptors[0][1]:
|
134
|
+
actual = className["1"]
|
135
|
+
else:
|
136
|
+
actual = className["2"]
|
137
|
+
|
138
|
+
|
139
|
+
result = ""
|
140
|
+
if actual == "???":
|
141
|
+
result = " => unknown."
|
142
|
+
elif className[classId] == actual:
|
143
|
+
result = " => success!!"
|
144
|
+
success = success + 1
|
145
|
+
else:
|
146
|
+
result = " => fail"
|
147
|
+
fail = fail + 1
|
148
|
+
|
149
|
+
print("expected: ", className[classId], ", actual: ", actual, result)
|
150
|
+
|
151
|
+
print("suceess percentage:", success/(success+fail))
|
152
|
+
```
|