2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

政見放送動画をIPPONグランプリ風にしてみた(OpenCV:Python版)

Last updated at Posted at 2019-11-22

きっかけ

動画内の文字を抽出しようと二値化してみたところ、背景が自然環境下ではなくクリアで何も写っていないためか人物とテロップのみが抽出された訳ですが、、、背景色黄色にしたらIPPONグランプリっぽくなるんじゃないかと思い実装してみました。

開発

import cv2
import numpy as np

if __name__ == '__main__':

	cap = cv2.VideoCapture('one_minutes.mp4')

	cap_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
	cap_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
	fps = cap.get(cv2.CAP_PROP_FPS)

	telop_height = 50

	fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
	writer = cv2.VideoWriter('threshold_raw.mp4',fourcc, fps, (cap_width, cap_height + telop_height))

	kernel = np.ones((3,3),np.uint8)

	count = 0
	try :
		while True:
			if not cap.isOpened():
				break

			if cv2.waitKey(1) & 0xFF == ord('q'):
				break

			ret, frame = cap.read()

			if frame is None:
				break

			frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
			ret2, frame = cv2.threshold(frame, 0, 255, cv2.THRESH_OTSU)

			#収縮 -> 膨張
			frame = cv2.morphologyEx(frame, cv2.MORPH_OPEN, kernel)

			background = np.zeros((cap_height, cap_width, 3), np.uint8)
			background[:] = tuple((80,235,247))

			telop = np.zeros((telop_height, cap_width, 3), np.uint8)
			telop[:] = tuple((128,128,128))

			#一旦モノクロ(dim=1)からカラー(dim=3)に変換
			frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
			#背景色と合成
			frame = cv2.bitwise_and(frame, background)

			images = [frame, telop]

			frame = np.concatenate(images, axis=0)
			font = cv2.FONT_HERSHEY_SIMPLEX
			cv2.putText(frame, "{:.4f} [sec]".format(round(count/fps, 4)), 
						(cap_width - 250, cap_height + telop_height - 10), 
						font, 
						1, 
						(0, 0, 255), 
						2, 
						cv2.LINE_AA)

			writer.write(frame)
			count += 1

	except cv2.error as e:
		print(e)	

	writer.release()
	cap.release()

補足

ret2, frame = cv2.threshold(frame, 0, 255, cv2.THRESH_OTSU)
で二値化しています。 ここでは大津のアルゴリズムを使っています。

frame = cv2.morphologyEx(frame, cv2.MORPH_OPEN, kernel)
では二値化した画像から収縮 -> 膨張して収縮の段階で白い斑点が黒く塗りつぶされてノイズが除去されます。


background = np.zeros((cap_height, cap_width, 3), np.uint8)
			background[:] = tuple((80,235,247))
background[:] = tuple((80,235,247))

# 一旦モノクロ(dim=1)からカラー(dim=3)に変換
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
# 背景色と合成
frame = cv2.bitwise_and(frame, background)

では黄色の単一背景色を作成し、二値化した画像とbitwise_andで合成しています。

結果

オープニング処理なし(加工なし)

threshold_raw.gif

オープニング処理あり

threshold_open.gif

少しわかりにくいかもしれませんが、上の政党名をみていると収縮縮小をした方がノイズが消えているのがわかると思います。

おわりに

割ときれいにオブジェクトを分離することができたので、次回は文字を抽出してみようと思います。

参考にしてみたリンク

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?