In [ ]:
import menpo.io as mio

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

image2 = mio.import_builtin_asset.breakingbad_jpg()
image2.crop_to_landmarks_proportion_inplace(0.3)

%matplotlib inline
image2.view_landmarks()

In [ ]:
import numpy as np

def extract_patch_vectors(image, group, label, patch_size,
                          normalize_patches=False):
    r"""
    returns a numpy.array of size (16*16*36) x 68
    """
    # extract patches
    patches = image.extract_patches_around_landmarks(
        group=group, label=label, patch_size=patch_size,
        as_single_array=not normalize_patches)

    # vectorize patches
    if normalize_patches:
        # initialize output matrix
        patches_vectors = np.empty(
            (np.prod(patches[0].shape) * patches[0].n_channels, len(patches)))

        # extract each vector
        for p in range(len(patches)):
            # normalize part
            patches[p].normalize_norm_inplace()

            # extract vector
            patches_vectors[:, p] = patches[p].as_vector()
    else:
        # initialize output matrix
        patches_vectors = np.empty((np.prod(patches.shape[1:]),
                                    patches.shape[0]))

        # extract each vector
        for p in range(patches.shape[0]):
            patches_vectors[:, p] = patches[p, ...].ravel()

    # return vectorized parts
    return patches_vectors

def vectorize_joan(patches):
    n_points = patches.shape[1]
    patch_len = patches.shape[0]
    app_len = patch_len * n_points
    app_mean = np.empty(app_len)
    for e in range(n_points):
        i_from = e * patch_len
        i_to = (e + 1) * patch_len
        app_mean[i_from:i_to] = patches[..., e]
    return app_mean

In [ ]:
from cvpr15.utils import build_patches_image, vectorize_patches_image

In [ ]:
from menpo.image import Image

%matplotlib inline

k = 44
n_channels = 1
l = 17 * 17 * n_channels

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

In [ ]:
i1 = extract_patch_vectors(image, 'PTS', 'all', (17, 17), normalize_patches=False)
vec_i1 = vectorize_joan(i1)

print i1.shape
print vec_i1.shape

Image(vec_i1[from_i:to_i].reshape(17, 17, n_channels)).view()

In [ ]:
i2 = build_patches_image(image, None, (17, 17))
vec_i2 = vectorize_patches_image(i2)

print i2.shape
print vec_i2.shape

Image(vec_i2[from_i:to_i].reshape(17, 17, n_channels)).view()

In [ ]:
from menpo.image import Image

%matplotlib inline

k = 37
n_channels = 3
l = 17 * 17 * n_channels

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

In [ ]:
i1 = extract_patch_vectors(image2, 'PTS', 'all', (17, 17), normalize_patches=False)
vec_i1 = vectorize_joan(i1)

print i1.shape
print vec_i1.shape

Image(vec_i1[from_i:to_i].reshape(17, 17, n_channels)).view()

In [ ]:
#i2 = build_patches_image(image2, None, (17, 17))
i2 = build_patches_image(image2, image2.landmarks['PTS']['all'], (17, 17))
vec_i2 = vectorize_patches_image(i2)

print i2.shape
print vec_i2.shape

Image(vec_i2[from_i:to_i].reshape(17, 17, n_channels)).view()

In [ ]:
import timeit

In [ ]:
%%timeit
i1 = extract_patch_vectors(image, 'PTS', 'all', (17, 17), normalize_patches=False)
vec_i1 = vectorize_joan(i1)

In [ ]:
%%timeit
i2 = build_patches_image(image, None, (17, 17))
vec_i2 = vectorize_patches_image(i2)

In [ ]: