使用Python和OpenCV實現(xiàn)動態(tài)背景的畫中畫效果
在本文中,我們將通過一個詳細的Python腳本,使用OpenCV庫來為視頻添加動態(tài)背景。這個腳本將指導你如何讀取兩個視頻文件,一個作為前景,另一個作為背景,并將它們合成一個視頻,其中前景視頻的特定區(qū)域將顯示背景視頻的內容。
在視頻上加入背景動態(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ù)(如果需要截斷背景視頻)
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] # 獲取幀的高和寬
# 定義黑色實心矩形的尺寸變量
top_bar_height = int(height * 0.30) # 頂部黑色實心矩形的高度設置為視頻總高度的30%
bottom_bar_height = int(height * 0.15) # 底部黑色實心矩形的高度設置為視頻總高度的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坐標
mask_y = mask_top_margin # 蒙版中心y坐標
# 創(chuàng)建蒙版
mask = np.zeros((height, width, 3), dtype=np.uint8) # 創(chuàng)建一個全黑的蒙版
cv2.rectangle(mask, (mask_x, mask_y), (mask_x + mask_width, mask_y + mask_height), (255, 255, 255), -1) # 在蒙版中繪制一個白色矩形
# 定義視頻寫入器
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 定義視頻編碼格式
out = cv2.VideoWriter(output_video_path, fourcc, 20.0, (width, height)) # 創(chuàng)建視頻寫入對象
# 背景視頻幀計數(shù)器
background_frame_index = 0
# 遍歷視頻幀
while cap.isOpened():
ret, frame = cap.read() # 讀取前景視頻的當前幀
if not ret:
break # 如果讀取結束,跳出循環(huán)
# 在視頻幀頂部繪制黑色實心矩形
cv2.rectangle(frame, (0, 0), (width, top_bar_height), (0, 0, 0), -1)
# 在視頻幀底部繪制黑色實心矩形
cv2.rectangle(frame, (0, height - bottom_bar_height), (width, height), (0, 0, 0), -1)
# 將蒙版應用到幀上
masked_frame = cv2.bitwise_and(frame, mask)
# 讀取背景視頻的當前幀
background_ret, background_frame = background_cap.read()
if not background_ret or background_frame_index >= max_background_frames:
# 如果背景視頻讀取失敗或達到最大幀數(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))
# 調整背景幀的透明度
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)
# 將背景幀與蒙版的反向進行混合
masked_background = cv2.bitwise_and(background_frame, inv_mask)
# 將兩個混合結果相加
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時運行較慢。
結尾與思考
通過上述代碼,我們成功地為一個視頻添加了動態(tài)背景,同時在視頻的頂部和底部添加了黑色實心矩形。這個技術可以廣泛應用于視頻編輯和特效制作中,例如制作新聞節(jié)目的背景、電影特效或者增強視頻的視覺效果。
這個項目也展示了Python和OpenCV在視頻處理方面的強大能力。通過簡單的代碼,我們就能夠實現(xiàn)復雜的視頻合成效果。這不僅為視頻制作人員提供了便利,也為愛好者提供了一個學習和實驗的平臺。
在未來,我們可以探索更多的視頻處理技術,比如使用深度學習來自動替換視頻中的背景,或者開發(fā)更復雜的特效來增強視頻內容。隨著技術的不斷進步,視頻處理的邊界也在不斷擴展,為我們提供了無限的可能性。
到此這篇關于使用Python和OpenCV實現(xiàn)動態(tài)背景的畫中畫效果的文章就介紹到這了,更多相關Python OpenCV動態(tài)背景內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于Python的微信機器人開發(fā) 微信登錄和獲取好友列表實現(xiàn)解析
這篇文章主要介紹了Python微信機器人開發(fā) 微信登錄和獲取好友列表實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08
python+playwright在線安裝和離線安裝詳細教程
Playwright是微軟在2020年初開源自動化測試工具,功能和selenium類似,都可以驅動瀏覽器進行各種自動化操作,這篇文章主要介紹了python+playwright在線安裝和離線安裝的相關資料,需要的朋友可以參考下2019-01-01
用Python自動清理電腦內重復文件,只要10行代碼(自動腳本)
這篇文章主要介紹了用Python自動清理電腦內重復文件,只要10行代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01

