从索引位置周围的矩阵中提取块

2024-09-30 05:18:18 发布

您现在位置:Python中文网/ 问答频道 /正文

(在python3中)如何从以每个元素为中心的m-by-n数组中提取几个n-by-m块

例如,在9×9矩阵中,使用3×3块,我需要在矩阵的每个位置提取块。这里的挑战(对我来说)是因为他各自区块内的I,J元素改变了位置

在这里,我展示了一个9乘9矩阵的9个块(9个位置)(当然有81个块要提取)

Here an image

下面的代码(仅适用于)在拐角处非常有效。在这里,块的大小(WSIWS按WSIZE)是奇数,以便在中间找到索引(eindex(0),eindex 1)。p>

def windowing(array,Wsize,Eindex):
    '''
    Extract an sub-array of array for the element located in the index 'Eindex'.
    Wsize-by-Wsize is the shape of the window 
    '''
    block=np.zeros(tuple(Wsize))
    k0 = int((Wsize[0]-1)/2)
    k1 = int((Wsize[1]-1)/2) 
    s0 =  Wsize[0]
    s1 =  Wsize[1]
    I =  array.shape[0]-1 # las index in i-direction
    J =  array.shape[1]-1 # las index in i-direction
    if (Eindex[0]==0) and (Eindex[1]==0):
        block=array[0:Eindex[0]+s0,0:Eindex[1]+s1]
        return block
    elif (Eindex[0]==I) and (Eindex[1]==0):
        block=array[-s0:,0:Eindex[1]+s1]
        return block
    elif (Eindex[0]==0) and (Eindex[1]==J):
        block=array[0:Eindex[0]+s0,-s1:]
        return block
    elif (Eindex[0]==I) and (Eindex[1]==J):
        block=array[-s0:,-s1:]
        return block

例如,检查:

x = np.arange(81).reshape(9,9)
print(windowing(x,[3,3],[0,0]))
print(windowing(x,[3,3],[8,8))
print(windowing(x,[3,3],[8,0]))
print(windowing(x,[3,3],[0,8]))

Tags: andtheinindexbyreturn矩阵block
2条回答

这是一种采用任意数组、坐标和窗口大小的方法

def compute_indices(c, ws, length):

    # default setting: % operations to accommodate odd/even window sizes
    low, high = c - (ws//2), c + (ws//2) + ws%2 

    # correction for overlap with borders of array
    if low<0:
        low, high = 0, ws
    elif high>length:
        low, high = -ws, None

    return low, high


def extract_block(arr, coords, window_size=(3,3)):

    # extract array shapes and window sizes into single 
    # variables
    len_r, len_c = arr.shape
    wsr, wsc = window_size

    # extract coords and correct for 0-indexing
    r, c = coords
    r0, c0 = r-1, c-1

    row_low, row_high = compute_indices(r0, wsr, len_r)
    col_low, col_high = compute_indices(c0, wsc, len_c)

    return arr[row_low:row_high, col_low:col_high]

测试用例:

a = np.arange(81).reshape(9,9)

extract_block(a, [1,1], (3,3))
array[[ 0  1  2]
     [ 9 10 11]
     [18 19 20]]

extract_block(a, [9,9], (3,3))
array([[60, 61, 62],
       [69, 70, 71],
       [78, 79, 80]])

extract_block(a, [5,2], (3,6))
array([[27, 28, 29, 30, 31, 32],
       [36, 37, 38, 39, 40, 41],
       [45, 46, 47, 48, 49, 50]])

您可以像这样使用numpy

import numpy as np
# Array 9 by 9
x = np.arange(81).reshape((9, 9))
# -1 is important for the indexing
desired_position = np.array([[1,1], [1,5], [1,9], [5,1], [5,5], [5,9], [9,1], [9,5], [9,9]]) - 1
#print(desired_position)
for dp in desired_position:
    pos = []
    p1, p2 =dp[0] - 1, dp[0] + 2
    if p1 <= 0:
        p1, p2 = 0, 3
    elif p2 >= x.shape[0]:
        p2, p1 = x.shape[0], x.shape[0] - 3
    pos.append([p1, p2])

    p1, p2 = dp[1] - 1, dp[1] + 2
    if p1 <= 0:
        p1, p2 = 0, 3
    elif p2 >= x.shape[1]:
        p2, p1 = x.shape[1], x.shape[1] - 3
    pos.append([p1, p2])
    print(x[pos[0][0]:pos[0][1],pos[1][0]:pos[1][1]])

请阅读numpy的docs了解更多信息

我编辑了代码,所以现在它可以工作了

相关问题 更多 >

    热门问题