【问题标题】:Python - Perform Operations for Every Element in a 2D Array and Store it in a new 2D ArrayPython - 对 2D 数组中的每个元素执行操作并将其存储在新的 2D 数组中
【发布时间】:2018-04-11 04:11:53
【问题描述】:

我通过读取灰度图像得到二维数组。

image = cv2.imread('hurricane katrina 1.jpg', cv2.IMREAD_GRAYSCALE)

我想对二维数组中的每个元素执行操作并将其存储在另一个二维数组中。所以我做了下面的命令:

R = 255 * abs(math.sin(b * image))

它会抛出一个错误。

Traceback (most recent call last):
File "C:/Users/alyss/AppData/Local/Programs/Python/Python36/Exercise#4_2.py", line 25, in <module>
R = 255 * abs(math.sin(b * image))
TypeError: only size-1 arrays can be converted to Python scalars

我知道在 Python 中有一种无需循环的简单方法。我该怎么做?

【问题讨论】:

    标签: python arrays python-3.x numpy opencv


    【解决方案1】:

    我想对二维数组中的每个元素执行操作并将其存储在另一个二维数组中。

    使用 NumPy 库对 N 维数组执行操作。

    编辑: 这是一个代码 sn-p。

    255 * numpy.absolute(numpy.sin(b * image))
    

    【讨论】:

    • 我正在使用它。我该怎么做?
    • @Eliyah 一个很好的起点是 Numpy 文档。具体来说,mathematical functions 部分似乎非常相关,包含对每个提供的功能的详细描述,以及它们的使用示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 2022-06-10
    相关资源
    最近更新 更多