extract_patches_2d#
- sklearn.feature_extraction.image.extract_patches_2d(image, patch_size, *, max_patches=None, random_state=None)[源码]#
将 2D 图像重塑为一组图像块。
生成的图像块被分配到专用数组中。
在用户指南中阅读更多内容。
- 参数:
- image形状为 (image_height, image_width) 或 (image_height, image_width, n_channels) 的 ndarray
原始图像数据。对于彩色图像,最后一个维度指定通道:RGB 图像将具有
n_channels=3
。- patch_sizeint 类型元组 (patch_height, patch_width)
单个图像块的尺寸。
- max_patchesint 或 float,默认值=None
要提取的最大图像块数量。如果
max_patches
是介于 0 和 1 之间的浮点数,则表示为总图像块数量的比例。如果max_patches
为 None,则表示可以提取的图像块总数。- random_stateint, RandomState 实例, 默认值=None
当
max_patches
不为 None 时,确定用于随机采样的随机数生成器。使用 int 类型可使随机性确定。请参阅术语表。
- 返回:
- 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]]]