简体   繁体   中英

Opencv make image transparent

Am new to OpenCV and currently working on a project (Sunglasses Filter). I would like to make the sunglassed transparent. This is what I have tried

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)

Both when I use the alpha glassMask1 it makes no different.

I can provide my full code if that would help.

Edited - Added my code When I modify the alpha the glass doesn't show at all


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

在此处输入图片说明

您应该尝试opencvaddWeighted函数,它可以让您将原始图像与太阳镜叠加,并有专门的不透明度参数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM