-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCV404Harris.py
More file actions
50 lines (43 loc) · 1.51 KB
/
CV404Harris.py
File metadata and controls
50 lines (43 loc) · 1.51 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
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import CV404Filters as myFilters
def harris_response(img, w=3,k= 0.05, gaussian_size =3):
'''
w : window size
'''
img_gray = myFilters.rgb2gray(img)
img_gray = myFilters.img_gaussian_filter(img_gray,gaussian_size)
Ix = myFilters.sobel_h(img_gray)
Iy = myFilters.sobel_v(img_gray)
Ixx = np.multiply(Ix,Ix)
Iyy = np.multiply(Iy,Iy)
Ixy = np.multiply(Ix,Iy)
Ixx_hat = myFilters.img_gaussian_filter(Ixx,w)
Iyy_hat = myFilters.img_gaussian_filter(Iyy,w)
Ixy_hat = myFilters.img_gaussian_filter(Ixy,w)
detM = np.multiply(Ixx_hat,Iyy_hat) - np.multiply(Ixy_hat,Ixy_hat)
traceM = Ixx_hat + Iyy_hat
R = detM - k * np.multiply(traceM,traceM) # Harris response
return R
def get_corners(img,w=3, k=0.05,thershold = 0.0001, gaussian_size=3):
R = harris_response(img,w,k,gaussian_size)
R/=np.max(R)
img_copy = np.copy(img)
corners = R > thershold
# imgplot = plt.imshow(corners ,cmap=plt.get_cmap('gray'))
# plt.show()
if(len(img.shape) == 2):
img_copy [corners] = 1
elif(len(img.shape)==3):
if(img.shape[2]==3):
img_copy [corners] = [255,0,0]
elif(img.shape[2]==4):
img_copy [corners] = [255,0,0,255]
return img_copy
# img_c = mpimg.imread('images\chess_board.jpg')
# # img_c = mpimg.imread('images\\shapes.jpg')
# out = get_corners(img_c)
# imgplot = plt.imshow(out,cmap=plt.get_cmap('gray'))
# plt.show()