Skip to content

Commit 4af195d

Browse files
committed
features.image_patches --> utils.extract_patches
1 parent d54a8b2 commit 4af195d

File tree

3 files changed

+69
-69
lines changed

3 files changed

+69
-69
lines changed

pygsp/features.py

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from .graphs import Graph
1111
from .filters import Filter
1212
from .utils import filterbank_handler
13-
from skimage.util import view_as_windows, pad
1413

1514

1615
def compute_avg_adj_deg(G):
@@ -103,69 +102,3 @@ def atom(x):
103102

104103
G.spectr = spectr
105104
return spectr
106-
107-
108-
def patch_features(img, patch_shape=(3, 3)):
109-
r"""
110-
Compute a patch feature vector for every pixel of an image.
111-
112-
Parameters
113-
----------
114-
img : array
115-
Input image.
116-
patch_shape : tuple, optional
117-
Dimensions of the patch window. Syntax: (height, width), or (height,),
118-
in which case width = height.
119-
120-
Returns
121-
-------
122-
array
123-
Feature matrix.
124-
125-
Notes
126-
-----
127-
The feature vector of a pixel `i` will consist of the stacking of the
128-
intensity values of all pixels in the patch centered at `i`, for all color
129-
channels. So, if the input image has `d` color channels, the dimension of
130-
the feature vector of each pixel is (patch_shape[0] * patch_shape[1] * d).
131-
132-
Examples
133-
--------
134-
>>> from pygsp import features
135-
>>> from skimage import data, img_as_float
136-
>>> img = img_as_float(data.camera()[::2, ::2])
137-
>>> X = features.patch_features(img)
138-
139-
"""
140-
141-
try:
142-
h, w, d = img.shape
143-
except ValueError:
144-
try:
145-
h, w = img.shape
146-
d = 0
147-
except ValueError:
148-
print("Image should be at least a 2-d array.")
149-
150-
try:
151-
r, c = patch_shape
152-
except ValueError:
153-
r = patch_shape[0]
154-
c = r
155-
if d == 0:
156-
pad_width = ((int((r - 0.5) / 2.), int((r + 0.5) / 2.)),
157-
(int((c - 0.5) / 2.), int((c + 0.5) / 2.)))
158-
window_shape = (r, c)
159-
d = 1 # For the reshape in the return call
160-
else:
161-
pad_width = ((int((r - 0.5) / 2.), int((r + 0.5) / 2.)),
162-
(int((c - 0.5) / 2.), int((c + 0.5) / 2.)),
163-
(0, 0))
164-
window_shape = (r, c, d)
165-
# Pad the image
166-
img_pad = pad(img, pad_width=pad_width, mode='symmetric')
167-
168-
# Extract patches
169-
patches = view_as_windows(img_pad, window_shape=window_shape)
170-
171-
return patches.reshape((h * w, r * c * d))

pygsp/graphs/nngraphs/imgpatches.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
from pygsp.graphs import NNGraph
4-
from pygsp.features import patch_features
4+
from pygsp import utils
55

66

77
class ImgPatches(NNGraph):
@@ -33,7 +33,7 @@ class ImgPatches(NNGraph):
3333
def __init__(self, img, patch_shape=(3, 3), n_nbrs=8, use_flann=True,
3434
dist_type='euclidean', symmetrize_type='full', **kwargs):
3535

36-
X = patch_features(img, patch_shape=patch_shape)
36+
X = utils.extract_patches(img, patch_shape=patch_shape)
3737

3838
super(ImgPatches, self).__init__(X,
3939
use_flann=use_flann,

pygsp/utils.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from scipy import kron, ones
1919
from scipy import sparse
2020
from scipy.io import loadmat as scipy_loadmat
21+
import skimage
2122

2223

2324
def build_logger(name, **kwargs):
@@ -391,6 +392,72 @@ def vec2mat(d, Nf):
391392
return np.reshape(d, (M / Nf, Nf, N), order='F')
392393

393394

395+
def extract_patches(img, patch_shape=(3, 3)):
396+
r"""
397+
Extract a patch feature vector for every pixel of an image.
398+
399+
Parameters
400+
----------
401+
img : array
402+
Input image.
403+
patch_shape : tuple, optional
404+
Dimensions of the patch window. Syntax: (height, width), or (height,),
405+
in which case width = height.
406+
407+
Returns
408+
-------
409+
array
410+
Feature matrix.
411+
412+
Notes
413+
-----
414+
The feature vector of a pixel `i` will consist of the stacking of the
415+
intensity values of all pixels in the patch centered at `i`, for all color
416+
channels. So, if the input image has `d` color channels, the dimension of
417+
the feature vector of each pixel is (patch_shape[0] * patch_shape[1] * d).
418+
419+
Examples
420+
--------
421+
>>> from pygsp import utils
422+
>>> import skimage
423+
>>> img = skimage.img_as_float(skimage.data.camera()[::2, ::2])
424+
>>> X = utils.extract_patches(img)
425+
426+
"""
427+
428+
try:
429+
h, w, d = img.shape
430+
except ValueError:
431+
try:
432+
h, w = img.shape
433+
d = 0
434+
except ValueError:
435+
print("Image should be at least a 2-d array.")
436+
437+
try:
438+
r, c = patch_shape
439+
except ValueError:
440+
r = patch_shape[0]
441+
c = r
442+
if d == 0:
443+
pad_width = ((int((r - 0.5) / 2.), int((r + 0.5) / 2.)),
444+
(int((c - 0.5) / 2.), int((c + 0.5) / 2.)))
445+
window_shape = (r, c)
446+
d = 1 # For the reshape in the return call
447+
else:
448+
pad_width = ((int((r - 0.5) / 2.), int((r + 0.5) / 2.)),
449+
(int((c - 0.5) / 2.), int((c + 0.5) / 2.)),
450+
(0, 0))
451+
window_shape = (r, c, d)
452+
# Pad the image
453+
img_pad = skimage.util.pad(img, pad_width=pad_width, mode='symmetric')
454+
455+
# Extract patches
456+
patches = skimage.util.view_as_windows(img_pad, window_shape=window_shape)
457+
458+
return patches.reshape((h * w, r * c * d))
459+
460+
394461
def import_modules(names, src, dst):
395462
"""Import modules in package."""
396463
for name in names:

0 commit comments

Comments
 (0)