OpenCV:扩充视频流源.png未能按预期呈现透明度

问题描述 投票:0回答:1

我正在尝试在open-cv中创建自己的面部过滤增强现实程序。这个想法是它将海狸映射到用户的脸上。当前,使用'cv2.imread(...)'加载此图像时,我无法获得适当的透明性。它在背景中看起来是黑色的,并且经常在某些白色区域中部分显示。当我在Photoshop中打开此图像时,我完全有能力将其移动到具有预期透明度结果的背景上。我想知道阿尔法是否无法正确渲染。这是我要在其中加载图像的相关代码。

import numpy
import cv2

def augment_stream(face: numpy.array, augment: numpy.array) -> numpy.array:
    face_h, face_w, _ = face.shape
    augment_h, augment_w, _ = augment.shape

    scalar = min(face_h / augment_h, face_w / augment_w)
    delta_augment_h = int(scalar * augment_h)
    delta_augment_w = int(scalar * augment_w)

    delta_augment_shape = (delta_augment_w, delta_augment_h)
    resized_augment = cv2.resize(augment, delta_augment_shape)

    augmented_face = face.copy()
    dark_pixels = (resized_augment < 250).all(axis=2)
    offset_x = int((face_w - delta_augment_w) / 2)
    offset_y = int((face_h - delta_augment_h) / 2)

    augmented_face[offset_y: offset_y+delta_augment_h, offset_x: offset_x+delta_augment_w][dark_pixels] = resized_augment[dark_pixels]

    return augmented_face

def main():
    stream = cv2.VideoCapture(0)
    cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
    augment = cv2.imread('assets/normal.png')
    # tmp = cv2.cvtColor(augment, cv2.COLOR_BGR2GRAY)
    # _,alpha = cv2.threshold(tmp,0,255,cv2.THRESH_BINARY)
    # b, g, r = cv2.split(augment)
    # rgba = [b,g,r, alpha]
    # dst = cv2.merge(rgba,4)
    # cv2.imwrite("assets/normal.png", dst)


    while True:
        ret, border = stream.read()
        border_h, border_w, _ = border.shape

        bw = cv2.equalizeHist(cv2.cvtColor(border, cv2.COLOR_BGR2GRAY))

        rects = cascade.detectMultiScale(bw, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)

        for x, y, w, h in rects:
            y0 = int(y - 0.25*h)
            y1 = int(y + 0.75*h)
            x0 = x
            x1 = x + w

            if x0 < 0 or x1 > border_w or y0 < 0 or y1 > border_h:
                continue

            border[y0: y1, x0: x1] = augment_stream(border[y0: y1, x0: x1], augment)

        cv2.imshow('border', border)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    stream.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    main()

“海狸脸” >>

我正在尝试在open-cv中创建自己的面部过滤增强现实程序。这个想法是它将海狸映射到用户的脸上。当前,加载时我无法获得适当的透明度...

python opencv augmented-reality face-recognition
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.