如何使用OpenCV叠加图像和文本/形状?

4

我在使用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)来完成这个操作,但我已经有一个类似的命令用于动态覆盖,并且如果我在其上插入第二个命令,则无法正常工作。有任何想法如何解决这个问题吗?
提前感谢您的回答!
1个回答

2
您正在使用的命令是:cv2.addWeighted(overlay, 1.0, output, 0, 0, output),其中alpha = 1.0beta = 0,因此没有透明度。
基本上,您是在将overlay图像复制到output图像中。 AddWeighted文档:

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混合示例。
为了使代码自包含,代码使用:

  • ffmpeg-python 生成合成视频(用于测试)。
  • 该代码绘制了一个红色圆形,替换了'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()

结果(最后一帧):
在这里输入图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接