OpenCV使图像透明

时间:2019-12-15 19:30:40

标签: python opencv

是OpenCV的新手,目前正在从事一个项目(太阳镜过滤器)。 我想使太阳镜透明。 这就是我尝试过的

sunglass = cv2.imread("sunglass.png")
sunglassCopy = sunglass.copy()
sunglassBGR = sunglassCopy[:,:,0:3]
glassMask1 = sunglassCopy[:,:,3]
# glassMask1 = np.uint8(glassMask1/255)
glassMask1 = cv2.multiply(glassMask1,3)

当我使用字母glassMask1时,两者没有什么不同。

如果可以的话,我可以提供完整的代码。

已编辑-添加了我的代码 当我修改Alpha时,玻璃根本不显示


output,bboxes = detectFaceOpenCVDnn(net, img)
f = bboxes[0]
face = img[f[1]:f[3],f[0]:f[2]]

eye,y1,y2,face_width=get_eye_roi(face)
sunglassCopy = sunglass.copy()
sunglassCopy = cv2.resize(sunglassCopy, (eye.shape[1],eye.shape[0]))
sunglassBGR = sunglassCopy[:,:,0:3]
# sunglassCopy[:,:,3] = sunglassCopy[:,:,3] * 0.5
glassMask1 = sunglassCopy[:,:,3]
glassMask1 = cv2.multiply(glassMask1,3)
# glassMask1 = glassMask1 * 1

glassMask = cv2.merge((glassMask1,glassMask1,glassMask1))
#make the values [0,1]
glassMask = np.uint8(glassMask/255)
faceWithGlass = face.copy()
eyeROI,_,_,_ = get_eye_roi(faceWithGlass)
maskedEye = cv2.multiply(eyeROI, (1-glassMask))
# BGRA = cv2.cvtColor(maskedEye, cv2.COLOR_BGR2BGRA)
# print(BGRA.shape)
# a=BGRA[:,:,3] * 0.5
# BGRA[...,3]=a
maskedGlass = cv2.multiply(glassMask,sunglassBGR)
eyeFinal = cv2.add(maskedEye, maskedGlass)
faceWithGlass[y1:y2, 0:face_width]=eyeFinal
img[f[1]:f[3],f[0]:f[2]] =faceWithGlass

enter image description here

1 个答案:

答案 0 :(得分:0)

您应该尝试使用opencv的{​​{1}}函数,该函数可以使您的原始图像与太阳镜重叠,并具有不透明度的专用参数。