Python 如何将一系列颜色转换为透明?

Python 如何将一系列颜色转换为透明?,python,opencv,Python,Opencv,我有一张图像,边缘有不同深浅的黑色,中间有一点红色。我想使用opencv将所有黑色像素转换为透明。我是opencv的新手,非常感谢您的帮助 我试着按照fireant在链接中所说的做,但没有成功。以下是我目前掌握的代码: img = cv2.imread("/home/ansuman/Downloads/lensf1.jpg") img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) tmp = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

我有一张图像,边缘有不同深浅的黑色,中间有一点红色。我想使用opencv将所有黑色像素转换为透明。我是opencv的新手,非常感谢您的帮助

我试着按照fireant在链接中所说的做,但没有成功。以下是我目前掌握的代码:

img = cv2.imread("/home/ansuman/Downloads/lensf1.jpg")
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
tmp = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_,alpha = cv2.threshold(tmp,5,255,cv2.THRESH_BINARY)
b,g,r = cv2.split(img)
rgba = [b,g,r,alpha]
dst = cv2.merge(rgba, 4)
plt.imshow(dst)
print(dst.shape)

face_cascade = cv2.CascadeClassifier('/home/ansuman/DIP/lensflare/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/home/ansuman/DIP/lensflare/haarcascade_eye.xml')

user = cv2.imread("/home/ansuman/Downloads/Dicaprio.jpg")
gray_user = cv2.cvtColor(user, cv2.COLOR_BGR2GRAY)
user = cv2.cvtColor(user, cv2.COLOR_BGR2BGRA)
faces = face_cascade.detectMultiScale(gray_user, 1.3, 5)
print("Faces:",faces)
for (x,y,w,h) in faces:
    roi_gray = gray_user[y:y+h,x:x+w]
    roi_color = user[y:y+h,x:x+w]
    eyes = eye_cascade.detectMultiScale(roi_gray)
    for (ex,ey,ew,eh) in eyes:
        print(ex,ey,ew,eh)
        #cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,0,255),5)

        # resizing & paste the lf image on user
        roi_eye = user[y+ey:y+ey+eh,x+ex:x+ex+ew]
        resized_lensflare = cv2.resize(dst,(eh,ew))    
        resized_lensflare = cv2.cvtColor(resized_lensflare, cv2.COLOR_BGR2RGBA)
        user[y+ey:y+ey+eh,x+ex:x+ex+ew] = resized_lensflare

您需要使用alpha混合将镜头光斑与背景图像结合起来。这是我用过的脱衣舞:

import cv2
flare = cv2.imread("/home/stephen/Desktop/flare.jpg")
user = cv2.imread("/home/stephen/Desktop/leo.jpg")
eyes = [[100,50,200,200],[175,50,200,200]]
for x,y,w,h in eyes:       
    # resizing & paste the lf image on user
    roi_eye = user[y:y+h,x:x+w]
    resized_lensflare = cv2.resize(flare,(w,h))    
    # Make foreground background and alpha
    foreground = resized_lensflare.copy()
    background = roi_eye.copy()
    alpha= foreground.copy()
    # Convert uint8 to float
    foreground = foreground.astype(float)
    background = background.astype(float) 
    # Normalize the alpha mask to keep intensity between 0 and 1
    alpha = alpha.astype(float)/255
    # Multiply the foreground with the alpha matte
    foreground = cv2.multiply(alpha, foreground)
    # Multiply the background with ( 1 - alpha )
    background = cv2.multiply(1.0 - alpha, background)
    # Add the masked foreground and background.
    outImage = cv2.add(foreground, background)
    # Mask the user image
    user[y:y+h,x:x+w] = outImage
cv2.imshow('img', user)
cv2.waitKey()
cv2.destroyAllWindows()

这很好,但我们不知道您的图像是什么样子!!!是的,我的坏朋友,“fireant在链接中说”怎么了?你能解释为什么“它不起作用”?这看起来是你应该做的。你发布的代码似乎不相关——主要是检测眼睛,这似乎与你所问的问题无关。问题应该包括硬编码坐标,并删除其他不相关的东西。这就是问题所在,我不知道为什么它对我不起作用。它只是按原样显示图像。看看这里,制作lazer eye effects是我见过的OpenCV最大的用途。哈哈,我的主要动机是使用OpenCV制作镜头光斑meme。他妈的让我的老师大吃一惊xD