'Opencv Masking via grayscale img does not work

I am a newbe in Opencv. When I use inverse of gray scale image for masking it works but gray scale does not work. What do I miss ? What does mask actually do? I want to use gray img to mask real img.

img1 = cv2.imread('cv_python/DATA/watermark_no_copy.png')
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
plt.imshow(img1)

enter image description here

img1_gray = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY)
plt.imshow(img1_gray, cmap = 'gray')

enter image description here

img1_g_inv = 255 - img1_gray
my_background = np.full(img1.shape, (255, 255, 255), dtype = np.uint8)
plt.imshow(my_background)

enter image description here

my_mask = cv2.bitwise_and(my_background, my_background, mask = img1_gray)
plt.imshow(my_mask)

enter image description here

my_mask2 = cv2.bitwise_and(my_background, my_background, mask = img1_g_inv)
plt.imshow(my_mask2)

enter image description here



Solution 1:[1]

Can you change my_background and my_mask like

before

my_background = np.full(img1.shape, (255, 255, 255), dtype = np.uint8)
my_mask = cv2.bitwise_and(my_background, my_background, mask = img1_gray)

after

my_background = np.full(img1_gray.shape, 255, dtype = np.uint8)
my_mask = cv2.bitwise_and(img1_gray, img1_gray, mask = my_background)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wtow