標籤:

Python計算機視覺第八章

8.1.2 提取圖像地稠密sift特徵(HOG)

dsift代碼

import siftfrom PIL import Imageimport numpy as npimport osdef process_image_dsift(imagename,resultname,size=20,steps =10,force_orientation = False,resize=None): im = Image.open(imagename).convert(L) if resize!=None: im = im.resize(resize) m,n = im.size if imagename[-3:]!=pgm: im.save(tmp.pgm) imagename = tmp.pgm scale = size/3.0 x,y = np.meshgrid(range(steps,m,steps),range(steps,n,steps)) xx,yy = x.flatten(),y.flatten() frame = np.array([xx,yy,scale*np.ones(xx.shape[0]),np.zeros(xx.shape[0])]) np.savetxt(tmp.frame,frame.T,fmt=%03.3f) if force_orientation: cmdd = str(sift +imagename+" --output="+resultname+ --read-frames=tmp.frame --orientations) else: cmdd = str(sift + imagename + " --output=" + resultname + --read-frames=tmp.frame) os.system(cmdd)def process_image_dsift_2(imagename,resultname,size=20,steps =10,force_orientation = False,resize=None): im = Image.open(imagename).convert(L) im = Image.open(imagename).convert(L) if resize != None: im = im.resize(resize) m, n = im.size if imagename[-3:] != pgm: im.save(tmp.pgm) imagename = tmp.pgm scale = size / 3.0 #numpy的meshgrid函數能夠返回數據的矢量化,可根據傳入的兩個一維數組參數生成兩個數組元 #素的列表。如果第一個參數是xarray,維度是xdimesion,第二個參數是yarray, #維度是ydimesion。那麼生成的第一個二維數組是以xarray為行,ydimesion行的向量; #而第二個二維數組是以yarray的轉置為列,xdimesion列的向量。 x, y = np.meshgrid(range(steps, m, steps), range(steps, n, steps)) xx, yy = x.flatten(), y.flatten() frame = np.array([xx, yy, scale * np.ones(xx.shape[0]), np.zeros(xx.shape[0])]) np.savetxt(tmp.frame, frame.T, fmt=%03.3f) #sift tmp.pgm --output=empire.sift --read-frames=tmp.frame --orientations

顯示代碼

import numpy as npfrom PIL import Imageimport pylab as plimport dsiftimport siftdsift.process_image_dsift_2(../data/empire.jpg,empire.sift,90,40,True)l,d = sift.read_features_from_file(empire.sift)im = np.array(Image.open(../data/empire.jpg))sift.plot_features(im,l,True)pl.show()

推薦閱讀:

【論文筆記】Harmonious Attention Network for Person Re-Identification
【小林的OpenCV基礎課 10】Canny邊緣檢測
相關濾波之基礎框架——MOSSE
【小林的OpenCV基礎課 15】剪刀手/分水嶺分割法
Model for Video Understanding【1】

TAG:計算機視覺 |