【问题标题】:Subtract the average of first and last value of each row from all values in the row从行中的所有值中减去每行的第一个和最后一个值的平均值
【发布时间】:2018-03-07 01:58:35
【问题描述】:

我有一个如下所示的 numpy 数组:

77.132  2.075   63.365  74.880
49.851  22.480  19.806  76.053
16.911  8.834   68.536  95.339
0.395   51.219  81.262  61.253
72.176  29.188  91.777  71.458
54.254  14.217  37.334  67.413
44.183  43.401  61.777  51.314
65.040  60.104  80.522  52.165
90.865  31.924  9.046   30.070
11.398  82.868  4.690   62.629

而我想要做的是

  • 求每行第一项和最后一项的平均值
  • 从该行中的每个像素中减去该平均值
  • 对每一行重复
  • 创建一个减去像素的新图像。

我已经尝试使用 for 循环,但我无法让它工作:

import numpy as np

#   Create random arrays to simulate images
np.random.seed(10)
image = 100 * np.random.rand(10, 4)

no_disk_list = []

#for row in image:
#    left, right =   row[0], row[-1]
#    average = (left + right) / 2.0
#    for i in row:
#        no_average = row[i] - average
#        print(average)
#        no_disk_list.append(no_average)

subtracted = np.ones_like(image)
height, width = image.shape
for row in image:
    left, right =   image[0], image[-1]
    average = (left + right) / 2.0
    for element in row:
        subtracted[row, element] = image[row, element] - average

两个嵌套循环都报错:

  File "C:/Users/Jeremy/Dropbox/Astro480/NEOWISE/subtract_disk.py", line 17, in <module>
    no_disk_value = row[i] - disk_value

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

对于第一个循环和

  File "C:/Users/Jeremy/Dropbox/Astro480/NEOWISE/subtract_pixels.py", line 23, in <module>
    print(image[row, element])

IndexError: arrays used as indices must be of integer (or boolean) type

第二个。问题hereherehere 在我的情况下用途有限。此外,我知道矢量化会是更好的方法,因为我最终将使用的图像有 130 万像素。如何使循环工作,甚至更好地矢量化计算?

【问题讨论】:

    标签: python arrays list numpy for-loop


    【解决方案1】:

    如果我正确理解了这个问题,这将起作用:

    subtracted = np.ones_like(image)
    height, width = image.shape
    for row_no, row in enumerate(image):   # keep the row number using enumerate
        left, right = row[0], row[-1]      # you need the first and last value of the ROW!
        average = (left + right) / 2.0
        # Also use enumerate in the inner loop
        for col_no, element in enumerate(row):
            subtracted[row_no, col_no] = element - average
    

    您甚至可以使用广播(“矢量化”)来大大缩短它:

    subtracted = image - (image[:, [0]] + image[:, [-1]]) / 2
    

    image[:, [0]] 是第一列,image[:, [-1]] 是最后一列。通过将它们相加并除以 2,您将得到一个包含每行平均值的 2D 数组。最后一步是从图像中减去它,在这种情况下这很容易,因为它会正确广播。

    一步一步:

    >>> arr = np.arange(20).reshape(4, 5)
    >>> arr
    array([[ 0,  1,  2,  3,  4],
           [ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14],
           [15, 16, 17, 18, 19]])
    >>> arr[:, [0]]  # first column
    array([[ 0],
           [ 5],
           [10],
           [15]])
    >>> arr[:, [-1]]  # last column
    array([[ 4],
           [ 9],
           [14],
           [19]])
    >>> (arr[:, [0]] + arr[:, [-1]]) / 2   # average
    array([[  2.],
           [  7.],
           [ 12.],
           [ 17.]])
    >>> arr - (arr[:, [0]] + arr[:, [-1]]) / 2  # subtracted
    array([[-2., -1.,  0.,  1.,  2.],
           [-2., -1.,  0.,  1.,  2.],
           [-2., -1.,  0.,  1.,  2.],
           [-2., -1.,  0.,  1.,  2.]])
    

    【讨论】:

    猜你喜欢
    • 2019-03-12
    • 2014-11-22
    • 2016-05-23
    • 2019-09-09
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多