I have an image that has different shades of black at the edges and a bit of red in the centre. I want to convert all the black pixels to transparent using opencv. I'm new to opencv so I'd appreciate your help.
I tried following what fireant said in the link: overlay a smaller image on a larger image python OpenCv, but it didn't work. Here's the code I have so far:
img = cv2.imread("/home/uwatt/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/uwatt/DIP/lensflare/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/home/uwatt/DIP/lensflare/haarcascade_eye.xml')
user = cv2.imread("/home/uwatt/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