-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCorner_Detection.py
More file actions
158 lines (112 loc) · 2.92 KB
/
Corner_Detection.py
File metadata and controls
158 lines (112 loc) · 2.92 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import cv2
import math
import random
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
import requests
from PIL import Image
# In[4]:
def load_image(name, no_alpha=True):
url = 'Image URL'
image = np.asarray(Image.open(requests.get(url, stream=True).raw))
if no_alpha and len(image) > 2 and image.shape[2] == 4:
image = image[:,:,:3]
return image[:,:,::-1].copy()
def resize(image, scale):
return cv2.resize(image, (int(image.shape[1]*scale), int(image.shape[0]*scale)))
def show(*images, titles=None, figsize=None, **kwargs):
ROWS, COLS = 1, len(images)
if figsize is not None:
plt.figure(figsize=(18,6))
for i, img in enumerate(images):
plt.subplot(ROWS, COLS, i+1)
if titles is not None:
plt.title(titles[i])
if len(img.shape) == 3:
plt.imshow(img[:,:,::-1], **kwargs)
else:
plt.imshow(img, **kwargs)
plt.show()
def gray(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
def conv(image, kernel):
return cv2.filter2D(src=image, ddepth=-1, kernel=kernel)
def gaussian_blur(image, size):
return cv2.GaussianBlur(image, (size, size), 0)
def visualize_corners(I, R, threshold=0.6):
I = I.copy()
loc = np.where(R >= threshold)
for pt in zip(*loc[::-1]):
cv2.circle(I, pt, 3, (0, 0, 255), -1)
return I
# In[6]:
SOBEL_X = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]], dtype=np.float32)
SOBEL_Y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], dtype=np.float32)
# In[7]:
I = load_image("img1.png")
I_gray = gray(I) / 255.0
def apply_sobel(I_gray):
I_x = conv(I_gray,SOBEL_X)
I_y = conv(I_gray,SOBEL_Y)
dx = I_x
dy = I_y
return dx, dy
sobel = apply_sobel(I_gray)
show(
*sobel,
titles=["dx", "dy"],
figsize=(16,4),
cmap="gray"
)
# In[8]:
def compute_components_of_H(dx, dy):
dx2 = np.square(dx)
dy2 = np.square(dy)
dxdy = dx*dy
dx2 = gaussian_blur(dx2,5)
dy2 = gaussian_blur(dy2,5)
dxdy = gaussian_blur(dxdy,5)
return dxdy, dx2, dy2
H = compute_components_of_H(*sobel)
show(
*H,
titles=["dxdy", "dx2", "dy2"],
figsize=(16,4),
cmap="gray"
)
# In[9]:
def compute_R(dxdy, dx2, dy2, k=0.06):
H=np.array([[dx2,dxdy],[dxdy,dy2]])
det=dx2*dy2-np.square(dxdy)
tr=np.matrix.trace(H)
R = det - k*(tr**2)
norm = (R-np.min(R))/(np.max(R)-np.min(R))
return norm
R = compute_R(*H)
show(
R,
titles=["R matrix"],
figsize=(16,4),
cmap="gray"
)
# In[10]:
def harris(I_gray, k=0.06):
R = I_gray
sobel = apply_sobel(R)
H = compute_components_of_H(*sobel)
R = compute_R(*H)
return R
I = load_image("img1.png")
I_gray = gray(I) / 255.0
R = harris(I_gray)
show(
R, visualize_corners(I, R),
titles=["R matrix", "Corners"],
figsize=(16,4),
cmap="gray"
)
# In[ ]: