Skip to content

Commit 97b4f39

Browse files
committed
add
1 parent 5e20de1 commit 97b4f39

22 files changed

+1803
-1004
lines changed

Question_81_90/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Q.85では最も色が近い画像が予測クラスであるとしたが、そ
167167

168168
これを回避するために、ここでは色合いが近い画像を3つ選び、それらの多数決によって予測クラスを決定し、Accuracyを計算せよ。
169169

170-
このように特徴が近いものを学習データから3つ選んで判断する手法をk近傍(k-NN: k-Nearest Neighbor)という。Q.85.のNN法はk=1の場合とみれる。
170+
このように特徴が近いものを学習データからk個選んで判断する手法をk近傍(k-NN: k-Nearest Neighbor)という。Q.85.のNN法はk=1の場合とみれる。
171171

172172

173173
答え

Question_81_90/answers/answer_81.jpg

-275 Bytes
Loading

Question_81_90/answers/answer_81.py

Lines changed: 72 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,94 @@
22
import numpy as np
33
import matplotlib.pyplot as plt
44

5-
# Read image
6-
img = cv2.imread("thorino.jpg").astype(np.float32)
7-
H, W, C = img.shape
5+
# Hessian corner detection
6+
def Hessian_corner(img):
7+
8+
## Grayscale
9+
def BGR2GRAY(img):
10+
gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
11+
gray = gray.astype(np.uint8)
12+
return gray
13+
14+
## Sobel
15+
def Sobel_filtering(gray):
16+
# get shape
17+
H, W = gray.shape
18+
19+
# sobel kernel
20+
sobely = np.array(((1, 2, 1),
21+
(0, 0, 0),
22+
(-1, -2, -1)), dtype=np.float32)
23+
24+
sobelx = np.array(((1, 0, -1),
25+
(2, 0, -2),
26+
(1, 0, -1)), dtype=np.float32)
827

9-
## Grayscale
10-
gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
11-
gray = gray.astype(np.uint8)
28+
# padding
29+
tmp = np.pad(gray, (1, 1), 'edge')
1230

13-
## Sobel
14-
sobely = np.array(((1, 2, 1),
15-
(0, 0, 0),
16-
(-1, -2, -1)), dtype=np.float32)
31+
# prepare
32+
Ix = np.zeros_like(gray, dtype=np.float32)
33+
Iy = np.zeros_like(gray, dtype=np.float32)
1734

18-
sobelx = np.array(((1, 0, -1),
19-
(2, 0, -2),
20-
(1, 0, -1)), dtype=np.float32)
35+
# get differential
36+
for y in range(H):
37+
for x in range(W):
38+
Ix[y, x] = np.mean(tmp[y : y + 3, x : x + 3] * sobelx)
39+
Iy[y, x] = np.mean(tmp[y : y + 3, x : x + 3] * sobely)
40+
41+
Ix2 = Ix ** 2
42+
Iy2 = Iy ** 2
43+
Ixy = Ix * Iy
2144

22-
tmp = np.pad(gray, (1, 1), 'edge')
45+
return Ix2, Iy2, Ixy
2346

24-
Ix = np.zeros_like(gray, dtype=np.float32)
25-
Iy = np.zeros_like(gray, dtype=np.float32)
47+
2648

27-
for y in range(H):
28-
for x in range(W):
29-
Ix[y, x] = np.mean(tmp[y:y+3, x:x+3] * sobelx)
30-
Iy[y, x] = np.mean(tmp[y:y+3, x:x+3] * sobely)
31-
32-
tmp = np.pad(Ix, (1, 1), 'edge')
49+
## Hessian
50+
def corner_detect(gray, Ix2, Iy2, Ixy):
51+
# get shape
52+
H, W = gray.shape
3353

34-
Ix2 = np.zeros_like(gray, dtype=np.float32)
35-
IxIy = np.zeros_like(gray, dtype=np.float32)
54+
# prepare for show detection
55+
out = np.array((gray, gray, gray))
56+
out = np.transpose(out, (1,2,0))
3657

37-
for y in range(H):
38-
for x in range(W):
39-
Ix2[y, x] = np.mean(tmp[y:y+3, x:x+3] * sobelx)
40-
IxIy[y, x] = np.mean(tmp[y:y+3, x:x+3] * sobely)
58+
# get Hessian value
59+
Hes = np.zeros((H, W))
4160

42-
tmp = np.pad(Iy, (1, 1), 'edge')
61+
for y in range(H):
62+
for x in range(W):
63+
Hes[y,x] = Ix2[y,x] * Iy2[y,x] - Ixy[y,x] ** 2
4364

