-1

我有一张图片:

>> img.shape
(720,1280)

我已经确定了一组我想要的 x,y 坐标,在它们为真的地方,将一致图像的值设置为 255。

这就是我的意思。形成索引是我的vals

>>> 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是冗余的range(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

我究竟做错了什么?

4

2 回答 2

0

这有效,但真的很难看:

# out[(vals[:][:,0],vals[:][:,1])]=255
out[(vals[:,0],vals[:,1])]=255

有更好的吗?

于 2019-04-10T02:06:21.323 回答
0

我认为这应该有所帮助:

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 
于 2019-04-10T04:50:30.590 回答