使用 np.where 在 numpy 数组中查找唯一的元组

班克斯

我想在带有 np.where 的 numpy 数组中找到唯一的颜色元组。到目前为止我的代码是:

from __future__ import print_function
import numpy as np 

a = range(10)
b = range(10,20)
c = range(20,30)
d = np.array(zip(a, b, c))
print(d)
e = np.array(zip(c, b, a))
print(e)
search = np.array((1,11,21))
search2 = np.array((0,11,21))
print(search, search2)
f = np.where(d == search, d, e)
g = np.where(d == search2, d, e)
print(f)
print(g)

当我运行代码时,它正确地找到了第二个位置的元组搜索。但是元组 search2 也位于第一个位置,尽管它没有作为唯一的元组包含在数组中。我如何在 numpy 中定义在数组中只能找到唯一的元组,以便为​​ g 提供一些值

[[20 10  0]  [21 11  1]  [22 12  2]  
 [23 13  3]  [24 14  4]  [25 15  5]  
 [26 16  6]  [27 17  7]  [28 18  8]  [29 19  9]]

但仍然找到唯一的元组搜索并给出 f

[[20 10  0]  [ 1 11 21]  [22 12  2]  
 [23 13  3]  [24 14  4]  [25 15  5]  
 [26 16  6]   [27 17  7]  [28 18  8]  [29 19  9]]

?

编辑:

OK,那么现在的问题是用python写一个GIF解码器。我有一个名为 image_a 的前一帧和一个名为 image_b 的后一帧。image_b 包含具有特定透明度颜色元组的像素,在这种特定情况下,对于上传的图像称为 transp_color,它是 (0, 16, 8)。该例程应该用来自 image_a 的像素值的颜色元组替换所有这些条目,但保持其他像素不变。我的代码是:

from __future__ import print_function
import numpy as np 
import cv2

image_a = cv2.imread("old_frame.png")
image_b = cv2.imread("new_frame.png")
cv2.imshow("image_a", image_a)
cv2.imshow("image_b", image_b)
transp_color = (0, 16, 8)
new_image = np.where(image_b == transp_color, image_a, image_b)
cv2.imshow("new_image", new_image)
cv2.waitKey()

old_frame.png 新框架.png 新图像.png

尝试使用 np.where 解决这个问题会导致结果图像中的颜色错误,如上图 3 所示。那么知道如何解决这个问题吗?

班克斯

好的,终于自己找到了解决方案,下面是代码:

import numpy as np
import cv2

image_a = cv2.imread("old_frame.png")
image_b = cv2.imread("new_frame.png")
cv2.imshow("image_a", image_a)
cv2.imshow("image_b", image_b)
transp_color = (0, 16, 8)[::-1]
channels = 3
f = np.all((image_b==transp_color), axis=-1)
flattened_image = np.reshape(image_b, (image_b.shape[0]*image_b.shape[1], channels))
old_flattened_image = np.reshape(image_a, (image_a.shape[0]*image_a.shape[1], channels))
f = np.reshape(f, (image_a.shape[0]*image_a.shape[1], 1))
np_image = np.array([old_flattened_image[i] if j else flattened_image[i] for i, j in enumerate(f)])
new_image = np.reshape(np_image, (image_a.shape[0], image_a.shape[1], channels))

# new_image = np.where(image_b == transp_color, image_a, image_b)

cv2.imshow("new_image", new_image)
cv2.waitKey()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

np.where 中的 numpy 广播

如何使用 np.where for 循环替换数组 numpy 中的值?

使用np.where从子数组中查找元素

分配给numpy数组时避免np.where

在3D numpy数组中查找唯一的元组

np.where()未在numpy python中返回预期的索引

Numpy 对 Python 中 np.where() 索引的快速迭代

使用基于另一个维度的 np.where 设置一个 numpy 切片

查找 numpy.where 返回的数组之间的唯一索引

使用np.where在2D数组中查找匹配的行

使用np.where在2D数组中查找元素的索引给出ValueError

np.where返回一个包含numpy数组的空数组

如何使用`np.where()`比较数组而不是单个值

使用np.where遍历多个数组

如何使用np.where()创建特定行的新数组?

在NumPy中,如何不使用np.s_从一维数组中提取范围?

使用np.where在matplotlib中按类别着色

如何将 np.append 与 np.where 方法一起使用?

np.add 让我头疼。如何使用 np.add 中的 where 选项?

np.where 不会更改给定列表列表的 numpy 数组

如何嵌套numpy()的np.where或另一个嵌套?

在numpy元组数组中的给定位置查找唯一值

在多维数组上使用numpy.where

在多维数组上使用 numpy where

numpy np.where扫描数组一次以获取对应于T / F条件的两组索引

numpy np.where扫描数组一次以获取对应于T / F条件的两组索引

如何将 np.where 函数与数组每个元素的索引一起使用?

numpy从np数组中删除维度

使用np.unique从2个Numpy数组中删除成对重复项