如何在 opencv python 中为网络摄像头视频提供透明边框?

How to give transparent border to webcam video in opencv python?

我尝试了一些方法,但都不适合我。 下面是我的代码

cv2.copyMakeBorder(img,30,30,10,10,cv2.BORDER_CONSTANT,value=[555,555,555,0])

这段代码显示了边框,最后一个 0 没有任何意义。还有其他方法可以做到这一点吗?

感谢您的关注。期待您的回复。

这对我有用:)

import numpy as np
import cv2
from tkinter import *
#import tkinter as tk
from PIL import Image, ImageTk
import sys

window = Tk()  #Makes main window
window.overrideredirect(True)
window.wm_attributes("-topmost", True)
window.geometry("+600+200")
display1 = Label(window)
display1.grid(row=1, column=0, padx=0, pady=0)  #Display 1
cap = cv2.VideoCapture(0)

def show_frame():
    _, frame = cap.read()
    frame = cv2.resize(frame, (400,400))
    #frame = cv2.flip(frame, 1)
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(master = display1, image=img)
    display1.imgtk = imgtk #Shows frame for display 1
    display1.configure(image=imgtk)
    #isQKeyPress = getIsQKeyPress() 
    if False:
        window.destroy()
    window.after(10, show_frame)

show_frame()
window.mainloop()

如果您 运行 SO

上的代码,您可能会遇到此错误
 {
  "message": "SyntaxError: import declarations may only appear at top level of a module",
  "filename": "https://stacksnippets.net/js",
  "lineno": 12,
  "colno": 8
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.0.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.0.0/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.4/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.12.1/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.6/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/1.2.1/knockout-min.js"></script>

<p>Hello this is a test code</p>

import numpy as np
import cv2
from tkinter import *
#import tkinter as tk
from PIL import Image, ImageTk
import sys

window = Tk()  #Makes main window
window.overrideredirect(True)
window.wm_attributes("-topmost", True)
window.geometry("+600+200")
display1 = Label(window)
display1.grid(row=1, column=0, padx=0, pady=0)  #Display 1
cap = cv2.VideoCapture(0)

def show_frame():
    _, frame = cap.read()
    frame = cv2.resize(frame, (400,400))
    #frame = cv2.flip(frame, 1)
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(master = display1, image=img)
    display1.imgtk = imgtk #Shows frame for display 1
    display1.configure(image=imgtk)
    #isQKeyPress = getIsQKeyPress() 
    if False:
        window.destroy()
    window.after(10, show_frame)

show_frame()
window.mainloop()