44-
Iy2 = np.zeros_like(gray, dtype=np.float32)
65+
## Detect Corner and show
66+
for y in range(H):
67+
for x in range(W):
68+
if Hes[y,x] == np.max(Hes[max(y-1, 0) : min(y+2, H), max(x-1, 0) : min(x+2, W)]) and Hes[y, x] > np.max(Hes) * 0.1:
69+
out[y, x] = [0, 0, 255]
4570

46-
for y in range(H):
47-
for x in range(W):
48-
Iy2[y, x] = np.mean(tmp[y:y+3, x:x+3] * sobely)
71+
out = out.astype(np.uint8)
4972

50-
out = np.array((gray, gray, gray))
51-
out = np.transpose(out, (1,2,0))
73+
return out
5274

53-
## Hessian
54-
Hes = np.zeros((H, W))
75+
76+
# 1. grayscale
77+
gray = BGR2GRAY(img)
5578

56-
for y in range(H):
57-
for x in range(W):
58-
Hes[y,x] = Ix2[y,x] * Iy2[y,x] - IxIy[y,x] ** 2
79+
# 2. get difference image
80+
Ix2, Iy2, Ixy = Sobel_filtering(gray)
5981

60-
## Detect Corner
61-
for y in range(H):
62-
for x in range(W):
63-
if Hes[y,x] == np.max(Hes[max(y-1,0):min(y+2,H), max(x-1,0):min(x+2,W)]) and Hes[y,x] > np.max(Hes)*0.1:
64-
out[y, x] = [0, 0, 255]
82+
# 3. corner detection
83+
out = corner_detect(gray, Ix2, Iy2, Ixy)
84+
85+
return out
86+
87+
88+
# Read image
89+
img = cv2.imread("thorino.jpg").astype(np.float32)
6590

66-
out = out.astype(np.uint8)
91+
# Hessian corner detection
92+
out = Hessian_corner(img)
6793

6894
cv2.imwrite("out.jpg", out)
6995
cv2.imshow("result", out)

Question_81_90/answers/answer_82.py

Lines changed: 106 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,111 @@
22
import numpy as np
33
import matplotlib.pyplot as plt
44

