gpt4 book ai didi

python - OpenCV:增强视频流源 .png 未按预期呈现透明度

转载 作者:行者123 更新时间:2023-12-02 17:24:22 26 4
gpt4 key购买 nike

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

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()

Augmented Beaver Face

最佳答案

使用问题 overlay a smaller image on a larger image python OpenCv 中的示例

我将其缩小为仅显示如何放置图像

  • 使用 cv2.IMREAD_UNCHANGED将其加载为 RGBA .
  • 将其拆分为 RGBA
  • 使用 Aimage 创建掩码和 border
  • 使用循环添加 channel

  • enter image description here
    import cv2

    stream = cv2.VideoCapture(0)

    # load RGBA
    augment = cv2.imread('image.png', cv2.IMREAD_UNCHANGED) # load RGBA

    # make it smaller then frame - only for test
    W = 320
    H = 240
    augment = cv2.resize(augment, (W, H))

    # split image and alpha
    image = augment[:,:,0:3]
    alpha = augment[:,:,3]
    mask_image = alpha / 255.0
    mask_border = 1.0 - mask_image

    # ROI - region of interest
    x1 = 200
    y1 = 100
    x2 = x1 + W
    y2 = y1 + H

    while True:
    ret, border = stream.read()

    # copy only in some region (ROI) (don't assign to variable) but gives worse result
    #cv2.copyTo(image, alpha, border[y1:y2, x1:x2])

    for c in range(0, 3): # channels RGB
    border[y1:y2, x1:x2, c] = (image[:, :, c]*mask_image + border[y1:y2, x1:x2, c]*mask_border)

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

    stream.release()
    cv2.destroyAllWindows()

    顺便说一句:我尝试使用 cv2.copyTo(image, mask_image, border)但它给出了更糟糕的结果——也许掩码/alpha 需要 3 个 channel 。

    似乎可以在 C/C++ 中完成 - how to insert a small size image on to a big image

    关于python - OpenCV:增强视频流源 .png 未按预期呈现透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60290354/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com