【问题标题】:Python, access to certain values in arrayPython,访问数组中的某些值
【发布时间】:2021-10-20 01:48:16
【问题描述】:

我有一个坐标数组 (y,x) 引用图像 fig300[0,:,:] 中的特定像素。 我想在我的图像中找到这些坐标,并为这些像素改变它们的强度。

我的数组的输出,称为 brighter_spot 是这样的:

array([[116, 159],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [134, 380],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106],
       [ 90, 752],
       [116, 159],
       [235, 206],
       [508, 106]], dtype=int64)

我以这种方式构建了 for 循环:

for y,x in brighter_spot:
        if fig300[0,y,x] == brighter_spot.any():
            fig300[0,y,x] = 0

for 循环没有任何效果,我认为它在比较每一对的数组中不能正常运行。 我该如何解决?

【问题讨论】:

  • 对不起,for循环的第一行没有“范围”这个词,这是一个打字错误。

标签: python arrays image for-loop pixel


【解决方案1】:

试试这个:

for y in range(brighter_spot):
    for x in range(y):
        if np.isin(brighter_spot, fig300[0, y, x]).any():
            fig300[0,y,x] = 0

【讨论】:

  • 这种方式报错信息为:TypeError: only integer scalar arrays can be convert to a scalar index
【解决方案2】:

“我想在我的图像中找到这些坐标,并为这些像素改变它们的强度”。

简单地做

for x,y in brighter_spot:
    fig300[0,y,x] = ...

不确定您想通过brighter_spot.any() 实现什么目标。你应该检查文档:https://numpy.org/doc/stable/reference/generated/numpy.any.html

这会在数组中查找任何非 None 值。在您的情况下,它始终返回true,然后您在每次迭代时将像素值与布尔值进行比较,始终返回False。这就是为什么 for 循环执行器没有任何效果的原因。

【讨论】:

    猜你喜欢
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多