Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyoyo-yo committed Oct 22, 2019
1 parent 31113f1 commit dc33ddf
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 54 deletions.
6 changes: 5 additions & 1 deletion Question_01_10/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,12 @@ v = 1/|R| * Sum_{i in R} v_i
ただし、画像の端はこのままではフィルタリングできないため、画素が足りない部分は0で埋める。これを**0パディング**と呼ぶ。
かつ、重みは正規化する。(sum g = 1)
重みはガウス分布から次式になる。
<img src='assets/gaussian_filter.png' width=200>
```bash
重み g(x,y,s) = 1/ (s*sqrt(2 * pi)) * exp( - (x^2 + y^2) / (2*s^2))
重み g(x,y,s) = 1/ (2 * pi * sigma * sigma) * exp( - (x^2 + y^2) / (2*s^2))
標準偏差s = 1.3による8近傍ガウシアンフィルタは
1 2 1
K = 1/16 [ 2 4 2 ]
Expand Down
Binary file modified Question_01_10/answers_image/answer_9.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions Question_01_10/answers_py/answer_9.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def gaussian_filter(img, K_size=3, sigma=1.3):
for x in range(-pad, -pad + K_size):
for y in range(-pad, -pad + K_size):
K[y + pad, x + pad] = np.exp( -(x ** 2 + y ** 2) / (2 * (sigma ** 2)))
K /= (sigma * np.sqrt(2 * np.pi))
K /= (2 * np.pi * sigma * sigma)
K /= K.sum()

tmp = out.copy()
Expand All @@ -30,8 +30,9 @@ def gaussian_filter(img, K_size=3, sigma=1.3):
for y in range(H):
for x in range(W):
for c in range(C):
out[pad + y, pad + x] = np.sum(K * tmp[y: y + K_size, x: x + K_size])
out[pad + y, pad + x, c] = np.sum(K * tmp[y: y + K_size, x: x + K_size, c])

out = np.clip(out, 0, 255)
out = out[pad: pad + H, pad: pad + W].astype(np.uint8)

return out
Expand Down
Binary file added Question_01_10/assets/gaussian_filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Question_41_50/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ if angle(x,y) = 135
2. LT以下のedge(x,y)=0
3. LT < edge(x,y) < HTの時、周り8ピクセルの勾配強度でHTより大きい値が存在すれば、edge(x,y)=255
ここでは、HT=160, LT=80とする。ちなみに閾値の値は結果を見ながら判断するしかない。
ここでは、HT=50, LT=20とする。ちなみに閾値の値は結果を見ながら判断するしかない。
以上のアルゴリズムによって、Canny法が行われる。
Expand Down
Binary file added Question_41_50/answers_image/.DS_Store
Binary file not shown.
Binary file modified Question_41_50/answers_image/answer_41_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Question_41_50/answers_image/answer_41_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Question_41_50/answers_image/answer_42_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Question_41_50/answers_image/answer_42_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Question_41_50/answers_image/answer_43.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 26 additions & 16 deletions Question_41_50/answers_py/answer_41.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ def gaussian_filter(img, K_size=3, sigma=1.3):

if len(img.shape) == 3:
H, W, C = img.shape
gray = False
else:
img = np.expand_dims(img, axis=-1)
H, W, C = img.shape
gray = True

## Zero padding
pad = K_size // 2
Expand All @@ -35,8 +37,8 @@ def gaussian_filter(img, K_size=3, sigma=1.3):
K = np.zeros((K_size, K_size), dtype=np.float)
for x in range(-pad, -pad + K_size):
for y in range(-pad, -pad + K_size):
K[y+pad, x+pad] = np.exp( -(x ** 2 + y ** 2) / (2 * (sigma ** 2)))
K /= (sigma * np.sqrt(2 * np.pi))
K[y + pad, x + pad] = np.exp( - (x ** 2 + y ** 2) / (2 * (sigma ** 2)))
K /= (2 * np.pi * sigma * sigma)
K /= K.sum()

tmp = out.copy()
Expand All @@ -45,9 +47,14 @@ def gaussian_filter(img, K_size=3, sigma=1.3):
for y in range(H):
for x in range(W):
for c in range(C):
out[pad + y, pad + x, c] = np.sum(K * tmp[y: y + K_size, x: x + K_size, c])
out[pad + y, pad + x, c] = np.sum(K * tmp[y : y + K_size, x : x + K_size, c])

out = np.clip(out, 0, 255)
out = out[pad : pad + H, pad : pad + W]
#out = out.astype(np.uint8)

out = out[pad: pad + H, pad: pad + W].astype(np.uint8)
if gray:
out = out[..., 0]

return out

Expand All @@ -57,13 +64,13 @@ def sobel_filter(img, K_size=3):
if len(img.shape) == 3:
H, W, C = img.shape
else:
img = np.expand_dims(img, axis=-1)
H, W, C = img.shape
#img = np.expand_dims(img, axis=-1)
H, W = img.shape

# Zero padding
pad = K_size // 2
out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float)
out[pad: pad + H, pad: pad + W] = gray.copy().astype(np.float)
out[pad : pad + H, pad : pad + W] = img.copy().astype(np.float)
tmp = out.copy()

out_v = out.copy()
Expand All @@ -77,14 +84,14 @@ def sobel_filter(img, K_size=3):
# filtering
for y in range(H):
for x in range(W):
out_v[pad + y, pad + x] = np.sum(Kv * (tmp[y: y + K_size, x: x + K_size]))
out_h[pad + y, pad + x] = np.sum(Kh * (tmp[y: y + K_size, x: x + K_size]))
out_v[pad + y, pad + x] = np.sum(Kv * (tmp[y : y + K_size, x : x + K_size]))
out_h[pad + y, pad + x] = np.sum(Kh * (tmp[y : y + K_size, x : x + K_size]))

out_v = np.clip(out_v, 0, 255)
out_h = np.clip(out_h, 0, 255)

out_v = out_v[pad: pad + H, pad: pad + W].astype(np.uint8)
out_h = out_h[pad: pad + H, pad: pad + W].astype(np.uint8)
out_v = out_v[pad : pad + H, pad : pad + W].astype(np.uint8)
out_h = out_h[pad : pad + H, pad : pad + W].astype(np.uint8)

return out_v, out_h

Expand Down Expand Up @@ -126,19 +133,22 @@ def angle_quantization(angle):
# angle quantization
angle = angle_quantization(angle)

return angle
return edge, angle


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

# Canny (step1)
angle = Canny_step1(img)
edge, angle = Canny_step1(img)

out = angle.astype(np.uint8)
edge = edge.astype(np.uint8)
angle = angle.astype(np.uint8)

# Save result
cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.imwrite("out.jpg", edge)
cv2.imshow("result", edge)
cv2.imwrite("out2.jpg", angle)
cv2.imshow("result2", angle)
cv2.waitKey(0)
cv2.destroyAllWindows()
42 changes: 26 additions & 16 deletions Question_41_50/answers_py/answer_42.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ def gaussian_filter(img, K_size=3, sigma=1.3):
K = np.zeros((K_size, K_size), dtype=np.float)
for x in range(-pad, -pad + K_size):
for y in range(-pad, -pad + K_size):
K[y+pad, x+pad] = np.exp( -(x ** 2 + y ** 2) / (2 * (sigma ** 2)))
K /= (sigma * np.sqrt(2 * np.pi))
K[y + pad, x + pad] = np.exp( - (x ** 2 + y ** 2) / (2 * (sigma ** 2)))
#K /= (sigma * np.sqrt(2 * np.pi))
K /= (2 * np.pi * sigma * sigma)
K /= K.sum()

tmp = out.copy()
Expand All @@ -45,22 +46,27 @@ def gaussian_filter(img, K_size=3, sigma=1.3):
for y in range(H):
for x in range(W):
for c in range(C):
out[pad + y, pad + x, c] = np.sum(K * tmp[y: y + K_size, x: x + K_size, c])
out[pad + y, pad + x, c] = np.sum(K * tmp[y : y + K_size, x : x + K_size, c])

out = out[pad: pad + H, pad: pad + W].astype(np.uint8)
out = np.clip(out, 0, 255)
out = out[pad : pad + H, pad : pad + W]
out = out.astype(np.uint8)
out = out[..., 0]

return out


# sobel filter
def sobel_filter(img, K_size=3):
H, W = img.shape
if len(img.shape) == 3:
H, W, C = img.shape
else:
H, W = img.shape

# Zero padding
pad = K_size // 2
out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float)
out[pad: pad + H, pad: pad + W] = gray.copy().astype(np.float)
out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float)
tmp = out.copy()

out_v = out.copy()
Expand Down Expand Up @@ -109,12 +115,13 @@ def angle_quantization(angle):
_angle[np.where((angle > 67.5) & (angle <= 112.5))] = 90
_angle[np.where((angle > 112.5) & (angle <= 157.5))] = 135

return angle
return _angle


def non_maximum_suppression(angle, edge):
H, W = angle.shape

_edge = edge.copy()

for y in range(H):
for x in range(W):
if angle[y, x] == 0:
Expand All @@ -137,10 +144,10 @@ def non_maximum_suppression(angle, edge):
if y == H-1:
dy1 = min(dy1, 0)
dy2 = min(dy2, 0)
if max(max(edge[y, x], edge[y+dy1, x+dx1]), edge[y+dy2, x+dx2]) != edge[y, x]:
edge[y, x] = 0
if max(max(edge[y, x], edge[y + dy1, x + dx1]), edge[y + dy2, x + dx2]) != edge[y, x]:
_edge[y, x] = 0

return edge
return _edge

# grayscale
gray = BGR2GRAY(img)
Expand All @@ -160,19 +167,22 @@ def non_maximum_suppression(angle, edge):
# non maximum suppression
edge = non_maximum_suppression(angle, edge)

return edge
return edge, angle


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

# Canny (step2)
edge = Canny_step2(img)
edge, angle = Canny_step2(img)

out = edge.astype(np.uint8)
edge = edge.astype(np.uint8)
angle = angle.astype(np.uint8)

# Save result
cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.imwrite("out.jpg", edge)
cv2.imshow("result", edge)
cv2.imwrite("out2.jpg", angle)
cv2.imshow("result2", angle)
cv2.waitKey(0)
cv2.destroyAllWindows()
49 changes: 31 additions & 18 deletions Question_41_50/answers_py/answer_43.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@ def gaussian_filter(img, K_size=3, sigma=1.3):

if len(img.shape) == 3:
H, W, C = img.shape
gray = False
else:
img = np.expand_dims(img, axis=-1)
H, W, C = img.shape
gray = True

## Zero padding
pad = K_size // 2
out = np.zeros([H + pad * 2, W + pad * 2, C], dtype=np.float)
out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float)
out[pad : pad + H, pad : pad + W] = img.copy().astype(np.float)

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

tmp = out.copy()
Expand All @@ -45,22 +48,29 @@ def gaussian_filter(img, K_size=3, sigma=1.3):
for y in range(H):
for x in range(W):
for c in range(C):
out[pad + y, pad + x, c] = np.sum(K * tmp[y: y + K_size, x: x + K_size, c])
out[pad + y, pad + x, c] = np.sum(K * tmp[y : y + K_size, x : x + K_size, c])

out = out[pad: pad + H, pad: pad + W].astype(np.uint8)
out = out[..., 0]
out = np.clip(out, 0, 255)
out = out[pad : pad + H, pad : pad + W]
out = out.astype(np.uint8)

if gray:
out = out[..., 0]

return out


# sobel filter
def sobel_filter(img, K_size=3):
H, W = img.shape
if len(img.shape) == 3:
H, W, C = img.shape
else:
H, W = img.shape

# Zero padding
pad = K_size // 2
out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float)
out[pad: pad + H, pad: pad + W] = gray.copy().astype(np.float)
out[pad : pad + H, pad : pad + W] = img.copy().astype(np.float)
tmp = out.copy()

out_v = out.copy()
Expand All @@ -74,14 +84,16 @@ def sobel_filter(img, K_size=3):
# filtering
for y in range(H):
for x in range(W):
out_v[pad + y, pad + x] = np.sum(Kv * (tmp[y: y + K_size, x: x + K_size]))
out_h[pad + y, pad + x] = np.sum(Kh * (tmp[y: y + K_size, x: x + K_size]))
out_v[pad + y, pad + x] = np.sum(Kv * (tmp[y : y + K_size, x : x + K_size]))
out_h[pad + y, pad + x] = np.sum(Kh * (tmp[y : y + K_size, x : x + K_size]))

out_v = np.clip(out_v, 0, 255)
out_h = np.clip(out_h, 0, 255)

out_v = out_v[pad: pad + H, pad: pad + W].astype(np.uint8)
out_h = out_h[pad: pad + H, pad: pad + W].astype(np.uint8)
out_v = out_v[pad : pad + H, pad : pad + W]
out_v = out_v.astype(np.uint8)
out_h = out_h[pad : pad + H, pad : pad + W]
out_h = out_h.astype(np.uint8)

return out_v, out_h

Expand Down Expand Up @@ -114,6 +126,7 @@ def angle_quantization(angle):

def non_maximum_suppression(angle, edge):
H, W = angle.shape
_edge = edge.copy()

for y in range(H):
for x in range(W):
Expand All @@ -137,10 +150,10 @@ def non_maximum_suppression(angle, edge):
if y == H-1:
dy1 = min(dy1, 0)
dy2 = min(dy2, 0)
if max(max(edge[y, x], edge[y+dy1, x+dx1]), edge[y+dy2, x+dx2]) != edge[y, x]:
edge[y, x] = 0
if max(max(edge[y, x], edge[y + dy1, x + dx1]), edge[y + dy2, x + dx2]) != edge[y, x]:
_edge[y, x] = 0

return edge
return _edge

def hysterisis(edge, HT=100, LT=30):
H, W = edge.shape
Expand All @@ -149,8 +162,8 @@ def hysterisis(edge, HT=100, LT=30):
edge[edge >= HT] = 255
edge[edge <= LT] = 0

_edge = np.zeros((H+2, W+2), dtype=np.float32)
_edge[1:H+1, 1:W+1] = edge
_edge = np.zeros((H + 2, W + 2), dtype=np.float32)
_edge[1 : H + 1, 1 : W + 1] = edge

## 8 - Nearest neighbor
nn = np.array(((1., 1., 1.), (1., 0., 1.), (1., 1., 1.)), dtype=np.float32)
Expand Down Expand Up @@ -187,7 +200,7 @@ def hysterisis(edge, HT=100, LT=30):
edge = non_maximum_suppression(angle, edge)

# hysterisis threshold
out = hysterisis(edge, 160, 80)
out = hysterisis(edge, 50, 20)

return out

Expand Down

0 comments on commit dc33ddf

Please sign in to comment.