``````

In [ ]:

import menpo.io as mio

image1 = mio.import_builtin_asset.takeo_ppm()
image1.crop_to_landmarks_proportion_inplace(0.3)
image1 = image1.as_greyscale()

image2.crop_to_landmarks_proportion_inplace(0.3)

%matplotlib inline
image2.view_landmarks()

``````
``````

In [ ]:

import numpy as np

def extract_patches_joan(image, centres, patch_shape, normalize_patches=False):
# extract patches
patches = image.extract_patches(centres, patch_size=patch_shape,
as_single_array=False)

if normalize_patches:
for p in range(len(patches)):
patches[p].normalize_norm_inplace()
return patches

def vectorize_patches_joan(patches):
# find lengths
patch_len = np.prod(patches[0].shape) * patches[0].n_channels
n_points = len(patches)

# initialize output matrix
patches_vectors = np.empty(patch_len * n_points)

# extract each vector
for p in range(n_points):
# find indices in target vector
i_from = p * patch_len
i_to = (p + 1) * patch_len

# store vector
patches_vectors[i_from:i_to] = patches[p].as_vector()

return patches_vectors

def vectorize1(patches_list):
r"""
Vectorization function A: It vectorizes a given list of patches.
Returns an ndarray of size (m*n) x 1
"""
return vectorize_patches_joan(patches_list)

def warp1(image, centres, patch_shape):
r"""
Warp function F: It extracts the patches around each shape point
returns a list with the patches
"""
return extract_patches_joan(image, centres, patch_shape,
normalize_patches=False)

r"""
Returns the gradients of the patches
n_dims x n_channels x n_points x (w x h)
"""
n_dims = patches[0].n_dims
n_channels = patches[0].n_channels
wh = np.prod(patches[0].shape)
n_points = len(patches)
gradients = np.empty((n_dims, n_channels, wh, n_points))
for k, i in enumerate(patches):
gradients[..., k] = np.reshape(g, (-1, n_channels, n_dims)).T

``````
``````

In [ ]:

from cvpr15.utils import build_patches_image, vectorize_patches_image
from menpo.image import Image

def warp2(image, centres, patch_shape):
r"""
Warp function F: It extracts the patches around each shape point
returns an image object of size:
n_points x patch_shape[0] x patch_shape[1] x n_channels
"""
return build_patches_image(image, centres, patch_shape)

def vectorize2(patches_image):
r"""
Vectorization function A: It vectorizes a given patches image.
Returns an ndarray of size (m*n) x 1
"""
return vectorize_patches_image(patches_image)

r"""
Returns the gradients of the patches
n_dims x n_channels x n_points x (w x h)
"""
n_points = patches.pixels.shape[0]
h = patches.pixels.shape[1]
w = patches.pixels.shape[2]
n_channels = patches.pixels.shape[3]

# initial patches: n_parts x height x width x n_channels
pixels = patches.pixels
# move parts axis to end: height x width x n_channels x n_parts
pixels = np.rollaxis(pixels, 0, pixels.ndim)
# merge channels and parts axes: height x width x (n_channels * n_parts)
pixels = np.reshape(pixels, (h, w, -1), order='F')
# compute and vectorize gradient: (height * width) x (n_channels * n_parts * n_dims)
# reshape gradient: (height * width) x n_parts x n_channels x n_dims
# then trnspose it: n_dims x n_channels x n_parts x (height * width)
return np.reshape(g, (-1, n_points, n_channels, 2)).T

``````
``````

In [ ]:

image = image2
patch_shape = (17, 17)
n_channels = image.n_channels

``````
``````

In [ ]:

i1 = warp1(image, image.landmarks['PTS']['all'], patch_shape)
vec_i1 = vectorize1(i1)
#e1 = vec_i1 - self.appearance_model[0]

``````
``````

In [ ]:

i2 = warp2(image, image.landmarks['PTS']['all'], patch_shape)
vec_i2 = vectorize2(i2)
#e1 = vec_i1 - self.appearance_model[0]

``````
``````

In [ ]:

import numpy as np

print vec_i1.shape, vec_i2.shape, np.array_equal(vec_i1, vec_i2)
print nabla_i1.shape, nabla_i2.shape, np.array_equal(nabla_i1, nabla_i2)

``````
``````

In [ ]:

%matplotlib inline

k = 37
d = 1
c = 2

m1 = nabla_i1[d, c, k, :].reshape(patch_shape[0], patch_shape[1])
m2 = nabla_i2[d, c, k, :].reshape(patch_shape[0], patch_shape[1])

Image(m1).view()
Image(m2).view_new()

``````
``````

In [ ]:

%matplotlib inline

k = 37

l = np.prod(patch_shape) * n_channels

from_i = k * l
to_i = (k+1) * l

Image(vec_i1[from_i:to_i].reshape(patch_shape[0], patch_shape[1], n_channels)).view()
Image(vec_i2[from_i:to_i].reshape(patch_shape[0], patch_shape[1], n_channels)).view_new()

``````
``````

In [ ]:

``````