5+
6+
# Harris corner detection
7+
def Harris_corner_step1(img):
8+
9+
## Grayscale
10+
def BGR2GRAY(img):
11+
gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
12+
gray = gray.astype(np.uint8)
13+
return gray
14+
15+
## Sobel
16+
def Sobel_filtering(gray):
17+
# get shape
18+
H, W = gray.shape
19+
20+
# sobel kernel
21+
sobely = np.array(((1, 2, 1),
22+
(0, 0, 0),
23+
(-1, -2, -1)), dtype=np.float32)
24+
25+
sobelx = np.array(((1, 0, -1),
26+
(2, 0, -2),
27+
(1, 0, -1)), dtype=np.float32)
28+
29+
# padding
30+
tmp = np.pad(gray, (1, 1), 'edge')
31+
32+
# prepare
33+
Ix = np.zeros_like(gray, dtype=np.float32)
34+
Iy = np.zeros_like(gray, dtype=np.float32)
35+
36+
# get differential
37+
for y in range(H):
38+
for x in range(W):
39+
Ix[y, x] = np.mean(tmp[y : y + 3, x : x + 3] * sobelx)
40+
Iy[y, x] = np.mean(tmp[y : y + 3, x : x + 3] * sobely)
41+
42+
Ix2 = Ix ** 2
43+
Iy2 = Iy ** 2
44+
Ixy = Ix * Iy
45+
46+
return Ix2, Iy2, Ixy
47+
48+
49+
# gaussian filtering
50+
def gaussian_filtering(I, K_size=3, sigma=3):
51+
# get shape
52+
H, W = I.shape
53+
54+
## gaussian
55+
I_t = np.pad(I, (K_size // 2, K_size // 2), 'edge')
56+
57+
# gaussian kernel
58+
K = np.zeros((K_size, K_size), dtype=np.float)
59+
for x in range(K_size):
60+
for y in range(K_size):
61+
_x = x - K_size // 2
62+
_y = y - K_size // 2
63+
K[y, x] = np.exp( -(_x ** 2 + _y ** 2) / (2 * (sigma ** 2)))
64+
K /= (sigma * np.sqrt(2 * np.pi))
65+
K /= K.sum()
66+
67+
# filtering
68+
for y in range(H):
69+
for x in range(W):
70+
I[y,x] = np.sum(I_t[y : y + K_size, x : x + K_size] * K)
71+
72+
return I
73+
74+
75+
# 1. grayscale
76+
gray = BGR2GRAY(img)
77+
78+
# 2. get difference image
79+
Ix2, Iy2, Ixy = Sobel_filtering(gray)
80+
81+
# 3. gaussian filtering
82+
Ix2 = gaussian_filtering(Ix2, K_size=3, sigma=3)
83+
Iy2 = gaussian_filtering(Iy2, K_size=3, sigma=3)
84+
Ixy = gaussian_filtering(Ixy, K_size=3, sigma=3)
85+
86+
# show result
87+
plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2)
88+
89+
plt.subplot(1,3,1)
90+
plt.imshow(Ix2, cmap='gray')
91+
plt.title("Ix^2")
92+
plt.axis("off")
93+
94+
plt.subplot(1,3,2)
95+
plt.imshow(Iy2, cmap='gray')
96+
plt.title("Iy^2")
97+
plt.axis("off")
98+
99+
plt.subplot(1,3,3)
100+
plt.imshow(Ixy, cmap='gray')
101+
plt.title("Ixy")
102+
plt.axis("off")
103+
104+
plt.savefig("out.png")
105+
plt.show()
106+
107+
5108
# Read image
6109
img = cv2.imread("thorino.jpg").astype(np.float32)
7-
H, W, C = img.shape
8-
9-
## Grayscale
10-
gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
11-
12-
# Harris
13-
14-
## Sobel
15-
sobely = np.array(((1, 2, 1),
16-
(0, 0, 0),
17-
(-1, -2, -1)), dtype=np.float32)
18-
19-
sobelx = np.array(((1, 0, -1),
20-
(2, 0, -2),
21-
(1, 0, -1)), dtype=np.float32)
22-
23-
tmp = np.pad(gray, (1, 1), 'edge')
24-
25-
Ix = np.zeros_like(gray, dtype=np.float32)
26-
Iy = np.zeros_like(gray, dtype=np.float32)
27-
28-
for y in range(H):
29-
for x in range(W):
30-
Ix[y, x] = np.sum(tmp[y:y+3, x:x+3] * sobelx)
31-
Iy[y, x] = np.sum(tmp[y:y+3, x:x+3] * sobely)
32-
33-
Ix2 = Ix ** 2
34-
Iy2 = Iy ** 2
35-
Ixy = Ix * Iy
36-
37-
## gaussian
38-
K_size = 3
39-
sigma = 3
40-
Ix2_t = np.pad(Ix2, (K_size // 2, K_size // 2), 'edge')
41-
Iy2_t = np.pad(Iy2, (K_size // 2, K_size // 2), 'edge')
42-
Ixy_t = np.pad(Ixy, (K_size // 2, K_size // 2), 'edge')
43-
44-
K = np.zeros((K_size, K_size), dtype=np.float)
45-
for x in range(K_size):
46-
for y in range(K_size):
47-
_x = x - K_size // 2
48-
_y = y - K_size // 2
49-
K[y, x] = np.exp( -(_x**2 + _y**2) / (2 * (sigma**2)))
50-
K /= (sigma * np.sqrt(2 * np.pi))
51-
K /= K.sum()
52-
53-
for y in range(H):
54-
for x in range(W):
55-
Ix2[y,x] = np.sum(Ix2_t[y:y+K_size, x:x+K_size] * K)
56-
Iy2[y,x] = np.sum(Iy2_t[y:y+K_size, x:x+K_size] * K)
57-
Ixy[y,x] = np.sum(Ixy_t[y:y+K_size, x:x+K_size] * K)
58-
59-
out = np.array((gray, gray, gray))
60-
out = np.transpose(out, (1,2,0))
61-
62-
plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2)
63-
64-
plt.subplot(1,3,1)
65-
plt.imshow(Ix2, cmap='gray')
66-
plt.title("Ix^2")
67-
plt.axis("off")
68-
69-
plt.subplot(1,3,2)
70-
plt.imshow(Iy2, cmap='gray')
71-
plt.title("Iy^2")
72-
plt.axis("off")
73-
74-
plt.subplot(1,3,3)
75-
plt.imshow(Ixy, cmap='gray')
76-
plt.title("Ixy")
77-
plt.axis("off")
78-
79-
plt.savefig("out.png")
80-
plt.show()
110+
111+
# Harris corner detection step1
112+
out = Harris_corner_step1(img)

0 commit comments

Comments
 (0)