越来越指数比较多维数组时 [英] getting indices when comparing multidimensional arrays
问题描述
我有两个numpy的阵列,一个 RGB
图像,一个像素值的查找表,例如:
IMG = np.random.randint(0,9,(3,3,3))
LUT = np.random.randint(0,9,(1,3,3))
我想是要知道 X,Y
协调 LUT
像素,其值是常见的到 IMG
和 LUT
,所以我尝试:
中的xrange X(img.shape [0]):
在的xrange Y(img.shape [1]):
打印np.transpose(np.concatenate(np.where(LUT == IMG [X,Y])))
在这一点上,问题是 IMG [X,Y]
,这将是 [int_r,int_g,int_b形式]
未得到评估作为一个单一的元素,所以这三个组件获得 IMG
...
我想输出是这样的:
(x_coord,y_coord)
但我只在形式获取输出:
[0 0 0]
[0 2 1]
[0 0 2]
[0 0 0]
[0 0 0]
[0 0 2]
[0 0 1]
[0 2 2]
[0 1 2]
任何人都可以请帮助?谢谢!
IMG = np.random.randint(0,9,(3,3,3))
LUT2 = IMG [1,2 ,:]#使我们确切地知道答案#比较两个矩阵
IMG == LUT2阵列([[[假,假,假]
[假,假,假]
[假,真,假], [假,假,假]
[假,假,假]
[真,真,真], [真,假,假]
[真,假,假]
[假,假,假]]],DTYPE =布尔)与所有真正的#行是匹配的人
np.where((IMG == LUT2)的.sum(轴= 2)== 3)(阵列([1]),阵列([2]))
我真的不知道为什么LUT充满了随机数。但是,我认为你要寻找具有完全相同的颜色的像素。如果是这样,这似乎工作。这是你需要做什么呢?
I have two numpy arrays, one an RGB
image, one a lookup table of pixel values, for example:
img = np.random.randint(0, 9 , (3, 3, 3))
lut = np.random.randint(0, 9, (1,3,3))
What I'd like is to know the x,y
coordinate in lut
of pixels whose values are common to img
and lut
, so I tried:
for x in xrange(img.shape[0]):
for y in xrange(img.shape[1]):
print np.transpose(np.concatenate(np.where(lut == img[x,y])))
At this point, the problem is that img[x,y]
, which will be in the form of [int_r, int_g, int_b]
does not get evaluated as a single element, so the three components get sought for separately in img
...
I would like the output to be something like:
(x_coord, y_coord)
But I only get output in the form of:
[0 0 0]
[0 2 1]
[0 0 2]
[0 0 0]
[0 0 0]
[0 0 2]
[0 0 1]
[0 2 2]
[0 1 2]
Can anyone please help? Thanks!
img = np.random.randint(0, 9 , (3, 3, 3))
lut2 = img[1,2,:] # so that we know exactly the answer
# compare two matrices
img == lut2
array([[[False, False, False],
[False, False, False],
[False, True, False]],
[[False, False, False],
[False, False, False],
[ True, True, True]],
[[ True, False, False],
[ True, False, False],
[False, False, False]]], dtype=bool)
# rows with all true are the matching ones
np.where( (img == lut2).sum(axis=2) == 3 )
(array([1]), array([2]))
I don't really know why lut is filled with random numbers. But, I assume that you want to look for the pixels that have the exactly same color. If so, this seems to work. Is this what you need to do?
这篇关于越来越指数比较多维数组时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!