[英]How to use OpenCV to overlay an image AND text/shapes?
我遇到了一個關於 OpenCV 透明覆蓋的問題。 到目前為止,這是我的代碼:
import cv2
cap = cv2.VideoCapture('Sample_Vid.mp4')
stat_overlay = cv2.imread('overlay.png')
fps = 21
if cap.isOpened():
while cap.isOpened():
ret, frame = cap.read()
overlay = frame.copy()
output = frame.copy()
cv2.rectangle(overlay, (0, 0), (730, 50), (0, 0, 0), -1)
cv2.putText(overlay, fps, (1230, 20), cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255), 1)
cv2.addWeighted(overlay, 1.0, output, 0, 0, output)
cv2.imshow('frame', output)
所以我的框架有一個矩形,上面顯示了 FPS。 現在我想首先覆蓋我的 stat_overlay 圖像,然后覆蓋文本和形狀,因為它們是動態的。 在我讀過的每一個解釋中,都被告知使用cv2.addWeighted(stat_overlay, 1.0, output, 0, 0, output)
,但我已經有一個類似動態疊加層使用的命令,如果我在它上面插入第二個命令, 這是行不通的。 任何想法如何解決這個問題?
感謝您提前回答!
您正在使用的命令: cv2.addWeighted(overlay, 1.0, output, 0, 0, output)
,使用alpha = 1.0
和beta = 0
,因此沒有透明度。
您基本上是將overlay
圖像復制到output
圖像中。
cv2.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]])
src1 – 第一個輸入數組。
alpha - 第一個數組元素的權重。
src2 – 與 src1 具有相同大小和通道編號的第二個輸入數組。
beta – 第二個數組元素的權重。
dst – 與輸入數組具有相同大小和通道數的輸出數組。
您還可以使用以下代碼來覆蓋文本:
output = frame.copy()
cv2.rectangle(output, (0, 0), (730, 50), (0, 0, 0), -1)
cv2.putText(output, fps, (1230, 20), cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255), 1)
對於覆蓋stat_overlay
您可以使用諸如Alpha 混合代碼示例之類的解決方案。
我不知道'overlay.png'
是 RGB 還是 RGBA 格式。
如果圖像具有 Alpha 通道,您可以將其用作透明平面。
如果圖像是 RGB,您可以創建所需的 alpha 平面。
如果'overlay.png'
是小圖像(如徽標),您可能不需要任何這些,您可以將小圖像“放置”在output
圖像上。
我創建了一個自包含的代碼示例,它基於alpha 混合示例。
為了使代碼自包含,代碼使用:
'overlay.png'
這是代碼:
import ffmpeg
import cv2
import numpy as np
in_filename = 'Sample_Vid.mp4' # Input file for testing (".264" or ".h264" is a convention for elementary h264 video stream file)
## Build synthetic video, for testing:
################################################
# ffmpeg -y -r 10 -f lavfi -i testsrc=size=192x108:rate=1 -c:v libx264 -crf 23 -t 50 test_vid.264
width, height = 640, 480
(
ffmpeg
.input('testsrc=size={}x{}:rate=1'.format(width, height), f='lavfi')
.output(in_filename, vcodec='libx264', crf=23, t=5)
.overwrite_output()
.run()
)
################################################
cap = cv2.VideoCapture('Sample_Vid.mp4')
#stat_overlay = cv2.imread('overlay.png')
# Create image with green circle, instead of reaing a file
# The image is created as RGBA (the 4'th plane is the transparency).
stat_overlay = np.zeros((height, width, 4), np.uint8)
cv2.circle(stat_overlay, (320, 240), 80, (0, 0, 255, 255), thickness=20) # Draw red circle (with alpha = 255)
# https://www.learnopencv.com/alpha-blending-using-opencv-cpp-python/
stat_alpha = stat_overlay[:, :, 3] # Take 4'th plane as alpha channel
stat_alpha = cv2.cvtColor(stat_alpha, cv2.COLOR_GRAY2BGR) # Duplicate alpha channel 3 times (to match output dimensions)
# https://www.learnopencv.com/alpha-blending-using-opencv-cpp-python/
# Normalize the alpha mask to keep intensity between 0 and 1
stat_alpha = stat_alpha.astype(float) / 255
stat_overlay = stat_overlay[:, :, 0:3] # Get RGB channels
fps = 21
if cap.isOpened():
while cap.isOpened():
ret, frame = cap.read()
if ret:
output = frame.copy()
# https://www.learnopencv.com/alpha-blending-using-opencv-cpp-python/
# Alpha blending:
foreground = stat_overlay.astype(float)
background = output.astype(float)
# Multiply the foreground with the alpha matte
foreground = cv2.multiply(stat_alpha, foreground)
# Multiply the background with ( 1 - alpha )
background = cv2.multiply(1.0 - stat_alpha, background)
# Add the masked foreground and background.
output = cv2.add(foreground, background).astype(np.uint8)
cv2.rectangle(output, (0, 0), (230, 50), (0, 0, 0), -1)
cv2.putText(output, str(fps), (123, 20), cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255), 1)
cv2.imshow('frame', output)
cv2.waitKey(1000)
else:
break
cv2.destroyAllWindows()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.