Harris is most commonly used as a feature detection algorithm.
First file harris.py
<pre name= "code" class= "python" >from scipy.ndimage import filtersfrom numpy import *from pylab import *def Compute_h Arris_response (im,sigma=3): Imx=zeros (Im.shape) #计算导数 filters.gaussian_filter (IM, (SIGMA,SIGMA), (0,1), IMX) Imy=zero S (im.shape) Filters.gaussian_filter (IM, (Sigma,sigma), (1,0), Imy) Wxx=filters.gaussian_filter (imx*imx,sigma) #计算har RIS matrix component Wxy=filters.gaussian_filter (IMX*IMY,SIGMA) wyy=filters.gaussian_filter (imy*imy,sigma) Wdet=Wxx*Wyy-Wxy **2 #计算矩阵的特征值和迹 wtr=wxx+wyy return wdet/wtrdef get_harris_points (harrisim,min_dist=10,threshold=0.1): Conner_ Threshold=harrisim.max () *threshold harrisim_t= (harrisim>conner_threshold) * Coords=array (Harrisim_t.nonzero () ). T Candidate_values=[harrisim[c[0],c[1]] for C in coords] Index=argsort (candidate_values) Allowed_locations=zeros ( Harrisim.shape) Allowed_locations[min_dist:-min_dist,min_dist:-min_dist]=1 filtered_coords=[] for i in index: If allowed_locationS[coords[i,0],coords[i,1]]==1:filtered_coords.append (Coords[i]) allowed_locations[(coords[i,0]-min_ DIST):(coords[i,0]+min_dist), (coords[i,1]-min_dist):(coords[i,1]+min_dist)]=0# here to ensure min_dist*min_ Dist only has a Harris feature point return filtered_coordsdef plot_harris_points (image,filtered_coords): Figure () Gray () imshow (image) plot ([p[1] for P in Filtered_coords],[p[0]for p in filtered_coords], ' + ') axis (' Off ') show ()
A second file test algorithm
From PIL import imagefrom numpy import *import harrisfrom pylab import *from scipy.ndimage import Filtersim=array (image.op En (' 33.jpg '). Convert (' L ')) Harrisim=harris.compute_harris_response (IM) filtered_coords=harris.get_harris_points ( Harrisim) harris.plot_harris_points (im,filtered_coords)
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Harris Algorithm Python implementation