1

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?

1 Answer 1

2

You could just add the two, right?

>>> res[cnd] += layer2[cnd]

>>> res
[[[ 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.]]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.