當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。