【问题标题】:numpy.where() returns empty array instead of replacingnumpy.where() 返回空数组而不是替换
【发布时间】:2020-01-29 00:39:22
【问题描述】:

我有 RGB 图像作为 numpy 数组,我想替换为某个像素值:

import numpy as np
import random

def rgb():
    L = [1]*3
    return [random.randrange(0, 256,1)for l in L]

def makeDummyData():
    #creates list of lists with 3 rgb, which contains values to replace (x)
    L = [1]*10000
    L = [rgb() for l in L]
    x = L[0]
    for i in range(500):
        L[i] = x
    return L, x

L, x = makeDummyData()
a = np.array(L).reshape(-1, 3)
a = np.array(a).reshape(100, 100, 3)
b = np.where(a == (np.array(x), [255,255,255], a))
print(b)

而不是返回一个数组,其中目标值被替换,输出是 (array([], dtype=int64),)

【问题讨论】:

  • 我觉得你的括号有点不对。

标签: python numpy


【解决方案1】:

np.where

  • 括号似乎已关闭
b = np.where(a == np.array(x), [255,255,255], a) # After == an extra '(' is used

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-30
    • 2016-06-27
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多