sklearn.feature_extraction.extract_patches_2d

sklearn.feature_extraction.image.extract_patches_2d(image, patch_size, *, max_patches=None, random_state=None)

[源码]

将二维图像重塑为一组补丁

产生的补丁被分配在一个专用的阵列中。

用户指南中阅读更多内容。

参数 说明
image ndarray of shape (image_height, image_width) or (image_height, image_width, n_channels)
原始图像数据。对于彩色图像,最后一个维度指定了通道:RGB图像的n_channels=3。
patch_size tuple of int (patch_height, patch_width)
一个patch的尺寸。
max_patches int or float, default=None
要提取的最大patch数。如果max_patch是0到1之间的浮点数,则它被认为是补丁总数的一个比例。
random_state int, RandomState instance, default=None
确定当max_patch不是None时用于随机采样的随机数生成器。使用int使随机性具有确定性。详见术语表
返回值 说明
patches array of shape (n_patches, patch_height, patch_width) or (n_patches, patch_height, patch_width, n_channels)
从图像中提取的patch集合,其中n_patch为max_patch或可提取的patch总数。

示例

>>> 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: (4276403)
>>> patches = image.extract_patches_2d(one_image, (22))
>>> print('Patches shape: {}'.format(patches.shape))
Patches shape: (272214223)
>>> # 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]]]

示例sklearn.feature_extraction.image.extract_patches_2d