I tried to make the political broadcast video like IPPON Grand Prix (OpenCV: Python version)

Trigger

When I tried to binarize the characters in the video, only the person and the telop were extracted probably because the background was not in the natural environment and nothing was reflected, but if the background color was yellow I thought it would look like an IPPON Grand Prix, so I implemented it.

development of

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)

			#Shrink->expansion
			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))

			#Once monochrome(dim=1)From color(dim=3)Conversion to
			frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
			#Background color and composition
			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()

Supplement

ret2, frame = cv2.threshold(frame, 0, 255, cv2.THRESH_OTSU) It is binarized with. Here, Otsu's algorithm is used.

frame = cv2.morphologyEx(frame, cv2.MORPH_OPEN, kernel) Now, from the binarized image, shrink-> expand and the white spots are filled with black at the stage of contraction to remove noise.


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

#Once monochrome(dim=1)From color(dim=3)Conversion to
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
#Background color and composition
frame = cv2.bitwise_and(frame, background)

Creates a single yellow background color and composites it with the binarized image with bitwise_and.

result

No opening process (no processing)

threshold_raw.gif

With opening process

threshold_open.gif

It may be a little confusing, but if you look at the names of the parties above, you can see that the noise disappears when the contraction is reduced.

in conclusion

I was able to separate the objects fairly cleanly, so next time I will try to extract the characters.

Link I tried to refer to

-opencv-python opening (Opening) -Opencv-python Bit unit processing -IPPON Grand Prix

Recommended Posts

I tried to make the political broadcast video like IPPON Grand Prix (OpenCV: Python version)
I tried to display the video playback time (OpenCV: Python version)
I tried face recognition from the video (OpenCV: python version)
I tried to make an image similarity function with Python + OpenCV
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
I tried "binarizing" the image with Python + OpenCV
[Python] Smasher tried to make the video loading process a function using a generator
I tried to graph the packages installed in Python
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
Continuation ・ I tried to make Slackbot after studying Python3
[Python] I tried to graph the top 10 eyeshadow rankings
I tried to solve the problem with Python Vol.1
Python OpenCV tried to display the image in text.
I tried to summarize the string operations of Python
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
(Python: OpenCV) I tried to output a value indicating the distance between regions while binarizing the video in real time.
I tried "gamma correction" of the image with Python + OpenCV
I tried to make various "dummy data" with Python faker
[Python] I tried to visualize the follow relationship of Twitter
I tried to implement the mail sending function in Python
I tried to enumerate the differences between java and python
I tried to make a stopwatch using tkinter in python
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to divide the file into folders with Python
I tried to make it possible to automatically send an email just by double-clicking the [Python] icon
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
[Python] I tried to make a simple program that works on the command line using argparse.
I tried to solve the ant book beginner's edition with python
I want to read the html version of "OpenCV-Python Tutorials" OpenCV 3.1 version
[Python] I tried to implement stable sorting, so make a note
I tried to make a regular expression of "time" using Python
[3rd] I tried to make a certain authenticator-like tool with python
I tried to process the image in "sketch style" with OpenCV
I tried to make a regular expression of "date" using Python
I tried to digitize the stamp stamped on paper using OpenCV
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to process the image in "pencil style" with OpenCV
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[Python] Simple Japanese ⇒ I tried to make an English translation tool
I tried to cut out a still image from the video
[1st] I tried to make a certain authenticator-like tool with python
I tried to improve the efficiency of daily work with Python
I tried to touch Python (installation)
How to get the Python version
I tried to move the ball
I tried to estimate the interval.
I tried to make it possible to automatically send an email just by double-clicking the [GAS / Python] icon
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
Python: I tried to make a flat / flat_map just right with a generator
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
When I tried to run Python, it was skipped to the Microsoft Store
Python -I tried to restore the dictionary comprehensive notation to its original form-
I tried to solve the 2020 version of 100 language processing [Chapter 3: Regular expressions 25-29]
[Python] I tried to analyze the pitcher who achieved no hit no run
[AWS] [GCP] I tried to make cloud services easy to use with Python
I tried to get the authentication code of Qiita API with Python.