RJ Uy
RJ Uy

Reputation: 397

Set ALL pixels to black if values at position is 0

I have images with the same shape defined as:

 img = cv2.imread(file, 0)  # values are 0 - 255 
 mask = cv2.imread(file2, 0) # values are only 0's and 255's

From the given images, I want to check if at mask[x,y] = 0, then set the img[x,y] = 0.

I can do this by doing a loop. But is there a way that I can do this in a numPyish way?

Upvotes: 1

Views: 32

Answers (1)

Guimoute
Guimoute

Reputation: 4629

You just need to create a mask (not the same as your existing variable) and apply it to the img array to specifically target indexes where you want to put a 0. Then it's as simple as:

mask2 = (mask == 0)
img[mask2] = 0

Alternatively,

img[mask.astype(bool)] = 0

Upvotes: 1

Related Questions