提供带有标签的图像(像素的值对应于其标签)以及接受的标签列表,我尝试使用以下命令创建“蒙版”图像255
如果像素标签被接受,则值,0
否则。
我知道这是一种缓慢的方法,因为它以 python 速度迭代图像(但它很好地演示了这个想法):
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 切片和屏蔽要快得多,但我不知道如何组成复杂的条件。当我将像素一一接受的标签屏蔽时,我仍然获得了巨大的加速,如下所示:
for value in accepted:
mask[labels == value] = 255
我能以某种方式制作一句台词做我想做的事吗?我的 python 知识很生疏(阅读:过去几年几乎没有 python),所以当我尝试使用我发现的一些示例来编写此内容时,这是我得到的最接近的:
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()
我看过类似的问题(例如here https://stackoverflow.com/questions/13461153/how-can-i-iterate-over-image-pixels-in-a-faster-manner-in-python or here https://stackoverflow.com/questions/26445153/iterations-through-pixels-in-an-image-are-terribly-slow-with-python-opencv等等),但它们似乎都涵盖了值来自某个范围或低于/高于阈值 (
任何关于如何检查“是否是可接受的值中的值”的建议都会很棒。