前言
模式识别视觉基础(视频处理知识) OpenCV应用 python 3.6+ 安装组件 pip install matplotlib numpy opencv-python pillow
要求: 从网上下载或自己手机录制一段视频(>30秒),第0-5秒显示一句话的字幕,第6-15秒显示另一句话的字幕。 第20秒开始从屏幕中心出现一个光点,发出眩光,逐渐扩大覆盖的整个屏幕(类似太阳),最后光点缩小复原,整个过程10秒。
一、代码实现
import cv2
import math
import numpy as np
org_video = "Screen-univers.mp4"
sub_video = "Screenrecorder_U.mp4"
cap = cv2.VideoCapture(org_video)
fps_video = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
videoWriter = cv2.VideoWriter(sub_video, fourcc, fps_video, (width, height))
text1='The scenery is infinitely good'
text2='And things need to be done early !'
def show_text(img,text,word_x):
word_y = int(height) - 80
position=(word_x, word_y)
font=cv2.FONT_HERSHEY_SIMPLEX
font_size= 3
color=(0, 0, 255)
A = 3
return cv2.putText(img, text, position, font, font_size, color, A)
def show_glare(img,time,count):
centerX = height / 2
centerY = width / 2
radius = int(((height/2)/time)*count)
strength = 200
for i in range(height):
for j in range(width):
distance = math.pow((centerY - j), 2) + math.pow((centerX - i), 2)
B = img[i, j][0]
G = img[i, j][1]
R = img[i, j][2]
if (distance < radius * radius):
result = (int)(strength * (1.0 - math.sqrt(distance) / radius))
B = img[i, j][0] + result
G = img[i, j][1] + result
R = img[i, j][2] + result
B = min(255, max(0, B))
G = min(255, max(0, G))
R = min(255, max(0, R))
img[i, j] = np.uint8((B, G, R))
else:
img[i, j] = np.uint8((B, G, R))
glare_time = int(fps_video*5)-1
glare_count = 0
frame_id = 0
while (cap.isOpened()):
ret, frame = cap.read()
if ret == True:
frame_id +=1
time_s = int(frame_id / fps_video)
if (0 < time_s <= 5):
show_text(frame,text1,500)
elif (6 < time_s <= 15):
show_text(frame,text2,350)
elif (20 < time_s <= 25):
glare_count += 1
show_glare(frame,glare_time,glare_count)
elif (25 < time_s <= 30):
glare_count -= 1
show_glare(frame,glare_time,glare_count)
videoWriter.write(frame)
else:
break
cap.release()
videoWriter.release()
cv2.destroyAllWindows()
二、结果展示
参考链接:
https://blog.csdn.net/weixin_45861496/article/details/124224815?spm=1001.2014.3001.5502
|