1.python提取Harris角点
其实有在网上参考部分代码,但是,有些博客的博主可能连代码都没跑过,直接网上找资源就粘贴在自己的博客下了,然而有些代码是会报错的
第一行就报错:SyntaxError: invalid syntax
应该是
"<pre name = "code" class = "python">"
这一行代码出现的问题。但是我查阅了好多博客,解释居然都一模一样,直接这么复制黏贴真的好嘛!我最后也没解决这个问题,但是我发现这一行并不需要,注释掉功能仍可以正常使用。如果有小伙伴能解释一下,那我是完全欢迎的!
这里一共有两个.py文件,第一个命名为harris.py,相当于自己定义了一个module,第二个看你喜好O(∩_∩)O
# <pre name = "code" class = "python" >
#coding:utf-8
from scipy.ndimage import filters
from numpy import *
from pylab import *
def compute_harris_response(im,sigma=3):
imx = zeros(im.shape)
#计算导数
filters.gaussian_filter(im,(sigma,sigma),(0,1),imx)
imy = zeros(im.shape)
filters.gaussian_filter(im,(sigma,sigma),(1,0),imy)
Wxx = filters.gaussian_filter(imx*imx,sigma)
#计算harris矩阵分量
Wxy = filters.gaussian_filter(imx*imy,sigma)
Wyy = filters.gaussian_filter(imy*imy,sigma)
#计算矩阵的特征值和迹
Wtr = Wxx+Wyy
return Wdet/Wtr
def get_harris_points(harrisim, min_dist = 10, threshold = 0.1):
conner_threshold = harrisim.max()*threshold
harrisim_t = (harrisim>conner_threshold)*1
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
#此处保证min_dist*min_dist只有一个harris特征点
return filtered_coords
def 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()
#coding:utf-8
from scipy.ndimage import filters
from numpy import *
from PIL import Image
import harris
from pylab import *
for filename in os.listdir(r"/path to your image"):
print(filename)
im = array(Image.open("/path to your image" + filename).convert('L'))
harrisim = harris.compute_harris_response(im)
filtered_coords = harris.get_harris_points(harrisim, 6)
harris.plot_harris_points(im, filtered_coords)