Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyoyo-yo committed Aug 27, 2019
1 parent 051ab19 commit de8a396
Show file tree
Hide file tree
Showing 16 changed files with 622 additions and 351 deletions.
4 changes: 2 additions & 2 deletions Question_51_60/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ NCCは照明変化に強いと言われる。

ZNCC(Zero means Normalized Cross Correlation)とは零平均正規化相互相関を類似度にする手法であり、Sが**最大**の位置がマッチング位置となる。

画像Iの平均値をmi、画像Tの平均値をmtとすると、Sは次式で計算される。
画像Iの平均値をmi、画像Tの平均値をmtとすると、Sは次式で計算される。(ただし、平均値はRGB成分ごとに減算する)

```bash
Sum_{x=0:w, y=0:h} (I(i+x, j+y)-mi) * (T(x, y)-mt)
Expand All @@ -129,7 +129,7 @@ S = ----------------------------------------------------------------------------
```

このSは、-1<=S<=1をとる。
ZNCCは平均値を引くことでNCCよりも照明変化に強いと言われる。(だが今回は検出が失敗する。)
ZNCCは平均値を引くことでNCCよりも照明変化に強いと言われる。

|入力 (imori.jpg) |テンプレート画像(imori_part.jpg)|出力(answers/answer_57.jpg)|
|:---:|:---:|:---:|
Expand Down
Binary file modified Question_51_60/answers/answer_51.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
158 changes: 101 additions & 57 deletions Question_51_60/answers/answer_51.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,109 @@
import numpy as np
import matplotlib.pyplot as plt


# Gray scale
def BGR2GRAY(img):
b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()

# Gray scale
out = 0.2126 * r + 0.7152 * g + 0.0722 * b
out = out.astype(np.uint8)

return out

# Otsu Binalization
def otsu_binarization(img, th=128):
H, W = img.shape
out = img.copy()

max_sigma = 0
max_t = 0

# determine threshold
for _t in range(1, 255):
v0 = out[np.where(out < _t)]
m0 = np.mean(v0) if len(v0) > 0 else 0.
w0 = len(v0) / (H * W)
v1 = out[np.where(out >= _t)]
m1 = np.mean(v1) if len(v1) > 0 else 0.
w1 = len(v1) / (H * W)
sigma = w0 * w1 * ((m0 - m1) ** 2)
if sigma > max_sigma:
max_sigma = sigma
max_t = _t

# Binarization
print("threshold >>", max_t)
th = max_t
out[out < th] = 0
out[out >= th] = 255

return out


# Erosion
def Erode(img, Erode_time=1):
H, W = img.shape
out = img.copy()

# kernel
MF = np.array(((0, 1, 0),
(1, 0, 1),
(0, 1, 0)), dtype=np.int)

# each erode
for i in range(Erode_time):
tmp = np.pad(out, (1, 1), 'edge')
# erode
for y in range(1, H+1):
for x in range(1, W+1):
if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4:
out[y-1, x-1] = 0

return out


# Dilation
def Dilate(img, Dil_time=1):
H, W = img.shape

# kernel
MF = np.array(((0, 1, 0),
(1, 0, 1),
(0, 1, 0)), dtype=np.int)

# each dilate time
out = img.copy()
for i in range(Dil_time):
tmp = np.pad(out, (1, 1), 'edge')
for y in range(1, H+1):
for x in range(1, W+1):
if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255:
out[y-1, x-1] = 255

return out


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

# Otsu binary
## Grayscale
out = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
out = out.astype(np.uint8)

## Determine threshold of Otsu's binarization
max_sigma = 0
max_t = 0

for _t in range(1, 255):
v0 = out[np.where(out < _t)[0]]
m0 = np.mean(v0) if len(v0) > 0 else 0.
w0 = len(v0) / (H * W)
v1 = out[np.where(out >= _t)[0]]
m1 = np.mean(v1) if len(v1) > 0 else 0.
w1 = len(v1) / (H * W)
sigma = w0 * w1 * ((m0 - m1) ** 2)
if sigma > max_sigma:
max_sigma = sigma
max_t = _t

## Binarization
#print("threshold >>", max_t)
th = max_t
out[out < th] = 0
out[out >= th] = 255

# Morphology filter
MF = np.array(((0, 1, 0),
(1, 0, 1),
(0, 1, 0)), dtype=np.int)

# Morphology - erode
Erode_time = 1
erode = out.copy()

for i in range(Erode_time):
tmp = np.pad(out, (1, 1), 'edge')
for y in range(1, H+1):
for x in range(1, W+1):
if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4:
erode[y-1, x-1] = 0

# Morphology - dilate
Dil_time = 1
dilate = out.copy()

