質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

Q&A

解決済

1回答

992閲覧

書籍「実践コンピュータービジョン」のプログラムにおける不明点

naberyo

総合スコア10

Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

0グッド

1クリップ

投稿2019/11/05 01:08

編集2019/11/05 06:07

現在「実践コンピュータービジョン」を読んでいるのですが2.2.3特徴点を検出するというところでSIFT特徴点を検出する所において

2.2.3

1from PIL import Image 2from numpy import * 3from pylab import * 4import sift 5 6imname = 'empire.jpg' 7im1 = array(Image.open(imname).convert('L')) 8sift.process_image(imname,'empire.sift') 9l1,d1 = sift.read_features_from_file('empire.sift') 10 11figure() 12gray() 13sift.plot_features(im1,l1,circle=True) 14show() 15

というプログラムで
l1,d1 = sift.read_features_from_file('empire.sift')の関数部分は

sift

1from PIL import Image 2from numpy import * 3from pylab import * 4import os 5 6def process_image(imagename,resultname,params="--edge-thresh 10 --peak-thresh 5"): 7 """ 画像を処理してファイルに結果を保存する """ 8 9 if imagename[-3:] != 'pgm': 10 # pgmファイルを作成する 11 im = Image.open(imagename).convert('L') 12 im.save('tmp.pgm') 13 imagename = 'tmp.pgm' 14 15 cmmd = str("sift "+imagename+" --output="+resultname+ " "+params) 16 os.system(cmmd) 17 print 'processed', imagename, 'to', resultname 18 19def read_features_from_file(filename): 20 """ 特徴量を読み込んで行列形式で返す """ 21 22 f = loadtxt(filename) 23 return f[:,:4],f[:,4:] #特徴点の配置と記述子 24 25def write_features_to_file(filename,locs,desc): 26 """ 特徴点の配置と記述子をファイルに保存する """ 27 savetxt(filename,hstack((locs,desc))) 28 29def plot_features(im,locs,circle=False): 30 """ 画像を特徴量とともに描画する。 31 入力:im(配列形式の画像)、locs(各特徴量の座標とスケール、方向)""" 32 33 def draw_circle(c,r): 34 t = arange(0,1.01,.01)*2*pi 35 x = r*cos(t) + c[0] 36 y = r*sin(t) + c[1] 37 plot(x,y,'b',linewidth=2) 38 39 imshow(im) 40 if circle: 41 for p in locs: 42 draw_circle(p[:2],p[2]) 43 else: 44 plot(locs[:,0],locs[:,1],'ob') 45 axis('off') 46 47def match(desc1,desc2): 48 """ 第1の画像の各記述子について、第2の画像の対応点を求める。 49 入力:desc1(第1の画像の記述子)、desc2(第2の画像の記述子)""" 50 51 desc1 = array([d/linalg.norm(d) for d in desc1]) 52 desc2 = array([d/linalg.norm(d) for d in desc2]) 53 54 dist_ratio = 0.6 55 desc1_size = desc1.shape 56 57 matchscores = zeros(desc1_size[0],'int') 58 desc2t = desc2.T # あらかじめ転置行列を計算しておく 59 60 for i in range(desc1_size[0]): 61 dotprods = dot(desc1[i,:],desc2t) # 内積ベクトル 62 dotprods = 0.9999*dotprods 63 # 第2の画像の特徴点の逆余弦を求め、ソートし、番号を返す 64 indx = argsort(arccos(dotprods)) 65 66 # 最も近い近接点との角度が、2番目に近いもののdist_rasio倍以下か? 67 if arccos(dotprods)[indx[0]] < dist_ratio * arccos(dotprods)[indx[1]]: 68 matchscores[i] = int(indx[0]) 69 70 return matchscores 71 72def match_twosided(desc1,desc2): 73 """ 双方向対称バージョンのmatch() """ 74 75 matches_12 = match(desc1,desc2) 76 matches_21 = match(desc2,desc1) 77 78 ndx_12 = matches_12.nonzero()[0] 79 80 # 対称でないものは除去する 81 for n in ndx_12: 82 if matches_21[int(matches_12[n])] != n: 83 matches_12[n] = 0 84 85 return matches_12 86 87def appendimages(im1,im2): 88 """ 2つの画像を左右に並べた画像を返す """ 89 90 # 行の少ない方を選び、空行を0で埋める 91 rows1 = im1.shape[0] 92 rows2 = im2.shape[0] 93 94 if rows1 < rows2: 95 im1 = concatenate((im1,zeros((rows2-rows1,im1.shape[1]))),axis=0) 96 elif rows1 > rows2: 97 im2 = concatenate((im2,zeros((rows1-rows2,im2.shape[1]))),axis=0) 98 # 行が同じなら、0で埋める必要はない 99 100 return concatenate((im1,im2), axis=1) 101 102 103def plot_matches(im1,im2,locs1,locs2,matchscores,show_below=True): 104 """ 対応点を線で結んで画像を表示する 105 入力: im1,im2(配列形式の画像)、locs1,locs2(特徴点座標) 106 machescores(match()の出力)、 107 show_below(対応の下に画像を表示するならTrue)""" 108 109 im3 = appendimages(im1,im2) 110 if show_below: 111 im3 = vstack((im3,im3)) 112 113 imshow(im3) 114 115 cols1 = im1.shape[1] 116 for i,m in enumerate(matchscores): 117 if m>0: plot([locs1[i][0],locs2[m][0]+cols1],[locs1[i][1],locs2[m][1]],'c') 118 axis('off')

となっております

このプログラムを実行したところ

runfile('C:/Users/suuri/.spyder/image/before/chap2/2.2.3.sift.py', wdir='C:/Users/suuri/.spyder/image/before/chap2') processed tmp.pgm to empire.sift sift.py:25: UserWarning: loadtxt: Empty input file: "empire.sift" f = loadtxt(filename) Traceback (most recent call last): File "<ipython-input-24-655ed05eb36d>", line 1, in <module> runfile('C:/Users/suuri/.spyder/image/before/chap2/2.2.3.sift.py', wdir='C:/Users/suuri/.spyder/image/before/chap2') File "D:\anaconda\envs\py27\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace) File "D:\anaconda\envs\py27\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 95, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "C:/Users/suuri/.spyder/image/before/chap2/2.2.3.sift.py", line 12, in <module> l1,d1 = sift.read_features_from_file('empire.sift') File "sift.py", line 26, in read_features_from_file return f[:,:4],f[:,4:] # 特徴点の配置と記述子 IndexError: too many indices for array

となり上手く動きません、数値を変えてみましても上手く行かない状況です。
return f[:,:4],f[:,4:] #特徴点の配置と記述子の所にエラーがあります、上手くpgmファイルも作成されておりません

当方実行環境はWin10、python2.7、spyderを使用中です。
解決方法を教えて頂けたら幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Q71

2019/11/05 01:37

loadtxt関数の戻り値と、return文が一致していません。loadtxtの内容、および読み込んだCSVの内容を追記して下さい。
naberyo

2019/11/05 06:06

事前にpgmファイルを作成して読み込むそうなので、その部分も記載しました
Q71

2019/11/05 06:53

ああ、すみません。「empire.shiftが空」と書いてありますね。このファイルを確認してください。
guest

回答1

0

自己解決

VLFeatのバージョンを最新版でなく0.9.13版にしたら動いたので、相性の問題だと思います。
ありがとうございました

投稿2019/11/05 07:58

naberyo

総合スコア10

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問