質問編集履歴
2
修正です
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
現在「実践コンピュータービジョン」を読んでいるのですが2.2.3特徴点を検出するというところでSIFT特徴点を
|
1
|
+
現在「実践コンピュータービジョン」を読んでいるのですが2.2.3特徴点を検出するというところでSIFT特徴点を検出する所において
|
2
2
|
|
3
3
|
```2.2.3 sift
|
4
4
|
|
1
関数部分のプログラムの全体部分を載せました、確認頂けましたらよろしくお願いします
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,12 +1,6 @@
|
|
1
1
|
現在「実践コンピュータービジョン」を読んでいるのですが2.2.3特徴点を検出するというところでSIFT特徴点をけんしゅつする所において
|
2
2
|
|
3
|
-
```
|
3
|
+
```2.2.3 sift
|
4
|
-
|
5
|
-
#!/usr/bin/python
|
6
|
-
|
7
|
-
# -*- coding: utf-8 -*-
|
8
|
-
|
9
|
-
|
10
4
|
|
11
5
|
from PIL import Image
|
12
6
|
|
@@ -44,7 +38,7 @@
|
|
44
38
|
|
45
39
|
l1,d1 = sift.read_features_from_file('empire.sift')の関数部分は
|
46
40
|
|
47
|
-
```
|
41
|
+
```sift
|
48
42
|
|
49
43
|
from PIL import Image
|
50
44
|
|
@@ -56,6 +50,32 @@
|
|
56
50
|
|
57
51
|
|
58
52
|
|
53
|
+
def process_image(imagename,resultname,params="--edge-thresh 10 --peak-thresh 5"):
|
54
|
+
|
55
|
+
""" 画像を処理してファイルに結果を保存する """
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
if imagename[-3:] != 'pgm':
|
60
|
+
|
61
|
+
# pgmファイルを作成する
|
62
|
+
|
63
|
+
im = Image.open(imagename).convert('L')
|
64
|
+
|
65
|
+
im.save('tmp.pgm')
|
66
|
+
|
67
|
+
imagename = 'tmp.pgm'
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
cmmd = str("sift "+imagename+" --output="+resultname+ " "+params)
|
72
|
+
|
73
|
+
os.system(cmmd)
|
74
|
+
|
75
|
+
print 'processed', imagename, 'to', resultname
|
76
|
+
|
77
|
+
|
78
|
+
|
59
79
|
def read_features_from_file(filename):
|
60
80
|
|
61
81
|
""" 特徴量を読み込んで行列形式で返す """
|
@@ -64,7 +84,197 @@
|
|
64
84
|
|
65
85
|
f = loadtxt(filename)
|
66
86
|
|
67
|
-
return f[:,:4],f[:,4:] #
|
87
|
+
return f[:,:4],f[:,4:] #特徴点の配置と記述子
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
def write_features_to_file(filename,locs,desc):
|
92
|
+
|
93
|
+
""" 特徴点の配置と記述子をファイルに保存する """
|
94
|
+
|
95
|
+
savetxt(filename,hstack((locs,desc)))
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
def plot_features(im,locs,circle=False):
|
100
|
+
|
101
|
+
""" 画像を特徴量とともに描画する。
|
102
|
+
|
103
|
+
入力:im(配列形式の画像)、locs(各特徴量の座標とスケール、方向)"""
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
def draw_circle(c,r):
|
108
|
+
|
109
|
+
t = arange(0,1.01,.01)*2*pi
|
110
|
+
|
111
|
+
x = r*cos(t) + c[0]
|
112
|
+
|
113
|
+
y = r*sin(t) + c[1]
|
114
|
+
|
115
|
+
plot(x,y,'b',linewidth=2)
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
imshow(im)
|
120
|
+
|
121
|
+
if circle:
|
122
|
+
|
123
|
+
for p in locs:
|
124
|
+
|
125
|
+
draw_circle(p[:2],p[2])
|
126
|
+
|
127
|
+
else:
|
128
|
+
|
129
|
+
plot(locs[:,0],locs[:,1],'ob')
|
130
|
+
|
131
|
+
axis('off')
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
def match(desc1,desc2):
|
136
|
+
|
137
|
+
""" 第1の画像の各記述子について、第2の画像の対応点を求める。
|
138
|
+
|
139
|
+
入力:desc1(第1の画像の記述子)、desc2(第2の画像の記述子)"""
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
desc1 = array([d/linalg.norm(d) for d in desc1])
|
144
|
+
|
145
|
+
desc2 = array([d/linalg.norm(d) for d in desc2])
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
dist_ratio = 0.6
|
150
|
+
|
151
|
+
desc1_size = desc1.shape
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
matchscores = zeros(desc1_size[0],'int')
|
156
|
+
|
157
|
+
desc2t = desc2.T # あらかじめ転置行列を計算しておく
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
for i in range(desc1_size[0]):
|
162
|
+
|
163
|
+
dotprods = dot(desc1[i,:],desc2t) # 内積ベクトル
|
164
|
+
|
165
|
+
dotprods = 0.9999*dotprods
|
166
|
+
|
167
|
+
# 第2の画像の特徴点の逆余弦を求め、ソートし、番号を返す
|
168
|
+
|
169
|
+
indx = argsort(arccos(dotprods))
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
# 最も近い近接点との角度が、2番目に近いもののdist_rasio倍以下か?
|
174
|
+
|
175
|
+
if arccos(dotprods)[indx[0]] < dist_ratio * arccos(dotprods)[indx[1]]:
|
176
|
+
|
177
|
+
matchscores[i] = int(indx[0])
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
return matchscores
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
def match_twosided(desc1,desc2):
|
186
|
+
|
187
|
+
""" 双方向対称バージョンのmatch() """
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
matches_12 = match(desc1,desc2)
|
192
|
+
|
193
|
+
matches_21 = match(desc2,desc1)
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
ndx_12 = matches_12.nonzero()[0]
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
# 対称でないものは除去する
|
202
|
+
|
203
|
+
for n in ndx_12:
|
204
|
+
|
205
|
+
if matches_21[int(matches_12[n])] != n:
|
206
|
+
|
207
|
+
matches_12[n] = 0
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
return matches_12
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
def appendimages(im1,im2):
|
216
|
+
|
217
|
+
""" 2つの画像を左右に並べた画像を返す """
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
# 行の少ない方を選び、空行を0で埋める
|
222
|
+
|
223
|
+
rows1 = im1.shape[0]
|
224
|
+
|
225
|
+
rows2 = im2.shape[0]
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
if rows1 < rows2:
|
230
|
+
|
231
|
+
im1 = concatenate((im1,zeros((rows2-rows1,im1.shape[1]))),axis=0)
|
232
|
+
|
233
|
+
elif rows1 > rows2:
|
234
|
+
|
235
|
+
im2 = concatenate((im2,zeros((rows1-rows2,im2.shape[1]))),axis=0)
|
236
|
+
|
237
|
+
# 行が同じなら、0で埋める必要はない
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
return concatenate((im1,im2), axis=1)
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
def plot_matches(im1,im2,locs1,locs2,matchscores,show_below=True):
|
248
|
+
|
249
|
+
""" 対応点を線で結んで画像を表示する
|
250
|
+
|
251
|
+
入力: im1,im2(配列形式の画像)、locs1,locs2(特徴点座標)
|
252
|
+
|
253
|
+
machescores(match()の出力)、
|
254
|
+
|
255
|
+
show_below(対応の下に画像を表示するならTrue)"""
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
im3 = appendimages(im1,im2)
|
260
|
+
|
261
|
+
if show_below:
|
262
|
+
|
263
|
+
im3 = vstack((im3,im3))
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
imshow(im3)
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
cols1 = im1.shape[1]
|
272
|
+
|
273
|
+
for i,m in enumerate(matchscores):
|
274
|
+
|
275
|
+
if m>0: plot([locs1[i][0],locs2[m][0]+cols1],[locs1[i][1],locs2[m][1]],'c')
|
276
|
+
|
277
|
+
axis('off')
|
68
278
|
|
69
279
|
```
|
70
280
|
|
@@ -74,7 +284,9 @@
|
|
74
284
|
|
75
285
|
このプログラムを実行したところ
|
76
286
|
|
287
|
+
|
288
|
+
|
77
|
-
```
|
289
|
+
```
|
78
290
|
|
79
291
|
runfile('C:/Users/suuri/.spyder/image/before/chap2/2.2.3.sift.py', wdir='C:/Users/suuri/.spyder/image/before/chap2')
|
80
292
|
|
@@ -124,6 +336,8 @@
|
|
124
336
|
|
125
337
|
となり上手く動きません、数値を変えてみましても上手く行かない状況です。
|
126
338
|
|
339
|
+
return f[:,:4],f[:,4:] #特徴点の配置と記述子の所にエラーがあります、上手くpgmファイルも作成されておりません
|
340
|
+
|
127
341
|
|
128
342
|
|
129
343
|
当方実行環境はWin10、python2.7、spyderを使用中です。
|