【问题标题】:Pairwise combinations of two image stacks两个图像堆栈的成对组合
【发布时间】:2023-01-25 00:47:41
【问题描述】:

我有 2 个 RGB 图像堆栈,每个图像堆栈包含 200 个图像。每个图像是 (300, 300, 3),所以每个堆栈是 (200, 300, 300, 3)。

所以我们有:

a_stack[200, 300, 300, 3]
b_stack[200, 300, 300, 3]

我的目标是计算每个堆栈中每对图像组合之间的欧几里德距离,我可以使用

measure = dist.euclidean(a_img.flatten(), b_img.flatten())

我的问题是构建适当的迭代器来获取 a_stackb_stack 之间的每个成对组合

我查看了itertools.combinations,但这似乎是针对字符串中元素的组合。 narrays有类似的东西吗?

【问题讨论】:

  • itertools 函数通常适用于任何可迭代对象,而不仅仅是字符串。因此,表达式 for a, b in itertools.combinations(a_stack, b_stack) 应该可以正常工作。但是,根据您的描述,您似乎在寻找itertools.product,而不是itertools.combinations
  • @PaulBrodersen 给出数组是 4D 的,那只会在第一维上创建产品吗?

标签: python combinations numpy-ndarray combinatorics


【解决方案1】:

欧几里得距离只是差平方和的根

squares = (a_stack - b_stack) ** 2
sum_of_squares = squares.sum(axis=[1, 2, 3])  # sum over the image axes
distance = np.sqrt(sum_of_squares)

【讨论】:

  • 获取距离不是问题,构造迭代器是
猜你喜欢
  • 2017-03-30
  • 2012-01-17
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 2020-11-17
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多