从python中的图像中有效地提取特定大小的补丁

Sha*_*han 2 python image image-processing

我有一个图像,我想从中提取不同大小的方形补丁.

我需要密集的补丁,也就是说,我需要在图像的每个像素处都有一个补丁.

例如,如果图像是100x100,并且补丁大小是64.

结果将是10000大小的补丁64x64

这些是我们用于过滤操作的补丁,例如.

如果有边界我想镜像图像.

使用python提取补丁的最有效方法是什么?

谢谢

Ste*_*alt 13

我想你正在寻找这样的东西:

http://scikit-image.org/docs/0.9.x/api/skimage.util.html#view-as-windows

  • 您可以使用``skimage.util.pad``预先添加首选边界. (2认同)

Mar*_*oma 10

sklearn

你可能想看看sklearn.feature_extraction.image.extract_patches_2dskimage.util.pad:

>>> from sklearn.feature_extraction.image import extract_patches_2d
>>> import numpy as np
>>> A = np.arange(4*4).reshape(4,4)
>>> window_shape = (2, 2)
>>> B = extract_patches_2d(A, window_shape)
>>> B[0]
array([[0, 1],
       [4, 5]])
>>> B
array([[[ 0,  1],
        [ 4,  5]],

       [[ 1,  2],
        [ 5,  6]],

       [[ 2,  3],
        [ 6,  7]],

       [[ 4,  5],
        [ 8,  9]],

       [[ 5,  6],
        [ 9, 10]],

       [[ 6,  7],
        [10, 11]],

       [[ 8,  9],
        [12, 13]],

       [[ 9, 10],
        [13, 14]],

       [[10, 11],
        [14, 15]]])
Run Code Online (Sandbox Code Playgroud)

skimage

扩大了Stefan van der Walt的答案:

安装skimage

在Ubuntu上

$ sudo apt-get install python-skimage
Run Code Online (Sandbox Code Playgroud)

要么

$ pip install scikit-image
Run Code Online (Sandbox Code Playgroud)

来自文档的示例

>>> from skimage.util import view_as_windows
>>> import numpy as np
>>> A = np.arange(4*4).reshape(4,4)
>>> A
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
>>> window_shape = (2, 2)
>>> B = view_as_windows(A, window_shape)
>>> B[0]
array([[[0, 1],
        [4, 5]],

       [[1, 2],
        [5, 6]],

       [[2, 3],
        [6, 7]]])

>>> B
array([[[[ 0,  1],
         [ 4,  5]],

        [[ 1,  2],
         [ 5,  6]],

        [[ 2,  3],
         [ 6,  7]]],


       [[[ 4,  5],
         [ 8,  9]],

        [[ 5,  6],
         [ 9, 10]],

        [[ 6,  7],
         [10, 11]]],


       [[[ 8,  9],
         [12, 13]],

        [[ 9, 10],
         [13, 14]],

        [[10, 11],
         [14, 15]]]])
Run Code Online (Sandbox Code Playgroud)