Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyoyo-yo committed Sep 2, 2019
1 parent 5e20de1 commit 97b4f39
Show file tree
Hide file tree
Showing 22 changed files with 1,803 additions and 1,004 deletions.
2 changes: 1 addition & 1 deletion Question_81_90/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Q.85では最も色が近い画像が予測クラスであるとしたが、そ

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

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


答え
Expand Down
Binary file modified Question_81_90/answers/answer_81.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
118 changes: 72 additions & 46 deletions Question_81_90/answers/answer_81.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,94 @@
import numpy as np
import matplotlib.pyplot as plt

# Read image
img = cv2.imread("thorino.jpg").astype(np.float32)
H, W, C = img.shape
# Hessian corner detection
def Hessian_corner(img):

## Grayscale
def BGR2GRAY(img):
gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
gray = gray.astype(np.uint8)
return gray

## Sobel
def Sobel_filtering(gray):
# get shape
H, W = gray.shape

# sobel kernel
sobely = np.array(((1, 2, 1),
(0, 0, 0),
(-1, -2, -1)), dtype=np.float32)

sobelx = np.array(((1, 0, -1),
(2, 0, -2),
(1, 0, -1)), dtype=np.float32)

## Grayscale
gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
gray = gray.astype(np.uint8)
# padding
tmp = np.pad(gray, (1, 1), 'edge')

## Sobel
sobely = np.array(((1, 2, 1),
(0, 0, 0),
(-1, -2, -1)), dtype=np.float32)
# prepare
Ix = np.zeros_like(gray, dtype=np.float32)
Iy = np.zeros_like(gray, dtype=np.float32)

sobelx = np.array(((1, 0, -1),
(2, 0, -2),
(1, 0, -1)), dtype=np.float32)
# get differential
for y in range(H):
for x in range(W):
Ix[y, x] = np.mean(tmp[y : y + 3, x : x + 3] * sobelx)
Iy[y, x] = np.mean(tmp[y : y + 3, x : x + 3] * sobely)

Ix2 = Ix ** 2
Iy2 = Iy ** 2
Ixy = Ix * Iy

tmp = np.pad(gray, (1, 1), 'edge')
return Ix2, Iy2, Ixy

Ix = np.zeros_like(gray, dtype=np.float32)
Iy = np.zeros_like(gray, dtype=np.float32)


for y in range(H):
for x in range(W):
Ix[y, x] = np.mean(tmp[y:y+3, x:x+3] * sobelx)
Iy[y, x] = np.mean(tmp[y:y+3, x:x+3] * sobely)

tmp = np.pad(Ix, (1, 1), 'edge')
## Hessian
def corner_detect(gray, Ix2, Iy2, Ixy):
# get shape
H, W = gray.shape

Ix2 = np.zeros_like(gray, dtype=np.float32)
IxIy = np.zeros_like(gray, dtype=np.float32)
# prepare for show detection
out = np.array((gray, gray, gray))
out = np.transpose(out, (1,2,0))

for y in range(H):
for x in range(W):
Ix2[y, x] = np.mean(tmp[y:y+3, x:x+3] * sobelx)
IxIy[y, x] = np.mean(tmp[y:y+3, x:x+3] * sobely)
# get Hessian value
Hes = np.zeros((H, W))

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

Iy2 = np.zeros_like(gray, dtype=np.float32)
## Detect Corner and show
for y in range(H):
for x in range(W):
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:
out[y, x] = [0, 0, 255]

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

out = np.array((gray, gray, gray))
out = np.transpose(out, (1,2,0))
return out

## Hessian
Hes = np.zeros((H, W))

# 1. grayscale
gray = BGR2GRAY(img)

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

## Detect Corner
for y in range(H):
for x in range(W):
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:
out[y, x] = [0, 0, 255]
# 3. corner detection
out = corner_detect(gray, Ix2, Iy2, Ixy)

return out


# Read image
img = cv2.imread("thorino.jpg").astype(np.float32)

out = out.astype(np.uint8)
# Hessian corner detection
out = Hessian_corner(img)

cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
Expand Down
180 changes: 106 additions & 74 deletions Question_81_90/answers/answer_82.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,111 @@
import numpy as np
import matplotlib.pyplot as plt


# Harris corner detection
def Harris_corner_step1(img):

## Grayscale
def BGR2GRAY(img):
gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
gray = gray.astype(np.uint8)
return gray

## Sobel
def Sobel_filtering(gray):
# get shape
H, W = gray.shape

# sobel kernel
sobely = np.array(((1, 2, 1),
(0, 0, 0),
(-1, -2, -1)), dtype=np.float32)

sobelx = np.array(((1, 0, -1),
(2, 0, -2),
(1, 0, -1)), dtype=np.float32)

# padding
tmp = np.pad(gray, (1, 1), 'edge')

