【问题标题】:how to fix png image onto another image with opencv with transparent background如何使用具有透明背景的opencv将png图像修复到另一个图像上
【发布时间】:2019-11-06 14:59:27
【问题描述】:

我想将眼镜 PNG 图像添加到另一个图像上,但我得到的 PNG 图像是白色背景的,它隐藏了脸部。

我需要知道我做错了什么。

原始人脸图像

眼镜原图

预期结果

import cv2
import numpy
img = cv2.imread("barack-obama.jpg")
lunette = cv2.imread("lunette.png", cv2.IMREAD_UNCHANGED)
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

eyes = eye_cascade.detectMultiScale(img, scaleFactor = 1.1, minNeighbors = 5)   

r = 500.0 / img.shape[1]
dim = (500, int(img.shape[0] * r))
resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)

 grey = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(grey, 1.3, 5)

for (x,y,w,h) in faces:

roi_grey = grey[y:y+h, x:x+w]
roi_color = resized[y:y + h, x:x + w]
eyes = eye_cascade.detectMultiScale(roi_grey)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

lunette = cv2.resize(lunette , (w,h))
w, h, c = lunette.shape 
for i in range(0, w):
for j in range(0, h):

    if lunette[i, j][0] != 0:
        resized[y + i, x + j] = lunette[i, j][1]
#help please
cv2.imshow('img',resized)

cv2.waitKey(0)

【问题讨论】:

标签: python python-3.x opencv


【解决方案1】:

这是在 Python/OpenCV/Numpy 中执行此操作的一种方法:

  • 阅读背景图片(奥巴马)
  • 读取覆盖图像(眼镜)不变
  • 从叠加图像中提取基本 BGR 通道
  • 从叠加图像中提取 Alpha 通道作为蒙版
  • 将叠加层的 BGR 通道插入到所需位置的背景图像中
  • 将蒙版插入到所需位置的背景图像大小的黑色图像中
  • 使用 Numpy 在哪里合成新的背景和使用新蒙版图像的叠加图像
  • 保存结果
import cv2
import numpy as np

# read background image
img = cv2.imread("obama.jpg")
ht, wd = img.shape[:2]

# read overlay image
img2 = cv2.imread("sunglasses.png", cv2.IMREAD_UNCHANGED)
ht2, wd2 = img2.shape[:2]

# extract alpha channel as mask and base bgr images
bgr = img2[:,:,0:3]
mask = img2[:,:,3]

# insert bgr into img at desired location and insert mask into black image
x = 580
y = 390

bgr_new = img.copy()
bgr_new[y:y+ht2, x:x+wd2] = bgr

mask_new = np.zeros((ht,wd), dtype=np.uint8)
mask_new[y:y+ht2, x:x+wd2] = mask
mask_new = cv2.cvtColor(mask_new, cv2.COLOR_GRAY2BGR)

# overlay the base bgr image onto img using mask
result = np.where(mask_new==255, bgr_new, img)

# save results
cv2.imwrite('obama_glasses.jpg', result)

# display results
cv2.imshow('bgr', bgr)
cv2.imshow('mask', mask)
cv2.imshow('bgr_new', bgr_new)
cv2.imshow('mask_new', mask_new)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

【讨论】:

    【解决方案2】:

    你可以使用 cvzone 库来解决这个问题。

    1. 安装 cvzone 库

      pip install cvzone
      
    2. 在您的项目中导入 cvzone

      import cvzone
      
    3. 使用以下格式读取您的表情符号

      lunette = cv2.imread("lunette.png", cv2.IMREAD_UNCHANGED)
      
    4. 现在像这样替换你的表情符号:

      YOUR BACKGROUND PICTURE = cvzone.overlayPNG(YOUR BACKGROUND PICTURE, lunette , [x, y])
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-23
      • 2012-12-19
      • 2012-02-26
      • 2017-10-16
      • 2018-10-31
      • 1970-01-01
      • 2011-07-20
      相关资源
      最近更新 更多