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_size整數元組 (patch_height, patch_width)

一個圖塊的尺寸。

max_patches整數或浮點數,預設值為 None

要提取的最大圖塊數。如果 max_patches 是介於 0 和 1 之間的浮點數,則會將其視為圖塊總數的比例。如果 max_patches 為 None,則表示可提取的圖塊總數。

random_state整數、RandomState 實例,預設值為 None

max_patches 不為 None 時,決定用於隨機取樣的隨機數字產生器。使用整數可使隨機性具確定性。請參閱詞彙表

傳回值:
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]]]