# prepare
Ix = np.zeros_like(gray, dtype=np.float32)
Iy = np.zeros_like(gray, dtype=np.float32)

# get differential
for y in range(H):
for x in range(W):
Ix[y, x] = np.mean(tmp[y : y + 3, x : x + 3] * sobelx)
Iy[y, x] = np.mean(tmp[y : y + 3, x : x + 3] * sobely)

Ix2 = Ix ** 2
Iy2 = Iy ** 2
Ixy = Ix * Iy

return Ix2, Iy2, Ixy


# gaussian filtering
def gaussian_filtering(I, K_size=3, sigma=3):
# get shape
H, W = I.shape

## gaussian
I_t = np.pad(I, (K_size // 2, K_size // 2), 'edge')

# gaussian kernel
K = np.zeros((K_size, K_size), dtype=np.float)
for x in range(K_size):
for y in range(K_size):
_x = x - K_size // 2
_y = y - K_size // 2
K[y, x] = np.exp( -(_x ** 2 + _y ** 2) / (2 * (sigma ** 2)))
K /= (sigma * np.sqrt(2 * np.pi))
K /= K.sum()

# filtering
for y in range(H):
for x in range(W):
I[y,x] = np.sum(I_t[y : y + K_size, x : x + K_size] * K)

return I


# 1. grayscale
gray = BGR2GRAY(img)

# 2. get difference image
Ix2, Iy2, Ixy = Sobel_filtering(gray)

# 3. gaussian filtering
Ix2 = gaussian_filtering(Ix2, K_size=3, sigma=3)
Iy2 = gaussian_filtering(Iy2, K_size=3, sigma=3)
Ixy = gaussian_filtering(Ixy, K_size=3, sigma=3)

# show result
plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2)

plt.subplot(1,3,1)
plt.imshow(Ix2, cmap='gray')
plt.title("Ix^2")
plt.axis("off")

plt.subplot(1,3,2)
plt.imshow(Iy2, cmap='gray')
plt.title("Iy^2")
plt.axis("off")

plt.subplot(1,3,3)
plt.imshow(Ixy, cmap='gray')
plt.title("Ixy")
plt.axis("off")

plt.savefig("out.png")
plt.show()


# Read image
img = cv2.imread("thorino.jpg").astype(np.float32)
H, W, C = img.shape

## Grayscale
gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]

# Harris

## Sobel
sobely = np.array(((1, 2, 1),
(0, 0, 0),
(-1, -2, -1)), dtype=np.float32)

sobelx = np.array(((1, 0, -1),
(2, 0, -2),
(1, 0, -1)), dtype=np.float32)

tmp = np.pad(gray, (1, 1), 'edge')

Ix = np.zeros_like(gray, dtype=np.float32)
Iy = np.zeros_like(gray, dtype=np.float32)

for y in range(H):
for x in range(W):
Ix[y, x] = np.sum(tmp[y:y+3, x:x+3] * sobelx)
Iy[y, x] = np.sum(tmp[y:y+3, x:x+3] * sobely)

Ix2 = Ix ** 2
Iy2 = Iy ** 2
Ixy = Ix * Iy

## gaussian
K_size = 3
sigma = 3
Ix2_t = np.pad(Ix2, (K_size // 2, K_size // 2), 'edge')
Iy2_t = np.pad(Iy2, (K_size // 2, K_size // 2), 'edge')
Ixy_t = np.pad(Ixy, (K_size // 2, K_size // 2), 'edge')

K = np.zeros((K_size, K_size), dtype=np.float)
for x in range(K_size):
for y in range(K_size):
_x = x - K_size // 2
_y = y - K_size // 2
K[y, x] = np.exp( -(_x**2 + _y**2) / (2 * (sigma**2)))
K /= (sigma * np.sqrt(2 * np.pi))
K /= K.sum()

for y in range(H):
for x in range(W):
Ix2[y,x] = np.sum(Ix2_t[y:y+K_size, x:x+K_size] * K)
Iy2[y,x] = np.sum(Iy2_t[y:y+K_size, x:x+K_size] * K)
Ixy[y,x] = np.sum(Ixy_t[y:y+K_size, x:x+K_size] * K)

out = np.array((gray, gray, gray))
out = np.transpose(out, (1,2,0))

plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2)

plt.subplot(1,3,1)
plt.imshow(Ix2, cmap='gray')
plt.title("Ix^2")
plt.axis("off")

plt.subplot(1,3,2)
plt.imshow(Iy2, cmap='gray')
plt.title("Iy^2")
plt.axis("off")

plt.subplot(1,3,3)
plt.imshow(Ixy, cmap='gray')
plt.title("Ixy")
plt.axis("off")

plt.savefig("out.png")
plt.show()

# Harris corner detection step1
out = Harris_corner_step1(img)
Loading

0 comments on commit 97b4f39

Please sign in to comment.