Following the example given in this answer I am trying to join two (actually more) images but I can not seem to make it right. Since images are difficult to comprehend (for me) I made a simple example
import numpy as np
import cv2
layer1 = np.zeros((5, 5, 4))
layer2 = np.zeros((5,5,4))
green_color = (0, 66, 0, 0)
otro_color = (40,0,0,0)
cv2.rectangle(layer1, (1, 1), (3, 3), green_color, 1)
print(layer1)
cv2.line(layer2, (1, 1), (3, 3), otro_color, 1)
print(layer2)
res = layer1[:]
cnd = layer2[:, :, 0] > 0
res[cnd] = layer2[cnd] #<-- this is not working correctly
print(res)
As a result first I have a matrix
[[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]]
Then I have the matrix
[[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 40. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 40. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 40. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]]
as expected. However when trying to fuse it I have
[[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 40. 0. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 66. 0. 0.]
[ 40. 0. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 66. 0. 0.]
[ 40. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]]
Which is not what I want. You can notice that in the second element everytime I apply 40 in the first channel, the 60 of the second channel dissappears.
What I want to get is
[[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 40. 66. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 66. 0. 0.]
[ 40. 0. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 66. 0. 0.]
[ 0. 66. 0. 0.]
[ 40. 66. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]]
I've tried (without success) to do
res= layer1 | layer2 # does not work
also
cnd = np.logical_or(layer1,layer2)
which gives a nice mask of trues where I want to put values but then
res[cnd] = layer2[cnd]
ruins everything.
How can I fusion those arrays to get the desired result?