欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python和OpenCV實(shí)現(xiàn)動(dòng)態(tài)背景的畫中畫效果

 更新時(shí)間:2024年11月20日 10:59:53   作者:黑金IT  
這篇文章將通過一個(gè)詳細(xì)的Python腳本,使用OpenCV庫來為視頻添加動(dòng)態(tài)背景,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

在本文中,我們將通過一個(gè)詳細(xì)的Python腳本,使用OpenCV庫來為視頻添加動(dòng)態(tài)背景。這個(gè)腳本將指導(dǎo)你如何讀取兩個(gè)視頻文件,一個(gè)作為前景,另一個(gè)作為背景,并將它們合成一個(gè)視頻,其中前景視頻的特定區(qū)域?qū)@示背景視頻的內(nèi)容。

在視頻上加入背景動(dòng)態(tài)的畫中畫。

代碼解析與注釋

# 視頻文件路徑
input_video_path = 'sc/input_video.mp4'  # 輸入視頻文件路徑
output_video_path = 'output_video_bg03.mp4'  # 輸出視頻文件路徑
background_video_path = 'sc/bg_03b.mp4'  # MP4視頻背景文件路徑

# 背景透明度(0.0 完全透明,1.0 完全不透明)
background_opacity = 0.6

# 背景視頻的最大幀數(shù)(如果需要截?cái)啾尘耙曨l)
max_background_frames = 1000

# 讀取視頻文件
cap = cv2.VideoCapture(input_video_path)  # 讀取前景視頻
background_cap = cv2.VideoCapture(background_video_path)  # 讀取背景視頻

# 獲取視頻幀的尺寸
ret, frame = cap.read()  # 讀取第一幀
if not ret:
    print("無法讀取視頻文件")
    raise Exception("無法讀取視頻文件")  # 如果無法讀取,拋出異常

height, width = frame.shape[:2]  # 獲取幀的高和寬

# 定義黑色實(shí)心矩形的尺寸變量
top_bar_height = int(height * 0.30)  # 頂部黑色實(shí)心矩形的高度設(shè)置為視頻總高度的30%
bottom_bar_height = int(height * 0.15)  # 底部黑色實(shí)心矩形的高度設(shè)置為視頻總高度的15%

# 定義蒙版的位置和大小
mask_top_margin = top_bar_height  # 蒙版頂部距離
mask_bottom_margin = bottom_bar_height  # 蒙版底部距離
mask_height = height - mask_top_margin - mask_bottom_margin  # 蒙版高度
mask_width = width  # 蒙版寬度
mask_x = 0  # 蒙版中心x坐標(biāo)
mask_y = mask_top_margin  # 蒙版中心y坐標(biāo)

# 創(chuàng)建蒙版
mask = np.zeros((height, width, 3), dtype=np.uint8)  # 創(chuàng)建一個(gè)全黑的蒙版
cv2.rectangle(mask, (mask_x, mask_y), (mask_x + mask_width, mask_y + mask_height), (255, 255, 255), -1)  # 在蒙版中繪制一個(gè)白色矩形

# 定義視頻寫入器
fourcc = cv2.VideoWriter_fourcc(*'mp4v')  # 定義視頻編碼格式
out = cv2.VideoWriter(output_video_path, fourcc, 20.0, (width, height))  # 創(chuàng)建視頻寫入對(duì)象

# 背景視頻幀計(jì)數(shù)器
background_frame_index = 0

# 遍歷視頻幀
while cap.isOpened():
    ret, frame = cap.read()  # 讀取前景視頻的當(dāng)前幀
    if not ret:
        break  # 如果讀取結(jié)束,跳出循環(huán)

    # 在視頻幀頂部繪制黑色實(shí)心矩形
    cv2.rectangle(frame, (0, 0), (width, top_bar_height), (0, 0, 0), -1)
    # 在視頻幀底部繪制黑色實(shí)心矩形
    cv2.rectangle(frame, (0, height - bottom_bar_height), (width, height), (0, 0, 0), -1)
    # 將蒙版應(yīng)用到幀上
    masked_frame = cv2.bitwise_and(frame, mask)
    # 讀取背景視頻的當(dāng)前幀
    background_ret, background_frame = background_cap.read()
    if not background_ret or background_frame_index >= max_background_frames:
        # 如果背景視頻讀取失敗或達(dá)到最大幀數(shù),重新開始背景視頻
        background_cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
        background_frame_index = 0
        background_ret, background_frame = background_cap.read()

    # 確保背景幀的尺寸與視頻幀匹配
    background_frame = cv2.resize(background_frame, (width, height))
    # 調(diào)整背景幀的透明度
    background_frame = background_frame.astype(float) * background_opacity
    background_frame = np.clip(background_frame, 0, 255).astype(np.uint8)

    # 創(chuàng)建反向蒙版
    inv_mask = cv2.bitwise_not(mask)
    # 將背景幀與蒙版的反向進(jìn)行混合
    masked_background = cv2.bitwise_and(background_frame, inv_mask)
    # 將兩個(gè)混合結(jié)果相加
    result_frame = cv2.add(masked_frame, masked_background)
    # 保存幀
    out.write(result_frame)
    # 更新背景視頻幀索引
    background_frame_index += 1

# 釋放資源
cap.release()
background_cap.release()
out.release()
cv2.destroyAllWindows()

注意

以上是通過cv2經(jīng)測式,如何視頻文件大于10M時(shí)運(yùn)行較慢。

結(jié)尾與思考

通過上述代碼,我們成功地為一個(gè)視頻添加了動(dòng)態(tài)背景,同時(shí)在視頻的頂部和底部添加了黑色實(shí)心矩形。這個(gè)技術(shù)可以廣泛應(yīng)用于視頻編輯和特效制作中,例如制作新聞節(jié)目的背景、電影特效或者增強(qiáng)視頻的視覺效果。

這個(gè)項(xiàng)目也展示了Python和OpenCV在視頻處理方面的強(qiáng)大能力。通過簡單的代碼,我們就能夠?qū)崿F(xiàn)復(fù)雜的視頻合成效果。這不僅為視頻制作人員提供了便利,也為愛好者提供了一個(gè)學(xué)習(xí)和實(shí)驗(yàn)的平臺(tái)。

在未來,我們可以探索更多的視頻處理技術(shù),比如使用深度學(xué)習(xí)來自動(dòng)替換視頻中的背景,或者開發(fā)更復(fù)雜的特效來增強(qiáng)視頻內(nèi)容。隨著技術(shù)的不斷進(jìn)步,視頻處理的邊界也在不斷擴(kuò)展,為我們提供了無限的可能性。

到此這篇關(guān)于使用Python和OpenCV實(shí)現(xiàn)動(dòng)態(tài)背景的畫中畫效果的文章就介紹到這了,更多相關(guān)Python OpenCV動(dòng)態(tài)背景內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論