使用 OpenCV-Python 将图像添加到实时摄像机源
在本文中,我们将学习如何使用Python中的 OpenCV 在实时摄像机源中插入图像。
分步实施
第 1 步:导入库
CV 将所有图像读取并存储为 NumPy 数组。我们需要 NumPy 库来操作图像,正如预期的那样,我们需要 cv2 模块。
Python3
# imorting libraries
import cv2
import numpy as np
Python3
# Setup camera
cap = cv2.VideoCapture(0)
# While loop
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Show the captured image
cv2.imshow('WebCam', frame)
# wait for the key and come out of the loop
if cv2.waitKey(1) == ord('q'):
break
# Discussed below
cv2.release()
cv2.destroyAllWindows()
Python3
# Read logo and resize
logo = cv2.imread('image.png')
size = 100
logo = cv2.resize(logo, (size, size))
Python3
# Create a mask of logo
img2gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 1, 255, cv2.THRESH_BINARY)
Python3
# Region of Interest (ROI), where we want
# to insert logo
roi = frame[-size-10:-10, -size-10:-10]
# Set an index of where the mask is
roi[np.where(mask)] = 0
roi += logo
Python3
cv2.imshow('WebCam', frame)
if cv2.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Python3
# importing the libraries
import cv2
import numpy as np
# Setup camera
cap = cv2.VideoCapture(0)
# Read logo and resize
logo = cv2.imread('image.png')
size = 100
logo = cv2.resize(logo, (size, size))
# Create a mask of logo
img2gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 1, 255, cv2.THRESH_BINARY)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Region of Image (ROI), where we want to insert logo
roi = frame[-size-10:-10, -size-10:-10]
# Set an index of where the mask is
roi[np.where(mask)] = 0
roi += logo
cv2.imshow('WebCam', frame)
if cv2.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
第 2 步:获取网络摄像头实时供稿
- 下一步是从我们的网络摄像头或任何其他连接的摄像头获取实时馈送。众所周知,视频是图像的集合。所以,我们要做的是让我们的网络摄像头每毫秒输入一个图像,并将其放入一个 while 循环中,使其成为一个不同图像的循环,背靠背显示以获取我们的实时网络摄像头馈送。
- 显然,这不会是一个永无止境的循环,所以我们正在设置一个打破循环的关键
- 这两行代码要做的是,它将等待指定的键,如果按下该键,它将跳出循环。
蟒蛇3
# Setup camera
cap = cv2.VideoCapture(0)
# While loop
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Show the captured image
cv2.imshow('WebCam', frame)
# wait for the key and come out of the loop
if cv2.waitKey(1) == ord('q'):
break
# Discussed below
cv2.release()
cv2.destroyAllWindows()
第 3 步:读取图像
下一步是读取图像并将其存储在一个变量中,以便通过cv2.imread访问它并调整图像大小。
蟒蛇3
# Read logo and resize
logo = cv2.imread('image.png')
size = 100
logo = cv2.resize(logo, (size, size))
第 4 步:在实时源中创建蒙版
接下来,我们应该为将要放置在网络摄像头源中的图像设置一个空间,通过屏蔽该区域以平滑放置图像。
为此,我们将使用cv2.cvtColor (要了解更多信息,请访问 cv2.cvtColor )首先将给定的图像转换为灰度图像,因为如果图像是灰度图像,则在 OpenCV 中很容易处理图像,并且通过cv2.THRESH_BINARY (要了解更多信息,请访问 cv2.THRESH_BINARY)对该范围内的像素进行阈值处理来掩盖该区域,从而为图像创建一个空间。
蟒蛇3
# Create a mask of logo
img2gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 1, 255, cv2.THRESH_BINARY)
现在,我们将找到应放置图像的 ROI(感兴趣范围)并屏蔽该区域并将图像插入实时提要中。
蟒蛇3
# Region of Interest (ROI), where we want
# to insert logo
roi = frame[-size-10:-10, -size-10:-10]
# Set an index of where the mask is
roi[np.where(mask)] = 0
roi += logo
第 6 步:显示视频
现在最好在使用后为其他来源释放网络摄像头,我们可以通过cv2.release()执行此操作,并在退出循环后使用cv2.destroyAllWindows() 。
蟒蛇3
cv2.imshow('WebCam', frame)
if cv2.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
通过执行这些简单的步骤,我们可以通过这种方法插入任何图像或任何徽标。
完整代码
蟒蛇3
# importing the libraries
import cv2
import numpy as np
# Setup camera
cap = cv2.VideoCapture(0)
# Read logo and resize
logo = cv2.imread('image.png')
size = 100
logo = cv2.resize(logo, (size, size))
# Create a mask of logo
img2gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 1, 255, cv2.THRESH_BINARY)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Region of Image (ROI), where we want to insert logo
roi = frame[-size-10:-10, -size-10:-10]
# Set an index of where the mask is
roi[np.where(mask)] = 0
roi += logo
cv2.imshow('WebCam', frame)
if cv2.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
输出: