opencv给视频加字幕加炫光


前言

模式识别视觉基础(视频处理知识) 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"

# Define the codec and create VideoWriter object
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

# Release everything if job is finished
cap.release()
videoWriter.release()
cv2.destroyAllWindows()

二、结果展示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


参考链接:

https://blog.csdn.net/weixin_45861496/article/details/124224815?spm=1001.2014.3001.5502

猜你喜欢

转载自blog.csdn.net/qq_50492541/article/details/124560016