-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathharris_corner.py
More file actions
119 lines (99 loc) · 3.28 KB
/
harris_corner.py
File metadata and controls
119 lines (99 loc) · 3.28 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from scipy.ndimage import filters
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as col
from scipy import signal
def harrisCorner(image):
'''
Compute Harris corenr using hessian matrix of the image
input : image
output : Harris operator.
'''
# Sobel operator is an approximation of first order derivative
# x derivative
sobelx = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
# y derivative
sobely = np.array([[-1, -2, -1],
[0, 0, 0],
[1, 2, 1]])
# Get Ixx
# To get second derivative differentiate twice.
Ixx = signal.convolve2d(signal.convolve2d(image, sobelx), sobelx)
# Iyy
Iyy = signal.convolve2d(signal.convolve2d(image, sobely), sobely)
# Ixy Image
Ixy = signal.convolve2d(signal.convolve2d(image, sobelx), sobely)
# Hessian Matrix is [Ixx Ixy
# Ixy Iyy]
# Lets show them
plt.figure("Original Image")
plt.imshow(image)
plt.figure("Ixx")
plt.imshow(Ixx)
plt.set_cmap("gray")
plt.figure("Iyy")
plt.imshow(Iyy)
plt.figure("Ixy")
plt.imshow(Ixy)
# Get Determinnate and trace
det = Ixx*Iyy - Ixy**2
trace = Ixx + Iyy
# Harris is det(H) - a * trace(H) let a = 0.2
H = det - 0.2 * trace
# lets show Harris matrix
plt.figure("Harris Operator")
plt.imshow(np.abs(H))
plt.show()
# Return harris matrix
return H
def compute_harris_response(im, sigma=3):
'''
Corner Detector by Harris and Stephens, instead of getting second order
derivative. Blure the image using gaussian and then get the first order
derivative
'''
# derivatives
imx = np.zeros(im.shape)
filters.gaussian_filter(im, (sigma, sigma), (0, 1), imx)
imy = np.zeros(im.shape)
filters.gaussian_filter(im, (sigma, sigma), (1, 0), imy)
# compute components of the Harris matrix
Wxx = filters.gaussian_filter(imx**2, sigma)
Wxy = filters.gaussian_filter(imx*imy, sigma)
Wyy = filters.gaussian_filter(imy**2, sigma)
# determinant and trace
Wdet = Wxx*Wyy - Wxy**2
Wtr = Wxx + Wyy
return Wdet - 0.2 * Wtr
def getHarrisPoints(harrisImage, threshold=0.1):
''' Extract corners > threshold where threshold is a fraction of maximum
corner value.
inputs: harrisImage , threshold
Output : selected poits coordinates
'''
# Find corners > threshold
selectedCorners = (harrisImage > threshold * harrisImage.max()) * 1
# get coordinates of selected corners
coords = np.array(selectedCorners.nonzero()).T
return coords
def plotHarrisPoints(image, filtered_coords):
""" Plots corners found in image. """
plt.figure()
plt.set_cmap('gray')
plt.imshow(image)
plt.plot([p[1] for p in filtered_coords], [p[0]
for p in filtered_coords], '+', color='red')
plt.axis('off')
plt.show()
if __name__ == '__main__':
# Load Image
image = plt.imread('images/squares.jpg')
hsv_image = col.rgb_to_hsv(image)
# Working on value channel
vIm = hsv_image[..., 2]
# Get Harris using gaussian
Hg = compute_harris_response(vIm)
coord = getHarrisPoints(Hg, 0.95)
plotHarrisPoints(image, coord)