2

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

MaKaNu
  • 762
  • 8
  • 25
  • 5
    `cv2.imshow` ignores transparency. I don't see any problem here, at least not in the functionality of OpenCV. – Dan Mašek Jul 20 '20 at 17:11
  • Okay so addWeigthed is not ignoring it? – MaKaNu Jul 20 '20 at 19:17
  • Yes, `addWeighted` doesn't ignore it -- it's not aware the array represents an image and that some particular layer specifies transparency in this case. The function just treats the inputs as arrays of numbers, and it performs the operation on each channel independently. – Dan Mašek Jul 20 '20 at 19:28
  • Okay, but that will also lead to the result, that my yellow background color will mixed into another picture, so the only posibilty to achieve the wanted result without using alpha composition is to asure that my background is black. Or does opencv has a function which does alpha composition? – MaKaNu Jul 20 '20 at 19:52
  • at least it is a bit confusing since the most img programms show alpha channel influence – MaKaNu Jul 20 '20 at 19:54
  • 1
    Not that I'm aware of, but it's fairly simple to implement. For example, see my answer here: https://stackoverflow.com/a/37198079/3962537 – Dan Mašek Jul 20 '20 at 19:57
  • Yes I know this. In my last step I did the first step with the mask. At least I don't need to get my alpha with a threshhold because it is already included inside my image. I would asume that something like this could be interesting by large scale images. On this site there is a pointer solution at the end https://www.learnopencv.com/alpha-blending-using-opencv-cpp-python/ time comparible we speaking of 1 to 6 FPS. – MaKaNu Jul 20 '20 at 20:07
  • I would accept an awnser that will bring your first comment into the right context. – MaKaNu Jul 20 '20 at 20:08

0 Answers0