Python遮罩一组值中的图像像素 [英] Python mask image pixels from a set of values

查看:115
本文介绍了Python遮罩一组值中的图像像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供了带有标签的图像(像素的值与其标签相对应),以及可接受的标签列表,如果像素标签被接受,我尝试创建具有255值的蒙版"图像,否则为0.

Provided an image with labels (the value of a pixel corresponds to its label), and the list of labels that are accepted, I am trying to create a "mask" image with 255 value if the pixels label is accepted, 0 otherwise.

我知道这是一种缓慢的方法,因为它以python-speed的速度遍历图像(但是很好地展示了这个想法):

I know that this is a slow approach as it iterates over the image at python-speed (but it demonstrates the idea well):

mask = numpy.zeros(labels.shape[:2], dtype = "uint8")

for i in xrange(mask.shape[0]):
    for j in xrange(mask.shape[1]):
        if labels[i][j] in accepted:
            mask[i][j] = 255

我知道使用python切片和遮罩要快得多,但是我不知道如何编写复杂的条件.当我逐个遮盖像素接受的标签时,我仍然可以得到极大的提速,就像这样:

I know that it is much faster to use python slicing and masking, but I do not know how to compose a complicated condition. I still get a tremendous speed-up when I mask the pixels one-by-one accepted label, like so:

for value in accepted:
    mask[labels == value] = 255

我可以以某种方式使单线工作成为我想要的吗?我的python知识是生锈的(阅读:过去几年中几乎没有python),所以当我尝试使用发现的一些示例来编写此代码时,这是我得到的最接近的信息:

Can I somehow make a one-liner doing what I want? My python knowledge is rusty (read: almost no python in the last few years), so while I tried composing this using some examples I found, this is the closest I got:

mask[(labels in accepted).all()] = 255

在这种情况下,我得到以下错误:ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

And in this case I get the following error: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我看过类似的问题(例如此处等),但它们似乎都覆盖了以下情况:值在一定范围内或低于/高于阈值(< 10),或者要更改的图像切片是连续的.

I've looked at similar SO questions (e.g.here or here and more) but they all seem to cover the cases where either the values are from a range or lower/higher than a threshold (<10) , or where the image slice to change is continuous.

关于是否是接受的值中的值"检查的任何建议都是很好的.

Any suggestion on how check "is value among accepted values" would be great.

推荐答案

与此同时,我找到了一个可以解决我自己问题的速度的解决方案:

In the meantime, I found an acceptable-speed solution to my own question:

mask = numpy.zeros(labels.shape[:2], dtype = "uint8")
mask[numpy.in1d(labels, accepted).reshape(mask.shape)] = 255

首先需要使用 numpy.in1d labels数组中获取一个布尔数组,并检查accepted中存在哪些布尔数组(python关键字"in"的逐元素函数).

It consists in first using numpy.in1d to get a boolean array from the labels array, and check which ones are present in accepted (element-wise function of the python keyword "in").

因为这显然必须返回一个1D数组,即使它可以应用于2D数组(它只是将数组拆开),所以我接下来使用reshape()来使布尔数组的尺寸与mask.

As this apparently necessarily returns a 1D array, even if it can be applied to a 2D array (it simply unravels the array), so I follow by using reshape() to make the boolean array dimensions correspond to that of the mask.

最后,我使用此布尔数组为mask的必需元素建立索引并将它们设置为所需的值.

Finally, I use this boolean array to index the required elements of mask and set them to a desired value.

这篇关于Python遮罩一组值中的图像像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