Numpy 使用x,y的数组作为图像数组中的索引
我有一个形象:Numpy 使用x,y的数组作为图像数组中的索引,numpy,numpy-indexing,Numpy,Numpy Indexing,我有一个形象: >> img.shape (720,1280) 我已经确定了一组x,y坐标,如果它们为真,则将共形图像的值设置为255 这就是我的意思。构成索引的是我的VAL: >>> vals.shape (720, 2) >>> vals[0] array([ 0, 186]) # the x is 0, the y is 186, I'd like to set value at img[0][186]=255 >>>
>> img.shape
(720,1280)
我已经确定了一组x,y坐标,如果它们为真,则将共形图像的值设置为255
这就是我的意思。构成索引的是我的VAL
:
>>> vals.shape
(720, 2)
>>> vals[0]
array([ 0, 186]) # the x is 0, the y is 186, I'd like to set value at img[0][186]=255
>>> vals[719]
array([719, 207]) # the x is 719, the y is 207, I'd like to set value at img[719][207]=255
vals
的第一个维度与范围(719)
冗余
我首先创建一个与img形状相同的图像:
>>> out = np.zeros_like(img)
>>> out.shape
(720, 1280)
但是从这里开始,我的索引进入out
似乎不起作用:
>>> out[vals] = 255
>>> out.shape
(720, 1280)
>>> out
array([[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
>>> out.min()
255
这使得/all/out
值为255,而不仅仅是索引为out==vals
的值
我希望:
>>> out[0][0]
0
>>> out[0][186]
255
>>> out[719][207]
255
我做错了什么?这是可行的,但确实很难看:
# out[(vals[:][:,0],vals[:][:,1])]=255
out[(vals[:,0],vals[:,1])]=255
有更好的吗?我认为这应该有帮助:
import numpy as np
img = np.random.rand(100, 200) # sample image, e.g. grayscaled.
where_to_change = [(20,10), (3, 4)] # in (x, y)-fashion
#So, you need to set: `img[20, 10] = 1`, `img[3, 4]= 1` etc....
img[list(zip(*where))] = 1