与numpy掩盖麻烦

时间:2017-10-20 17:21:03

标签: python numpy

im = cv2.imread(image)
print(im.shape)  # (420, 640, 3)
salt_pepper =im.copy()

mask =np.random.randint(0,21, size =im.shape)
print('salt_pepper',salt_pepper.shape) # (420, 640, 3)
print('mask',mask.shape) # (420, 640, 3)
white =np.argwhere(mask == 20)  #pulls out the args where mask has value of 20


#For example these are the last five values of white:

    white[-5:]

   array([[419, 623,   2],
       [419, 637,   0],
       [419, 637,   1],
       [419, 639,   1],
       [419, 639,   2]], dtype=int64)


However, this is resulting in an error:
salt_pepper[white] = 0


    IndexError             Traceback (most recent call last)
<ipython-input-102-05ab9dcc5dd9> in <module>()
----> 1 salt_pepper[white] = 0

IndexError: index 632 is out of bounds for axis 0 with size 420

我假设因为np数组希望索引是元组格式而不是数组格式。有没有numpy.function可以解决这个问题?

0 个答案:

没有答案