for i in range(Dil_time):
tmp = np.pad(out, (1, 1), 'edge')
for y in range(1, H+1):
for x in range(1, W+1):
if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255:
dilate[y-1, x-1] = 255

out = np.abs(erode - dilate) * 255

# Grayscale
gray = BGR2GRAY(img)

# Otsu's binarization
otsu = otsu_binarization(gray)

# Erode image
eroded = Erode(otsu)

# Delate image
dilated = Dilate(otsu)

# Morphology
out = np.abs(eroded - dilated) * 255

# Save result
cv2.imwrite("out.jpg", out)
Expand Down
Binary file modified Question_51_60/answers/answer_52.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
158 changes: 102 additions & 56 deletions Question_51_60/answers/answer_52.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,110 @@
import numpy as np
import matplotlib.pyplot as plt

# Gray scale
def BGR2GRAY(img):
b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()

# Gray scale
out = 0.2126 * r + 0.7152 * g + 0.0722 * b
out = out.astype(np.uint8)

return out

# Otsu Binalization
def otsu_binarization(img, th=128):
H, W = img.shape
out = img.copy()

max_sigma = 0
max_t = 0

# determine threshold
for _t in range(1, 255):
v0 = out[np.where(out < _t)]
m0 = np.mean(v0) if len(v0) > 0 else 0.
w0 = len(v0) / (H * W)
v1 = out[np.where(out >= _t)]
m1 = np.mean(v1) if len(v1) > 0 else 0.
w1 = len(v1) / (H * W)
sigma = w0 * w1 * ((m0 - m1) ** 2)
if sigma > max_sigma:
max_sigma = sigma
max_t = _t

# Binarization
print("threshold >>", max_t)
th = max_t
out[out < th] = 0
out[out >= th] = 255

return out


# Erosion
def Erode(img, Erode_time=1):
H, W = img.shape
out = img.copy()

# kernel
MF = np.array(((0, 1, 0),
(1, 0, 1),
(0, 1, 0)), dtype=np.int)

# each erode
for i in range(Erode_time):
tmp = np.pad(out, (1, 1), 'edge')
# erode
for y in range(1, H+1):
for x in range(1, W+1):
if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4:
out[y-1, x-1] = 0

return out


# Dilation
def Dilate(img, Dil_time=1):
H, W = img.shape

# kernel
MF = np.array(((0, 1, 0),
(1, 0, 1),
(0, 1, 0)), dtype=np.int)

# each dilate time
out = img.copy()
for i in range(Dil_time):
tmp = np.pad(out, (1, 1), 'edge')
for y in range(1, H+1):
for x in range(1, W+1):
if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255:
out[y-1, x-1] = 255

return out

# Opening morphology
def Morphology_Opening(img, time=1):
dil = Dilate(img, Dil_time=time)
erode = Erode(dil, Erode_time=time)
return erode

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

# Otsu binary
## Grayscale
out = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
out = out.astype(np.uint8)

## Determine threshold of Otsu's binarization
max_sigma = 0
max_t = 0

for _t in range(1, 255):
v0 = out[np.where(out < _t)]
m0 = np.mean(v0) if len(v0) > 0 else 0.
w0 = len(v0) / (H * W)
v1 = out[np.where(out >= _t)]
m1 = np.mean(v1) if len(v1) > 0 else 0.
w1 = len(v1) / (H * W)
sigma = w0 * w1 * ((m0 - m1) ** 2)
if sigma > max_sigma:
max_sigma = sigma
max_t = _t

## Binarization
#print("threshold >>", max_t)
th = max_t
out[out < th] = 0
out[out >= th] = 255

# Morphology filter
MF = np.array(((0, 1, 0),
(1, 0, 1),
(0, 1, 0)), dtype=np.int)

# Morphology - erode
Erode_time = 3
mor = out.copy()

for i in range(Erode_time):
tmp = np.pad(mor, (1, 1), 'edge')
for y in range(1, H+1):
for x in range(1, W+1):
if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4:
mor[y-1, x-1] = 0

# Morphology - dilate
Dil_time = 3

for i in range(Dil_time):
tmp = np.pad(mor, (1, 1), 'edge')
for y in range(1, H+1):
for x in range(1, W+1):
if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255:
mor[y-1, x-1] = 255

out = out - mor

# Grayscale
gray = BGR2GRAY(img)

# Otsu's binarization
otsu = otsu_binarization(gray)

# Opening process
opened = Morphology_Opening(otsu, time=3)

# Tophat
out = np.abs(otsu - opened) * 255

# Save result
cv2.imwrite("out.jpg", out)
Expand Down
Binary file modified Question_51_60/answers/answer_53.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit de8a396

Please sign in to comment.