Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyoyo-yo committed Jan 24, 2019
1 parent 3d1c1be commit 61b4e34
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 6 deletions.
66 changes: 66 additions & 0 deletions Question_91_100/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Q. 91 - 100

## Q.91. K-meansによる減色処理 (Step.1) 色の距離によるクラス分類

*imori.jpg*をK-meansを用いた減色処理せよ。

減色処理はQ.6でも扱ったが、Q.6では予め決めた色に減色した。ここで扱うのはK-meansを用いて動的に減色する色を決定する。

アルゴリズムは,
1. 画像からランダムにK個のRGB成分をサンプリングする。(これをクラスと呼ぶことにする。)
2. 画像のそれぞれの画素に対して色の距離が最小となるクラスのインデックスを割り振る。

```bash
色の距離 dis = sqrt( (R-R')^2 + (G-G')^2 + (B-B')^2)
```
3. 各インデックスに対応する色成分の平均をRGBそれぞれに対して取り、新たなクラスとする。
4. 元のクラスと新しいクラスが全く同じならK-meansを終了する。そうでなければ、新しいクラスを元クラスとして2-3を繰り返す。
5. 元画像の各画素で色の距離が最小となるクラスのRGBを割り当てる。
ここでは1-2を実装せよ。
- クラス数はk=5とする
- ここでは画像をreshape((HxW, 3))にreshapeすると扱いやすくなる。
- 1においてはnp.random.seed(0)として、np.random.choice(np.arrange(画像のWxH), 5, replace=False)
- まずは3-5のループを考えずに実装せよ
解答では0-4にインデックスづけされたものをx50にして見やすいようにしている。
```bash
# 最初に選べれた色
[[140. 121. 148.]
[135. 109. 122.]
[211. 189. 213.]
[135. 86. 84.]
[118. 99. 96.]]
```
最初に選ばれた色との色の距離でクラスのインデックスをつけたもの(アルゴリズム2)。
|入力 (imori.jpg) |出力(answer_91.jpg)|
|:---:|:---:|
|![](imori.jpg)|![](answer_91.jpg)|
答え >> answer_91.py
## Q.92. K-meansによる減色処理 (Step.2) 減色処理
ここではアルゴリズム3-5も実装せよ。
```bash
# 選ばれた色
[[182.86730957 156.13246155 180.24510193]
[156.75152588 123.88993835 137.39085388]
[227.31060791 199.93135071 209.36465454]
[ 91.9105835 57.94448471 58.26378632]
[121.8759613 88.4736557 96.99688721]]
```
減色処理したもの。塗り絵イラスト風な画像にできる。k=10にすればある程度の色を保持しながらもイラスト風に減色できる。
また、k=5にして*madara.jpg*にも試してみよ。
|入力 (imori.jpg) |出力(answer_92.jpg)|k=10(answer_92_k10.jpg)| |入力2 (madara.jpg) |出力(answer_92_m.jpg) |
|:---:|:---:|:---:|:---:|:---:|
|![](imori.jpg)|![](answer_92.jpg)|![](answer_92_k10.jpg)|![](madara.jpg)|![](answer_92_m.jpg)|
答え >> answer_92.py
Binary file added Question_91_100/answer_91.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions Question_91_100/answer_91.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import cv2
import numpy as np
import matplotlib.pyplot as plt
from glob import glob

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

# k-Means
Class = 5

np.random.seed(0)

img = np.reshape(img, (H*W, -1))

i = np.random.choice(np.arange(H*W), Class, replace=False)
Cs = img[i].copy()

print(Cs)

clss = np.zeros((H*W), dtype=int)

for i in range(H*W):
dis = np.sum(np.abs(Cs - img[i]), axis=1)
clss[i] = np.argmin(dis)


out = np.reshape(clss, (H, W)) * 50
out = out.astype(np.uint8)

cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.waitKey(0)
Binary file added Question_91_100/answer_92.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions Question_91_100/answer_92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import cv2
import numpy as np
import matplotlib.pyplot as plt
from glob import glob

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

# k-Means
Class = 5

np.random.seed(0)

img = np.reshape(img, (H*W, -1))


i = np.random.choice(np.arange(H*W), Class, replace=False)
Cs = img[i].copy()


while True:

clss = np.zeros((H*W), dtype=int)

for i in range(H*W):
dis = np.sum(np.abs(Cs - img[i]), axis=1)
clss[i] = np.argmin(dis)

Cs_tmp = np.zeros((Class, 3))

for i in range(Class):
Cs_tmp[i] = np.mean(img[clss==i], axis=0)

if (Cs == Cs_tmp).all():
break
else:
Cs = Cs_tmp.copy()

out = np.zeros((H*W, 3), dtype=np.float32)

for i in range(Class):
out[clss == i] = Cs[i]

print(Cs)

out[out < 0] = 0
out[out > 255] = 255
out = np.reshape(out, (H, W, 3))
out = out.astype(np.uint8)

cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.waitKey(0)
Binary file added Question_91_100/answer_92_k10.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_91_100/answer_92_m.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_91_100/imori.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_91_100/madara.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ English is here >> https://github.com/KuKuXia/Image_Processing_100_Questions


## Recent
- あと14問なんじゃあああああああああああ
- あと8問なんじゃあああああああああああぁぁぁぁぁぁぁあぁぁぁぁぁあぁ
- **【New!】2019.1.24. Q.87 - 92 K-means を追加**
- **【New!】2019.1.24. Q.84 - 86 簡単な画像認識 を追加**
- **【New!】2019.1.23. Q.81 - 83 コーナー検出 を追加**
- **【New!】2019.1.23. Q.77 - 80 ガボールフィルタ を追加**
Expand Down Expand Up @@ -225,11 +226,18 @@ $ python sample.py

|番号|問題||番号|問題|
|:---:|:---:|:---:|:---:|:---:|
| 81 | Hessianのコーナー検出 | | 86 | 簡単な画像認識 (Step.3) 評価
| 82 | Harrisのコーナー検出 (Step.1) Sobel + Gaussian
| 83 | Harrisのコーナー検出 (Step.2) コーナー検出
| 84 | 簡単な画像認識 (Step.1) 減色化 + ヒストグラム
| 85 | 簡単な画像認識 (Step.2) クラス判別
| 81 | Hessianのコーナー検出 | | 86 | 簡単な画像認識 (Step.3) 評価(Accuracy) |
| 82 | Harrisのコーナー検出 (Step.1) Sobel + Gaussian | | 87 | 簡単な画像認識 (Step.4) k-NN |
| 83 | Harrisのコーナー検出 (Step.2) コーナー検出 | | 88 | K-means (Step.1) 重心作成 |
| 84 | 簡単な画像認識 (Step.1) 減色化 + ヒストグラム | | 89 | K-means (Step.2) クラスタリング |
| 85 | 簡単な画像認識 (Step.2) クラス判別 | | 90 | K-means (Step.3) 初期ラベルの変更 |

### [問題91 - 100](https://github.com/yoyoyo-yo/Gasyori100knock/tree/master/Question_91_100)

|番号|問題||番号|問題|
|:---:|:---:|:---:|:---:|:---:|
| 91 | K-meansによる減色処理 (Step.1) 色の距離によるクラス分類
| 92 | K-meansによる減色処理 (Step.2) 減色処理

## TODO

Expand Down

0 comments on commit 61b4e34

Please sign in to comment.