Skip to content

Commit

Permalink
features.image_patches --> utils.extract_patches
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeff committed Aug 21, 2017
1 parent d54a8b2 commit 4af195d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 69 deletions.
67 changes: 0 additions & 67 deletions pygsp/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from .graphs import Graph
from .filters import Filter
from .utils import filterbank_handler
from skimage.util import view_as_windows, pad


def compute_avg_adj_deg(G):
Expand Down Expand Up @@ -103,69 +102,3 @@ def atom(x):

G.spectr = spectr
return spectr


def patch_features(img, patch_shape=(3, 3)):
r"""
Compute a patch feature vector for every pixel of an image.
Parameters
----------
img : array
Input image.
patch_shape : tuple, optional
Dimensions of the patch window. Syntax: (height, width), or (height,),
in which case width = height.
Returns
-------
array
Feature matrix.
Notes
-----
The feature vector of a pixel `i` will consist of the stacking of the
intensity values of all pixels in the patch centered at `i`, for all color
channels. So, if the input image has `d` color channels, the dimension of
the feature vector of each pixel is (patch_shape[0] * patch_shape[1] * d).
Examples
--------
>>> from pygsp import features
>>> from skimage import data, img_as_float
>>> img = img_as_float(data.camera()[::2, ::2])
>>> X = features.patch_features(img)
"""

try:
h, w, d = img.shape
except ValueError:
try:
h, w = img.shape
d = 0
except ValueError:
print("Image should be at least a 2-d array.")

try:
r, c = patch_shape
except ValueError:
r = patch_shape[0]
c = r
if d == 0:
pad_width = ((int((r - 0.5) / 2.), int((r + 0.5) / 2.)),
(int((c - 0.5) / 2.), int((c + 0.5) / 2.)))
window_shape = (r, c)
d = 1 # For the reshape in the return call
else:
pad_width = ((int((r - 0.5) / 2.), int((r + 0.5) / 2.)),
(int((c - 0.5) / 2.), int((c + 0.5) / 2.)),
(0, 0))
window_shape = (r, c, d)
# Pad the image
img_pad = pad(img, pad_width=pad_width, mode='symmetric')

# Extract patches
patches = view_as_windows(img_pad, window_shape=window_shape)

return patches.reshape((h * w, r * c * d))
4 changes: 2 additions & 2 deletions pygsp/graphs/nngraphs/imgpatches.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

from pygsp.graphs import NNGraph
from pygsp.features import patch_features
from pygsp import utils


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

X = patch_features(img, patch_shape=patch_shape)
X = utils.extract_patches(img, patch_shape=patch_shape)

super(ImgPatches, self).__init__(X,
use_flann=use_flann,
Expand Down
67 changes: 67 additions & 0 deletions pygsp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from scipy import kron, ones
from scipy import sparse
from scipy.io import loadmat as scipy_loadmat
import skimage


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


def extract_patches(img, patch_shape=(3, 3)):
r"""
Extract a patch feature vector for every pixel of an image.
Parameters
----------
img : array
Input image.
patch_shape : tuple, optional
Dimensions of the patch window. Syntax: (height, width), or (height,),
in which case width = height.
Returns
-------
array
Feature matrix.
Notes
-----
The feature vector of a pixel `i` will consist of the stacking of the
intensity values of all pixels in the patch centered at `i`, for all color
channels. So, if the input image has `d` color channels, the dimension of
the feature vector of each pixel is (patch_shape[0] * patch_shape[1] * d).
Examples
--------
>>> from pygsp import utils
>>> import skimage
>>> img = skimage.img_as_float(skimage.data.camera()[::2, ::2])
>>> X = utils.extract_patches(img)
"""

try:
h, w, d = img.shape
except ValueError:
try:
h, w = img.shape
d = 0
except ValueError:
print("Image should be at least a 2-d array.")

try:
r, c = patch_shape
except ValueError:
r = patch_shape[0]
c = r
if d == 0:
pad_width = ((int((r - 0.5) / 2.), int((r + 0.5) / 2.)),
(int((c - 0.5) / 2.), int((c + 0.5) / 2.)))
window_shape = (r, c)
d = 1 # For the reshape in the return call
else:
pad_width = ((int((r - 0.5) / 2.), int((r + 0.5) / 2.)),
(int((c - 0.5) / 2.), int((c + 0.5) / 2.)),
(0, 0))
window_shape = (r, c, d)
# Pad the image
img_pad = skimage.util.pad(img, pad_width=pad_width, mode='symmetric')

# Extract patches
patches = skimage.util.view_as_windows(img_pad, window_shape=window_shape)

return patches.reshape((h * w, r * c * d))


def import_modules(names, src, dst):
"""Import modules in package."""
for name in names:
Expand Down

0 comments on commit 4af195d

Please sign in to comment.