【问题标题】:It there a way to get a matrix of maximum values in numpy?有没有办法在numpy中获得最大值矩阵?
【发布时间】:2020-02-24 14:54:49
【问题描述】:

问题是我有一个包含 RGB 通道的图像。 我想从中得到一个矩阵,该矩阵具有跨轴图像的最大值 - 这是通道。

例如

np.random.seed(42)
rand=np.random.randint(low=0, high=255, size=(4,8,3), dtype="uint8")
rand[0][np.newaxis,:,:]=
  [[[102, 220, 225],
    [ 95, 179,  61],
    [234, 203,  92],
    [  3,  98, 243],
    [ 14, 149, 245],
    [ 46, 106, 244],
    [ 99, 187,  71],
    [212, 153, 199]]

从这里我想得到一个矩阵,它看起来像这样 [[220],[179],[234],[243],[245],[244],[187],[212]]。

Argmax 为我提供了这些值的索引。

 np.argmax(exp,2) #array([[2, 1, 0, 2, 2, 2, 1, 0]], dtype=int64)

如何使用此信息自己获取值或有任何其他方法来获取它们?

同样的问题也适用于整个矩阵,而不仅仅是一行。

【问题讨论】:

  • 要直接获取这些最大值,请使用rand.max(axis=-1)?
  • @Divakar,是的,这是一个答案,谢谢。由于某种原因发现只有 amax 和 argmax 函数...
  • 那么,您对amax 做了什么尝试?
  • 可能我忘记使用轴了 :) 它只给了我最大数量。

标签: python numpy image-processing matrix


【解决方案1】:

我想我找到了答案 - 它就像 here 这样的 np.max(rand,axis=2) 一样简单。

就像在@Divakar 评论中一样 - 使用 rand.max(axis=-1) 的另一种方式。

【讨论】:

    猜你喜欢
    • 2019-04-02
    • 2021-07-27
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多