Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将numpy图像阵列切片为块_Python_Image Processing_Numpy_Computer Vision - Fatal编程技术网

Python 将numpy图像阵列切片为块

Python 将numpy图像阵列切片为块,python,image-processing,numpy,computer-vision,Python,Image Processing,Numpy,Computer Vision,我正在使用python进行目标检测的图像处理。我需要将我的图像分成所有可能的块。例如,给定此玩具图像: x = np.arange(25) x = x.reshape((5, 5)) [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19] [20 21 22 23 24]] 我想检索给定大小的所有可能的块,例如2x2块是: [[0 1] [5 6]] [[1 2] [6 7]] 。。等等我怎样才能

我正在使用python进行目标检测的图像处理。我需要将我的图像分成所有可能的块。例如,给定此玩具图像:

x = np.arange(25)
x = x.reshape((5, 5))

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]
我想检索给定大小的所有可能的块,例如2x2块是:

[[0 1]
 [5 6]]
[[1 2]
 [6 7]]

。。等等我怎样才能做到这一点呢?

您可以使用以下方法:

def rolling_window(arr, window):
    """Very basic multi dimensional rolling window. window should be the shape of
    of the desired subarrays. Window is either a scalar or a tuple of same size
    as `arr.shape`.
    """
    shape = np.array(arr.shape*2)
    strides = np.array(arr.strides*2)
    window = np.asarray(window)
    shape[arr.ndim:] = window # new dimensions size
    shape[:arr.ndim] -= window - 1
    if np.any(shape < 1):
        raise ValueError('window size is too large')
    return np.lib.stride_tricks.as_strided(arr, shape=shape, strides=strides)

# Now:
slices = rolling_window(arr, 2)
# Slices will be 4-d not 3-d as you wanted. You can reshape
# but it may need to copy (not if you have done no slicing, etc. with the array):
slices = slices.reshape(-1,slices.shape[2:])
def滚动窗口(arr,窗口):
“非常基本的多维滚动窗口。窗口的形状应为
窗口是一个标量或大小相同的元组
如“arr.shape”。
"""
shape=np.数组(arr.shape*2)
步长=np.数组(arr.strips*2)
窗口=np.asarray(窗口)
形状[arr.ndim:]=窗口#新尺寸
形状[:arr.ndim]-=窗口-1
如果np.有(形状<1):
raise VALUERROR('窗口大小太大')
返回np.lib.stride\u tricks.as\u striped(arr,shape=shape,stripes=stripes)
#现在:
切片=滚动窗口(arr,2)
#切片将是4-d,而不是您想要的3-d。你可以重塑
#但它可能需要复制(如果您没有对阵列进行切片等操作,则不需要复制):
切片=切片。重塑(-1,切片。形状[2:])

带有双循环和切片的简单代码:

>>> a = np.arange(12).reshape(3,4)
>>> print(a)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
>>> r = 2
>>> n_rows, n_cols = a.shape
>>> for row in range(n_rows - r + 1):
...     for col in range(n_cols - r + 1):
...         print(a[row:row + r, col:col + r])
...
[[0 1]
 [4 5]]
[[1 2]
 [5 6]]
[[2 3]
 [6 7]]
[[4 5]
 [8 9]]
[[ 5  6]
 [ 9 10]]
[[ 6  7]
 [10 11]]
scikit映像就是这样做的

>>> from sklearn.feature_extraction import image
>>> one_image = np.arange(16).reshape((4, 4))
>>> one_image
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
>>> patches = image.extract_patches_2d(one_image, (2, 2))
>>> print(patches.shape)
(9, 2, 2)
>>> patches[0]
array([[0, 1],
       [4, 5]])
>>> patches[1]
array([[1, 2],
       [5, 6]])
>>> patches[8]
array([[10, 11],
       [14, 15]])
很棒的python技能……:)很好用。