首页 > 解决方案 > 使用 np.isin 和 np.where 进行快速像素处理 [issue]

问题描述

我是 python 和图像处理的新手。我正在尝试使用 np.isin 和 np.where 在输入图像中查找给定 RGB 值的列表(我想避免在所有像素上进行嵌套循环)。所以,这里是输入和输出

输入:https ://imgur.com/eNylzA9

输出:https ://imgur.com/lDctkj9

我正在使用以下代码 -

fliterlist = [
    [244,240,255],
    [253,239,255],
    [255,234,249],
    [255,230,245],
    [255,229,243],
    [255,228,242]
]
# actual list has more than 100 elements

def imageTest(img,count=0):

    outImg = np.zeros(img.shape,dtype=np.uint8)

    posArray=(np.isin(img,bb)).all(axis=2)
    outImg[np.where(posArray)] = [255,255,255]

    outname = './fast/imageTest_'+str(count)+'.jpg'
    outputlist[outname]=outImg

    return

出于某种原因,我没有得到预期的输出。我的意思是,如果我使用双嵌套循环来迭代所有像素,我会得到想要的结果。但在这里看起来 np.isin 给了我一个不同的结果。请帮我确定问题。

这是完美工作的示例想法 -

图片 - https://imgur.com/zP3zuLj

标签: python-3.xnumpyopencv

解决方案


使用opencv的inrange函数,如下:

mask = cv2.inRange(image, lower_range, upper_range)

我知道您想找到一个特定的像素,因此请相应地调整范围。根据我的经验,无论您尝试完成什么任务,使用一定范围的像素都会更好,这将是有益的。但以后取决于你。


推荐阅读