【问题标题】:Enumerate through 2D array in numpy and append to new array在numpy中枚举二维数组并附加到新数组
【发布时间】:2019-09-05 05:18:38
【问题描述】:

我正在尝试通过一个 2D numpy 形状数组 (512, 512) 进行枚举,该数组保存图像的像素值。所以基本上它是一个数组,表示图像的像素值的宽度和高度。我试图通过每个元素枚举输出:[y_index,x_index,pixel_value]。我需要将这 3 个输出值存储在一个数组中(现有的或新的,以执行效率更高者为准)。 所以输入数组的形状为 (512, 512),如果我没记错的话,输出数组的形状为 (262144, 3)。 262144 是 512x512 矩阵中的像素总数。 3 因为有 3 列,对于我需要提取的 3 条数据:pixel valuey coordinatex coordinate。所以基本上我想要一个像素值数组及其 y、x 坐标。

在下面的代码中,我使用ndenumerate 来枚举像素值(512、512)的 numpy 数组(img)。但我正在努力解决如何将输出值存储在数组中。我创建了coordinates 数组来存储输出值,但是我在最后一行的尝试显然不正确,无法达到预期的效果。那么如何解决这个问题呢?

img = as_np_array[:, :, 0]
img = img.reshape(512, 512)
coordinates = np.empty([262,144, 3])
for index, x in np.ndenumerate(img):
    coordinates = np.append(coordinates, [[x], index[0], index[1]])

我面临的另一个挑战是,要执行此代码,我的 Intel Core i7 2.7GHz(4 核)处理器大约需要 7-10 分钟(有时可能更长时间)才能执行。有没有更高效的代码可以执行得更快?

任何帮助将不胜感激。

【问题讨论】:

  • 你不能用三个变量来表示像素值,x坐标和y坐标。在这种情况下,您可以使用现有数组和ycoord, xcoord = numpy.mgrid[0:img.shape[0],0:img.shape[1]]
  • 感谢您的回复。对不起,但我不明白你的特定答案格式。基本上我需要一个包含 3 列的数组输出:pixel value,以及它的xy 坐标。我特别需要该格式的原因是因为我需要将其保存为 CSV 文件以具有特定的数据格式/结构。你有ycoordxcoord,但我需要一个数组输出中的所有内容。
  • 不一定:您也可以在 x 和 y 坐标上创建一个双循环,然后将一行 x、y、pixel_value 输出到 CSV 文件。在这种情况下,您既不需要那么大的二维数组,也不需要我的解决方案,只需要像 for x in range(512): for y in range(512): csv.write(x, y, img[y,x]) 这样的东西。
  • 非常感谢您的建议。我现在只是在尝试这段代码。我尝试将此代码作为一个小数组示例的测试运行:array2 = np.array([[1,1,1], [2,2,2], [3,3,3]]) with open('csv_test.csv', 'w', newline='') as f: thewriter = csv.writer(f) thewriter.writerow(['x_coord', 'y_coord', 'pixel_value']) for y in np.nditer(array2): for x in np.nditer(y): thewriter.writerow([x, y, array2[y,x]]),但出现错误:index 3 is out of bounds for axis 0 with size 3。不知道为什么会这样? np.nditer 是问题还是其他问题?
  • 因为np.nditer 会遍历大于一维的扁平数组。此外,您的代码不能反映我写的内容。我逐步浏览了数组的尺寸(形状);您逐步浏览数组本身,并使用 inside 的值来索引数组:for y in np.nditer(array2): ... array2[y,x]。那是行不通的(除了一些非常特殊的情况)。

标签: python arrays numpy enumeration numpy-ndarray


【解决方案1】:

您可以使用numpy.indices 来执行此操作。您最终想要的是 image_datayx 索引和相应的像素 (px)。 image_data 一共有三列。

row, col = np.indices(img.shape)
y, x, px = row.flatten(), col.flatten(), img.flatten()
image_data = np.array([y, x, px]).T

详细示例:

img = np.arange(20).reshape(5, 4)

def process_image_data(img):
    row, col = np.indices(img.shape)
    return (row.flatten(), col.flatten(), img.flatten())

y, x, px = process_image_data(img)

【讨论】:

    【解决方案2】:

    对我有用的解决方案是:

    with open('img_pixel_coor1.csv', 'w', newline='') as f:
        headernames = ['y_coord', 'x_coord', 'pixel_value']
        thewriter = csv.DictWriter(f, fieldnames=headernames)
        thewriter.writeheader()     
        for index, pix in np.ndenumerate(img):
            thewriter.writerow({'y_coord' : index[0], 'x_coord' : index[1], 'pixel_value' : pix})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-29
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      相关资源
      最近更新 更多