提取2D图像块#

sklearn.feature_extraction.image.extract_patches_2d(image, patch_size, *, max_patches=None, random_state=None)[source]#

将二维图像重塑为一系列图像块。

生成的图像块将分配在一个专用的数组中。

更多信息请参见用户指南

参数:
image形状为 (image_height, image_width) 或 (image_height, image_width, n_channels) 的ndarray

原始图像数据。对于彩色图像,最后一维指定通道:RGB图像的n_channels=3

patch_size整数元组 (patch_height, patch_width)

一个图像块的尺寸。

max_patchesint 或 float,默认为 None

要提取的最大图像块数量。如果max_patches是0到1之间的浮点数,则将其视为总图像块数量的比例。如果max_patches为None,则对应于可以提取的总图像块数量。

random_stateint,RandomState 实例,默认为 None

确定当max_patches不为 None 时用于随机采样的随机数生成器。使用整数可以使随机性确定性。参见词汇表

返回:
patches形状为 (n_patches, patch_height, patch_width) 或 (n_patches, patch_height, patch_width, n_channels) 的数组

从图像中提取的补丁集合,其中n_patches要么是max_patches,要么是可提取的补丁总数。

示例

>>> from sklearn.datasets import load_sample_image
>>> from sklearn.feature_extraction import image
>>> # Use the array data from the first image in this dataset:
>>> one_image = load_sample_image("china.jpg")
>>> print('Image shape: {}'.format(one_image.shape))
Image shape: (427, 640, 3)
>>> patches = image.extract_patches_2d(one_image, (2, 2))
>>> print('Patches shape: {}'.format(patches.shape))
Patches shape: (272214, 2, 2, 3)
>>> # Here are just two of these patches:
>>> print(patches[1])
[[[174 201 231]
  [174 201 231]]
 [[173 200 230]
  [173 200 230]]]
>>> print(patches[800])
[[[187 214 243]
  [188 215 244]]
 [[187 214 243]
  [188 215 244]]]