【问题标题】:How to calculate the offset based on xy coordinates to read from numpy.memmap?如何根据 xy 坐标计算偏移量以从 numpy.memmap 读取?
【发布时间】:2017-02-09 13:49:19
【问题描述】:

我使用numpy.memmap 创建了大numpy array,如下所示。

import numpy as np
memmapData = np.memmap('test.memmap', dtype='uint8', mode='w+', shape=(10000, 10000, 3))

现在如何根据一些 XY 坐标读取数组。看完之后,我需要画一个矩形。

(即)X1=0,Y1=1000 X2=X1+500,Y2=Y1+500。那么现在我如何通过上面的坐标从test.memmap 读取numpy 数组。在这我需要如何使用 numpy.memmap 属性 offset

【问题讨论】:

    标签: python arrays numpy offset


    【解决方案1】:

    为什么需要使用offset?你不能像普通数组一样索引它吗?

    In [221]: memmapData = np.memmap('test.memmap', dtype='uint8', mode='w+', shape=
         ...: (10, 10, 3))
    
    In [222]: memmapData[...]=np.arange(100).reshape(10,10)[:,:,None]
    
    In [223]: memmapData[0,5,:]
    Out[223]: memmap([5, 5, 5], dtype=uint8)
    
    In [224]: memmapData[0,5:,:]
    Out[224]: 
    memmap([[5, 5, 5],
           [6, 6, 6],
           [7, 7, 7],
           [8, 8, 8],
           [9, 9, 9]], dtype=uint8)
    

    offset 是您在创建地图时可以提供的参数。它是固定的。您不必摆弄它来访问元素。

    【讨论】:

    • @hpaulj 你能简单解释一下你的代码吗?
    猜你喜欢
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 2018-08-28
    • 2021-08-04
    • 1970-01-01
    相关资源
    最近更新 更多