やったこと
Qiitaの記事を参考にプログラムを組み直しました.
具体的には,Qiitaではイベントカメラの動画がカラーになっていますが,
動いた箇所を白一色で表示し,それ以外は黒色で表示しようとしています.
なので,画像をグレースケールに変換後,二値化による処理をしたいのですがうまくいきません.
下の該当箇所である,
python
1 b_img = cv2.cvtColor(b_img, cv2.COLOR_BGR2GRAY)
をコメントアウトすると二値化した動画を作成できます.
しかしコメントアウトをしないと開けない動画が作成されるためここが原因だと考えています.
python
1import numpy as np 2import cv2 3 4# th= 閾値 5def img2event_image(base_img, img, th = 30, plus = 1, minus = -1): 6 img3 = base_img.astype(float) - img.astype(float) 7 8 index1 = img3 > th 9 img3[index1] = plus 10 11 index2 = img3 < -1 * th 12 img3[index2] = minus 13 14 img3[~(index1) & ~(index2)] = 0 15 16 return img3.astype(np.uint8) 17 18 19def __main(): 20 path = "./sample.mp4" 21 output_name = "./output.mp4" 22 23 cap = cv2.VideoCapture(path) 24 fourcc = cv2.VideoWriter_fourcc(*"mp4v") 25 w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 26 h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 27 fps = 30 28 out = cv2.VideoWriter(output_name, fourcc, fps, (w, h)) 29 30 success, image = cap.read() 31 base_image = image[:] 32 33 while success: 34 b_img = img2event_image(base_image.copy(), image.copy()) 35 36 b_img = cv2.cvtColor(b_img, cv2.COLOR_BGR2GRAY)#該当の箇所 37 b_img = cv2.threshold(b_img,85,255,cv2.THRESH_BINARY_INV)[1]#2値化で白黒にする 38 39 out.write(b_img) 40 base_image = image[:] 41 # 1フレームずつ取得する 42 success, image = cap.read() 43 44 print("done") 45 cap.release() 46 cv2.destroyAllWindows() 47 48if __name__ == '__main__': 49 50 __main()
どのようにプログラムを変更すればよろしいでしょうか?
よろしくお願いいたします.
あなたの回答
tips
プレビュー