teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

d

2019/05/21 09:54

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -13,20 +13,21 @@
13
13
  import cv2
14
14
  import numpy as np
15
15
 
16
- input_dir = Path('input') # 入力ディレクトリのパス、画像以外のファイルがない前提
16
+ input_dir = Path('input')
17
17
  output_dir = Path('output')
18
18
  output_dir.mkdir(exist_ok=True)
19
19
 
20
- before = (0, 0, 255) # 置換する色 (blue, green, red) の順
20
+ before = (0, 0, 255)
21
- after = (0, 0, 1) # 置換後の色 (blue, green, red) の順
21
+ after = (0, 0, 1)
22
22
 
23
23
  for img_path in input_dir.iterdir():
24
24
  # 画像を読み込む。
25
25
  img = cv2.imread(str(img_path))
26
26
 
27
- # を置換する。
27
+ # (0, 0, 255) (1, 0, 0) に置換する。
28
28
  idx_to_replace = (img == before).all(axis=2)
29
29
  img[idx_to_replace] = after
30
+ img[~idx_to_replace] = (0, 0, 0)
30
31
 
31
32
  # 保存する。
32
33
  save_path = output_dir / img_path.name

1

d

2019/05/21 09:54

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -13,7 +13,7 @@
13
13
  import cv2
14
14
  import numpy as np
15
15
 
16
- input_dir = Path('input')
16
+ input_dir = Path('input') # 入力ディレクトリのパス、画像以外のファイルがない前提
17
17
  output_dir = Path('output')
18
18
  output_dir.mkdir(exist_ok=True)
19
19