Opencv或Numpy-有效替换图像中的像素列表 [英] Opencv or Numpy-- replace a list of pixels in an image, efficiently
问题描述
您的OpenCV或Numpy Gurus,
我一直在寻找答案,但是很惊讶在这里或其他地方找不到它...
我有一个黑色图像,我想用某个值替换一个像素列表(相当大的列表).将来,某些值"将是值的列表,但是目前,让我们保持简单.
进一步简化,我只在下面使用一个由2个数字组成的二维数组,而不是RGB值... 因此,这种效率很低的方法适用于这种情况:
a = np.zeros((5, 5,1))
for i in np.asarray( ([2,3],[3,4]) ):
a[i[0], i[1]] = 20
我希望有一种在二维数组上使用np.put
的方法,而我不依赖python循环.
现在,我建议我可以使用
a[ ([2,3],[3,4]) = 20
但是,当我在真正的OpenCV问题中使用这种方法时,却发现它不起作用.这种非常低效的方法可以做到:
coords_list = ([3,5],[55,60],[25,90])
black_image =np.zeros((480,640,3))
for i in coords_list:
black_image[i[1],i[0]] = [255,255,255]
我希望对许多人来说这是一个简单的问题.
谢谢!
实际上,您可以直接使用坐标元组索引矩阵:简单的a[([2, 3], [3, 4])] = 20
给出与for循环完全相同的结果. /p>
Hi OpenCV or Numpy Gurus,
I've been searching to an answer to this, but I'm surprised to not find it here or elsewhere...
I have a black image, and I want to replace a list of pixels (quite a large list) with a certain value. In the future, the "certain value" will be a list of values, but for the moment, let's keep it simple.
Simplifying even further, I just use a 2-d array of single numbers below, instead of RGB values... So, this fairly inefficient approach works for this sort of thing:
a = np.zeros((5, 5,1))
for i in np.asarray( ([2,3],[3,4]) ):
a[i[0], i[1]] = 20
I was hoping there was a way to use np.put
on a two dimensional array, where I don't rely on a python loop.
Now I had the suggestion that I could use
a[ ([2,3],[3,4]) = 20
However I notice when I use this approach in my real OpenCV problem it does not work. This very inefficient approach does:
coords_list = ([3,5],[55,60],[25,90])
black_image =np.zeros((480,640,3))
for i in coords_list:
black_image[i[1],i[0]] = [255,255,255]
I expect this is a simple question to many out there.
Thanks!
In fact, you can directly use your tuple of coordinates to index your matrix: a simple a[([2, 3], [3, 4])] = 20
gives the exact same result as your for loop here.
这篇关于Opencv或Numpy-有效替换图像中的像素列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!