当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Python sklearn extract_patches_2d用法及代码示例

本文简要介绍python语言中 sklearn.feature_extraction.image.extract_patches_2d 的用法。

用法:

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

将 2D 图像重塑为补丁集合

生成的补丁分配在一个专用数组中。

在用户指南中阅读更多信息。

参数

imagendarray 形状 (image_height, image_width) 或 (image_height, image_width, n_channels)

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

patch_sizeint 的元组 (patch_height, patch_width)

一个补丁的尺寸。

max_patchesint 或浮点数,默认=无

要提取的最大补丁数。如果max_patches 是介于 0 和 1 之间的浮点数,则将其视为补丁总数的比例。

random_stateint,RandomState 实例,默认=无

确定当 max_patches 不为 None 时用于随机采样的随机数生成器。使用int 使随机性具有确定性。请参阅术语表。

返回

patches形状数组 (n_patches, patch_height, patch_width) 或 (n_patches, patch_height, patch_width, n_channels)

从图像中提取的补丁集合,其中n_patchesmax_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]]]

相关用法


注:本文由纯净天空筛选整理自scikit-learn.org大神的英文原创作品 sklearn.feature_extraction.image.extract_patches_2d。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。