@@ -51,7 +51,7 @@ def _make_edges_3d(n_x, n_y, n_z=1):
51
51
52
52
53
53
def _compute_gradient_3d (edges , img ):
54
- n_x , n_y , n_z = img .shape
54
+ _ , n_y , n_z = img .shape
55
55
gradient = np .abs (img [edges [0 ] // (n_y * n_z ),
56
56
(edges [0 ] % (n_y * n_z )) // n_z ,
57
57
(edges [0 ] % (n_y * n_z )) % n_z ] -
@@ -331,33 +331,33 @@ def extract_patches_2d(image, patch_size, max_patches=None, random_state=None):
331
331
Returns
332
332
-------
333
333
patches : array, shape = (n_patches, patch_height, patch_width) or
334
- (n_patches, patch_height, patch_width, n_channels)
335
- The collection of patches extracted from the image, where `n_patches`
336
- is either `max_patches` or the total number of patches that can be
337
- extracted.
334
+ (n_patches, patch_height, patch_width, n_channels)
335
+ The collection of patches extracted from the image, where `n_patches`
336
+ is either `max_patches` or the total number of patches that can be
337
+ extracted.
338
338
339
339
Examples
340
340
--------
341
- >>> import numpy as np
341
+ >>> from sklearn.datasets import load_sample_images
342
342
>>> from sklearn.feature_extraction import image
343
- >>> one_image = np.arange(16).reshape((4, 4))
344
- >>> one_image
345
- array([[ 0, 1, 2, 3],
346
- [ 4, 5, 6, 7],
347
- [ 8, 9, 10, 11],
348
- [12, 13, 14, 15]])
343
+ >>> # Use the array data from the first image in this dataset:
344
+ >>> one_image = load_sample_images().images[0]
345
+ >>> print('Image shape: {}'.format(one_image.shape))
346
+ Image shape: (427, 640, 3)
349
347
>>> patches = image.extract_patches_2d(one_image, (2, 2))
350
- >>> patches.shape
351
- (9, 2, 2)
352
- >>> patches[0]
353
- array([[0, 1],
354
- [4, 5]])
355
- >>> patches[1]
356
- array([[1, 2],
357
- [5, 6]])
358
- >>> patches[8]
359
- array([[10, 11],
360
- [14, 15]])
348
+ >>> print('Patches shape: {}'.format(patches.shape))
349
+ Patches shape: (272214, 2, 2, 3)
350
+ >>> # Here are just two of these patches:
351
+ >>> print(patches[1]) # doctest: +NORMALIZE_WHITESPACE
352
+ [[[174 201 231]
353
+ [174 201 231]]
354
+ [[173 200 230]
355
+ [173 200 230]]]
356
+ >>> print(patches[800])# doctest: +NORMALIZE_WHITESPACE
357
+ [[[187 214 243]
358
+ [188 215 244]]
359
+ [[187 214 243]
360
+ [188 215 244]]]
361
361
"""
362
362
i_h , i_w = image .shape [:2 ]
363
363
p_h , p_w = patch_size
@@ -420,7 +420,6 @@ def reconstruct_from_patches_2d(patches, image_size):
420
420
-------
421
421
image : array, shape = image_size
422
422
the reconstructed image
423
-
424
423
"""
425
424
i_h , i_w = image_size [:2 ]
426
425
p_h , p_w = patches .shape [1 :3 ]
@@ -461,7 +460,21 @@ class PatchExtractor(BaseEstimator):
461
460
If None, the random number generator is the RandomState instance used
462
461
by `np.random`.
463
462
463
+ Examples
464
+ --------
465
+ >>> from sklearn.datasets import load_sample_images
466
+ >>> from sklearn.feature_extraction import image
467
+ >>> # Use the array data from the second image in this dataset:
468
+ >>> X = load_sample_images().images[1]
469
+ >>> print('Image shape: {}'.format(X.shape))
470
+ Image shape: (427, 640, 3)
471
+ >>> pe = image.PatchExtractor(patch_size=(2, 2))
472
+ >>> pe_fit = pe.fit(X)
473
+ >>> pe_trans = pe.transform(X)
474
+ >>> print('Patches shape: {}'.format(pe_trans.shape))
475
+ Patches shape: (545706, 2, 2)
464
476
"""
477
+
465
478
def __init__ (self , patch_size = None , max_patches = None , random_state = None ):
466
479
self .patch_size = patch_size
467
480
self .max_patches = max_patches
@@ -498,7 +511,6 @@ def transform(self, X):
498
511
The collection of patches extracted from the images, where
499
512
`n_patches` is either `n_samples * max_patches` or the total
500
513
number of patches that can be extracted.
501
-
502
514
"""
503
515
self .random_state = check_random_state (self .random_state )
504
516
n_images , i_h , i_w = X .shape [:3 ]
0 commit comments