forked from colinmccormick/CVwithPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harris.py
69 lines (54 loc) · 1.93 KB
/
harris.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# harris.py
from PIL import Image
from numpy import *
import matplotlib.pyplot as pyplot
from scipy.ndimage import filters
from scipy.ndimage import measurements,morphology
import imtools
def compute_harris_response(im,sigma=3):
"""Compute Harris corner detector response"""
# derivatives
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)
# components
Wxx = filters.gaussian_filter(imx*imx,sigma)
Wyy = filters.gaussian_filter(imy*imy,sigma)
Wxy = filters.gaussian_filter(imx*imy,sigma)
# determinant and trace; include small epsilon to avoid Wtr = 0
Wdet = Wxx*Wyy - Wxy**2
Wtr = Wxx + Wyy + 0.001
if Wtr.min() == 0:
print 'Divide by zero'
return Wdet / Wtr
def get_harris_points(harrisim,min_dist=10,threshold=0.1):
"""Identify and sort candidate corner points;
filter out ones that are too close"""
# find top corner candidates above a threshold
corner_threshold = harrisim.max() * threshold
harrisim_t = (harrisim > corner_threshold) * 1
# get coords, vals of candidates; sort
ccoords = array(harrisim_t.nonzero()).T
cvals = [harrisim[c[0],c[1]] for c in ccoords]
index = argsort(cvals)
# store allowed points in an array (away from edge)
allowed_locations = zeros(harrisim.shape)
allowed_locations[min_dist:-min_dist,min_dist:-min_dist] = 1
# identify best corner candidates, separated by min_dist
filtered_candidates = []
for i in index:
cx = ccoords[i,0]
cy = ccoords[i,1]
if allowed_locations[cx,cy] == 1:
filtered_candidates.append(ccoords[i])
allowed_locations[(cx-min_dist):(cx+min_dist),(cy-min_dist):(cy+min_dist)] = 0
return filtered_candidates
def plot_harris_points(im,filtered_candidates):
"""Plot corners found in image"""
pyplot.figure()
pyplot.gray()
pyplot.imshow(im)
pyplot.plot([p[1] for p in filtered_candidates],[p[0] for p in filtered_candidates],'r*')
pyplot.axis('off')
pyplot.show()