简体   繁体   English

如果 position 的值为 0,则将所有像素设置为黑色

[英]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.从给定的图像中,我想检查 mask[x,y] = 0,然后设置 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?但是有没有一种方法可以让我以 numPyish 的方式做到这一点?

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:您只需要创建一个掩码(与现有变量不同)并将其应用于img数组,以专门针对您想要放置 0 的索引。然后它很简单:

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

Alternatively,或者,

img[mask.astype(bool)] = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM