forked from gzr2017/ImageProcessing100Wen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
166 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)| | ||
|:---:|:---:| | ||
||| | ||
答え >> 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) | | ||
|:---:|:---:|:---:|:---:|:---:| | ||
|||||| | ||
答え >> answer_92.py |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters