This repository has been archived by the owner on Mar 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
practicum1.py
228 lines (180 loc) · 6 KB
/
practicum1.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# -*- coding: utf-8 -*-
import numpy as np
import cv2
'''
Google it:
* trackbar
* hihgui tutorial
* Partial differentials
* image.shape
* tentative numpy tutorial
* opencv
* scipy
'''
def partial_differential(i,a):
if a == "d/dx":
for k1 in range(m-1):
for k2 in range(n):
i[k1][k2] = i[k1+1][k2] - i[k1][k2]
elif a == "d/dy":
for k1 in range(m):
for k2 in range(n-1):
i[k1][k2] = i[k1][k2+1] - i[k1][k2]
elif a == "d^2/dx^2":
for k1 in range(m-2):
for k2 in range(n):
i[k1][k2] = i[k1+2][k2] - 2 * i[k1+1][k2] + i[k1][k2]
elif a == "d^2/dy^2":
for k1 in range(m):
for k2 in range(n-2):
i[k1][k2] = i[k1][k2+2] - 2 * i[k1][k2+1] + i[k1][k2]
else:
print("incorrect input")
return i
def convolution(image, kernel):
# m n
# (k*l)*[x,y] = SUM SUM k[i,j] * l[x-i,y-j]
# j=1 i=1
image = np.array(image)
kernel = np.array(kernel)
height, width = image.shape
height_kern, width_kern = kernel.shape
kernel_centerY = height_kern / 2
kernel_centerX = width_kern / 2
image2 = np.zeros((height,width), dtype = np.int)
for y1 in range(height):
for x1 in range(width):
for y2 in range(height_kern):
n = height_kern - 1 - y2
for x2 in range(width_kern):
m = width_kern - 1 - x2
ii = y1 + (y2 - kernel_centerY)
jj = x1 + (x2 - kernel_centerX)
if ((ii >= 0) and (ii < height) and (jj >=0) and (jj < width)):
image2[y1][x1] += image[ii][jj] * kernel[n][m]
return np.array(image2)
# http://docs.scipy.org/doc/scipy/reference/ndimage.html
def morphology_dilation(image, struct_element):
image = np.array(image)
struct_element = np.array(struct_element)
height, width = image.shape
height_struct, width_struct = struct_element.shape
image2 = np.zeros((height,width), dtype = np.int)
struct_element_centerY = height_struct / 2
struct_element_centerX = width_struct / 2
for y1 in range(height):
for x1 in range(width):
if (image[y1][x1] == 1):
for y2 in range(height_struct):
for x2 in range(width_struct):
ii = y1 + y2 - struct_element_centerY
jj = x1 + x2 - struct_element_centerX
if (ii >= 0) and (ii < height) and (jj >=0) and (jj < width):
image2[ii][jj] = max(image[ii][jj], struct_element[y2][x2])
return image2
def morphology_erosion(image, struct_element):
image = np.array(image)
struct_element = np.array(struct_element)
height, width = image.shape
height_struct, width_struct = struct_element.shape
image2 = np.zeros((height,width), dtype = np.int)
struct_element_centerY = height_struct / 2
struct_element_centerX = width_struct / 2
for y1 in range(height):
for x1 in range(width):
sum = 0
for y2 in range(height_struct):
for x2 in range(width_struct):
ii = y1 + y2 - struct_element_centerY
jj = x1 + x2 - struct_element_centerX
if (ii >= 0) and (ii < height) and (jj >=0) and (jj < width):
if (image[ii][jj] == struct_element[y2][x2]):
sum += 1
if (sum == (height_struct * width_struct)):
image2[y1][x1] = image[ii][jj]
else:
image2[y1][x1] = 0
return image2
def morphology_opening(image, struct_element):
image2 = morphology_erosion(image, struct_element)
image3 = morphology_dilation(image2, struct_element)
return image3
def morphology_closing(image, struct_element):
image2 = morphology_dilation(image, struct_element)
image3 = morphology_erosion(image2, struct_element)
return image3
def partial_derivatives(image, a):
if a == "dx":
return convolution(image, [[1,-1]])
elif a == "dy":
return convolution(image, [[1],[-1]])
def element_multiply(image, image2):
image = np.array(image)
image2 = np.array(image2)
height, width = image.shape
height2, width2 = image2.shape
if (height == height2) and (width == width2):
for y in range(height):
for x in range(width):
image[y][x] *= image2[y][x]
return image
def corner_response(s_x, s_y, s_xy):
h, w = s_x.shape
detector = np.zeros((h,w), dtype = np.int)
determinant = 0
trace = 0
k = 0.06 # 0.04 - 0.06
for y in range(h):
for x in range(w):
determinant = s_x[y][x] * s_y[y][x] - s_xy[y][x] * s_xy[y][x]
trace = s_x[y][x] + s_y[y][x]
detector[y][x] = determinant - k * (trace * trace)
return detector
def corner_threshold(image):
h, w = image.shape
for y in range(h):
for x in range(w):
for i in range(3):
for j in range(3):
ii = y + i - 1
jj = x + j - 1
if (ii != y) and (jj != x) and (ii >= 0) and (ii < h) and (jj >= 0) and (jj < w):
if (image[ii][jj] > image[y][x]):
image[y][x] = 0
i = 2
j = 2
return image
def harris_corner_detector(image,kernel):
# Вычислить частные производные Ix Iy
image_dx = partial_derivatives(image, "dx")
image_dy = partial_derivatives(image, "dy")
# Вычислить поэлементные произведения: Ix^2, Iy^2, Ixy = Ix * Iy
imageXX = element_multiply(image_dx, image_dx)
imageYY = element_multiply(image_dy, image_dy)
imageXY = element_multiply(image_dx, image_dy)
# Выполнить гауссово размытия для полученных матриц
s_x = convolution(imageXX, kernel)
s_y = convolution(imageYY, kernel)
s_xy = convolution(imageXY, kernel)
# В каждой точке xy определить матрицу
# H = [
# [ s_x , s_xy ]
# [ s_xy , s_y ]
# ]
# Вычислить отклик детектора
detector = corner_response(s_x, s_y, s_xy)
# Применить подавление немаксимумов
detector = corner_threshold(detector)
return detector
if __name__ == "__main__":
image = cv2.imread("empire_state.jpg",2)
kernel = [[0,1,0],
[1,4,1],
[0,1,0]]
const = 0.125
kernel = [[x * const for x in y ] for y in kernel]
kernel = np.array(kernel)
image2 = harris_corner_detector(image,kernel)
cv2.imshow("1",image)
cv2.imshow("2", image2)
cv2.waitKey()