Load Image with transparent Background

Viewed 674

What I have tried

I try to load this

Small Circle

image with opencv in python and as you can see the image will be loaded here as png without alpha channel. But the original image has just everything transparent and only the circles are visible. In this following example I load this image and use cv2.imshow to display the image. The interesting part is, that my complete background is somehow yellowish.

import cv2

foreground = cv2.imread('img2.png', cv2.IMREAD_UNCHANGED)

cv2.imshow('img', foreground)
cv2.waitKey(0)

cv2.destroyAllWindows()

The result of imshow looks like this:

enter image description here

So I investigated the image further after loading the code and checked the array values. I already now that my first pixel is background but I needed to find the green pixels:

import cv2
import numpy as np

foreground = cv2.imread('img2.png', cv2.IMREAD_UNCHANGED)

bg_pixel_u = 0
bg_pixel_v = 0

print(foreground[bg_pixel_u, bg_pixel_v,:])
# > [115 189 247 0]

gr_pixel_u = np.where(foreground[:,:,0] != 115)[0][0]
gr_pixel_v = np.where(foreground[:,:,0] != 115)[1][0]

print(foreground[gr_pixel_u, gr_pixel_v,:])
# > [7 255 82 254]

cv2.imshow('img', foreground)
cv2.waitKey(0)

cv2.destroyAllWindows()

so I did a small workaround with a mask array:

mask = foreground[:,:,3] == 0
foreground[mask, :3] = [0, 0, 0]

So my final image looks like this:

enter image description here

My Problem

So my values [115 189 247 0] are coming from my original image and they are the background that I used in gimp. They are not visible until I load the image with opencv. So is there a way of loading images so can easily stack images with cv2.addWeighted? Because this functions seems to ignore my alpha channel values.

My versions

python: 3.7.7 opencv: 3.4.2

0 Answers
Related