Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在3D RGB numpy阵列中设置遮罩像素_Python_Arrays_Numpy - Fatal编程技术网

Python 在3D RGB numpy阵列中设置遮罩像素

Python 在3D RGB numpy阵列中设置遮罩像素,python,arrays,numpy,Python,Arrays,Numpy,我想使用遮罩设置3d numpy阵列(RGB图像)中与某些条件匹配的所有像素。我有这样的想法: def make_dot(img, color, radius): """Make a dot of given color in the center of img (rgb numpy array)""" (ydim,xdim,dummy) = img.shape # make an open grid of x,y y,x = np.ogrid[0:ydim, 0

我想使用遮罩设置3d numpy阵列(RGB图像)中与某些条件匹配的所有像素。我有这样的想法:

def make_dot(img, color, radius):
    """Make a dot of given color in the center of img (rgb numpy array)"""
    (ydim,xdim,dummy) = img.shape
    # make an open grid of x,y
    y,x = np.ogrid[0:ydim, 0:xdim, ]
    y -= ydim/2                 # centered at the origin
    x -= xdim/2
    # now make a mask
    mask = x**2+y**2 <= radius**2 # start with 2d
    mask.shape = mask.shape + (1,) # make it 3d
    print img[mask].shape
    img[mask] = color

img = np.zeros((100, 200, 3))
make_dot(img, np.array((.1, .2, .3)), 25)

因为img[mask]的形状是(1961,);i、 e.它被展平,只包含“有效”像素,这是有意义的;但是我如何才能使它“通过掩码写入”,因为它只设置掩码为1的像素?请注意,我想一次为每个像素写入三个值(最后一个dim)。

您几乎完全正确

(ydim,xdim,dummy) = img.shape
# make an open grid of x,y
y,x = np.ogrid[0:ydim, 0:xdim, ]
y -= ydim/2                 # centered at the origin
x -= xdim/2
# now make a mask
mask = x**2+y**2 <= radius**2 # start with 2d
img[mask,:] = color
(ydim、xdim、dummy)=img.shape
#用x,y做一个开放的网格
y、 x=np.ogrid[0:ydim,0:xdim,]
y-=ydim/2#以原点为中心
x-=xdim/2
#现在做一个面具

面具=x**2+y**2完美,谢谢!对于未来的读者来说,将掩码保留为2d(本例中为
(100200)
)是关键。一旦我这样做了,我甚至不需要
掩码,:
部分;只要
img[mask]=color
就可以了。我想numpy索引的工作原理不仅仅是一个小魔术。顺便说一句,我刚刚更新了numpy数组索引文档,以澄清什么让我困惑。
(ydim,xdim,dummy) = img.shape
# make an open grid of x,y
y,x = np.ogrid[0:ydim, 0:xdim, ]
y -= ydim/2                 # centered at the origin
x -= xdim/2
# now make a mask
mask = x**2+y**2 <= radius**2 # start with 2d
img[mask,:] = color