3. 基于AR的虚拟商店
import cv2
import numpy as np
import os
# 初始化摄像头
cap = cv2.VideoCapture(0)
cv2.namedWindow("AR Virtual Store")
# 加载商品图像
products = []
product_names = ["product1.png", "product2.png", "product3.png"]
for name in product_names:
img = cv2.imread(os.path.join("images", name), cv2.IMREAD_UNCHANGED)
products.append(img)
# 定义绘制函数
def overlay_image(background, overlay, x, y):
h, w = overlay.shape[0], overlay.shape[1]
alpha_overlay = overlay[:, :, 3] / 255.0
alpha_background = 1.0 - alpha_overlay
for c in range(0, 3):
background[y:y+h, x:x+w, c] = (alpha_overlay * overlay[:, :, c] +
alpha_background * background[y:y+h, x:x+w, c])
while True:
ret, frame = cap.read()
if not ret:
break
# 进行商品绘制
overlay_image(frame, products[0], 100, 100)
overlay_image(frame, products[1], 300, 100)
overlay_image(frame, products[2], 500, 100)
cv2.imshow("AR Virtual Store", frame)
key = cv2.waitKey(1)
if key == 27: # 按ESC退出
break
cap.release()
cv2.destroyAllWindows()
标签:overlay,frame,商店,cv2,虚拟,products,background,alpha,AR
From: https://blog.51cto.com/u_16213142/11935786