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.
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()
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
.
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.
I was able to separate the objects fairly cleanly, so next time I will try to extract the characters.
-opencv-python opening (Opening) -Opencv-python Bit unit processing -IPPON Grand Prix