簡體   English   中英

如何使用 Python 中的 OpenCv 將圖像添加到視頻中?

[英]How to add an image to a video using OpenCv in Python?

我試圖通過修改我在網上找到的代碼(用於合並兩張圖片),使用 OpenCv 為我的視頻添加徽標。 該過程以某種方式起作用,但添加的徽標僅顯示了四分之一,而不是完整的。 所以我的問題是:

  • 如何更改添加徽標的大小和裁剪?
  • 是否也可以更改視頻上的 position(如底部)?

任何幫助或文檔(對於初學者)將不勝感激。

我使用的代碼:

import cv2
import numpy as np
 
foreground = cv2.imread('logo.jpg')
cap = cv2.VideoCapture("video.mp4")
alpha = 0.4
while True:
    ret, background = cap.read()
    background = cv2.flip(background,1)
    added_image = cv2.addWeighted(background[150:250,150:250,:],alpha,foreground[0:100,0:100,:],1-alpha,0)
    background[150:250,150:250] = added_image
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(background,'alpha:{}'.format(alpha),(10,30), font, 1,(255,255,255),2,cv2.LINE_AA)
    cv2.imshow('a',background)
    k = cv2.waitKey(10)
    if k == ord('q'):
        break
    if k == ord('a'):
        alpha +=0.1
        if alpha >=1.0:
            alpha = 1.0
    elif k== ord('d'):
        alpha -= 0.1
        if alpha <=0.0:
            alpha = 0.0
cap.release()
cv2.destroyAllWindows()

我在你的代碼中添加了注釋和一些易於使用的東西,以幫助更容易理解每個部分應該做什么。 整體結構是相同的,但希望這可以幫助您弄清楚為什么覆蓋看起來是這樣的。

import cv2
import numpy as np
 
# load image and start video
foreground = cv2.imread('logo.jpg')
cap = cv2.VideoCapture("video.mp4")

# init font
font = cv2.FONT_HERSHEY_SIMPLEX

# position of logo on video
top_left = [0, 0] # the top-left corner of your logo goes here
tx = top_left[0] # less typing later
ty = top_left[1]

# crop of logo
left = 0
right = 100
top = 0 # y = 0 is the top of the image
bottom = 100

# calculate width and height of logo crop
width = right - left
height = bottom - top

# main loop
alpha = 0.4
while True:
    # read image
    ret, background = cap.read()

    # horizontal flip to create mirror image
    background = cv2.flip(background,1)

    # quick primer on 2d image slicing:
    # images are [y,x]
    # crop = image[0:100, 300:500] will be a 200x100 image (width x height) 
    # the logo slice should give you a decent idea of what's going on

    # WARNING: there are no out-of-bounds checks here, make sure that
    # tx + width is less than the width of the background
    # ty + height is less than the height of the background
    # right is less than the width of the foreground
    # bottom is less than the height of the foreground

    # get crops of background and logo
    background_slice = background[ty:ty+height, tx:tx+width]; 
    logo_slice = foreground[top:bottom, left:right]; 

    # since we don't change our crop, we could do the logo slice outside the loop
    # but I'll keep it here for clarity

    # add slice of logo onto slice of background
    added_image = cv2.addWeighted(background_slice,alpha,logo_slice,1-alpha,0)
    background[ty:ty+height, tx:tx+width] = added_image

    # draw alpha value
    cv2.putText(background,'alpha:{}'.format(alpha),(10,30), font, 1,(255,255,255),2,cv2.LINE_AA)

    # show image
    cv2.imshow('a',background)
    k = cv2.waitKey(10)

    # process keypresses
    if k == ord('q'):
        break
    if k == ord('a'):
        alpha +=0.1
        if alpha >=1.0:
            alpha = 1.0
    elif k== ord('d'):
        alpha -= 0.1
        if alpha <=0.0:
            alpha = 0.0

# close
cap.release()
cv2.destroyAllWindows()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM