4
votes

Hey everyone I'm trying to make a game using my webcam where I need some objects to fall down the screen while Im streaming video with my webcam (that stream being my background).

I am using python and module cv2 of opencv

The question is: how do I apply a mask to this objects? I already have an image to be the mask of the original image but I dont know how to apply it to subtract the background of the original image..

ive already tried using the cv2.bitwise_and but nothing happened, the imaged stayed the same with black background:

#targets
original_ball = cv2.imread("Aqua-Ball-Red-icon.png")
ball = cv2.resize(bola_original, (64,64), fx=1, fy=1)
#mask
mask_original = cv2.imread("input-mask.png",0)
mask = cv2.resize(mask_original, (64,64), fx=1, fy=1)
res = cv2.bitwise_and(ball, ball, mask = mask)

Thanks in advance!

2
What are the 0/1 values of your mask? - loopbackbee
Since you told about the game, have you checked the module 'pygame'? - Abid Rahman K
yes I already did but I already made the entire game using opencv (cv2 module) and numpy, the only problem that I have is with the mask of the targets in the game - Cap.Alvez

2 Answers

1
votes

If you're using cv2, you're working with numpy arrays, and there's no need to turn to opencv for something as simple as a mask.

First, manipulate (multiply, possibly) your mask array so that the values you want (i.e.: those not masked out) have value 1. Then, multiply your source image by your mask. That will make the resulting image have the original pixels where the mask == 1, and 0 (i.e.:black) where the mask == 0.

Here's the gist of it:

import numpy
original_ball = cv2.imread("Aqua-Ball-Red-icon.png")
ball = cv2.resize(bola_original, (64,64), fx=1, fy=1)
mask_original = cv2.imread("input-mask.png",0)
mask = cv2.resize(mask_original, (64,64), fx=1, fy=1)

max_value= numpy.max(mask)
mask/=max_value
res= ball*mask

Depending on the color depth of your input-mask.png, you may need to reduce it to grayscale first

1
votes

The mask was not appearing because of the 0 argument next to the name of the mask image).