0
点赞
收藏
分享

微信扫一扫

7. 基于AR的虚拟家具摆放应用

7. 基于AR的虚拟家具摆放应用

import cv2
import numpy as np
import os

# 初始化摄像头
cap = cv2.VideoCapture(0)
cv2.namedWindow("AR Virtual Furniture")

# 加载家具图像
furniture = []
furniture_names = ["chair.png", "table.png", "sofa.png"]
for name in furniture_names:
    img = cv2.imread(os.path.join("images", name), cv2.IMREAD_UNCHANGED)
    furniture.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, furniture[0], 100, 100)
    overlay_image(frame, furniture[1], 300, 100)
    overlay_image(frame, furniture[2], 500, 100)

    cv2.imshow("AR Virtual Furniture", frame)

    key = cv2.waitKey(1)
    if key == 27:  # 按ESC退出
        break

cap.release()
cv2.destroyAllWindows()

举报

相关推荐

0 条评论