Python進行sift特徵檢測
阿新 • • 發佈:2018-12-23
# -*- coding: utf-8 -*- from PIL import Image from pylab import * from numpy import * import os def process_image(imagename, resultname, params="--edge-thresh 10 --peak-thresh 5"): """ 處理一幅影象,然後將結果儲存在檔案中""" if imagename[-3:] != 'pgm': #建立一個pgm檔案 im = Image.open(imagename).convert('L') im.save('tmp.pgm') imagename ='tmp.pgm' cmmd = str("sift "+imagename+" --output="+resultname+" "+params) os.system(cmmd) print(cmmd) print ('processed', imagename, 'to', resultname) def read_features_from_file(filename): """讀取特徵屬性值,然後將其以矩陣的形式返回""" f = loadtxt(filename) return f[:,:4], f[:,4:] #特徵位置,描述子 def write_featrues_to_file(filename, locs, desc): """將特徵位置和描述子儲存到檔案中""" savetxt(filename, hstack((locs,desc))) def plot_features(im, locs, circle=False): """顯示帶有特徵的影象 輸入:im(陣列影象),locs(每個特徵的行、列、尺度和朝向)""" def draw_circle(c,r): t = arange(0,1.01,.01)*2*pi x = r*cos(t) + c[0] y = r*sin(t) + c[1] plot(x, y, 'b', linewidth=2) imshow(im) if circle: for p in locs: draw_circle(p[:2], p[2]) else: plot(locs[:,0], locs[:,1], 'ob') axis('off') imname = 'D:\mywindows\Python Sample\hx.jpg' im1 = array(Image.open(imname).convert('L')) process_image(imname, 'img.sift') l1,d1 = read_features_from_file('img.sift') figure() gray() plot_features(im1, l1, circle=True) show()