Ejemplo n.º 1
0
    def __init__(self, dir_name, verbose=False):
        """Load a collection of images from a directory"""

        self.image_coll = ImageCollection.from_directory(dir_name,
                                                         verbose=verbose)
        self.num_images = self.image_coll.num_images
        iflat = self.image_coll.flat()
        data_mean = iflat.mean(axis=1)
        img_flat_norm = iflat - data_mean[:, N.newaxis]

        # Compute singular values and U matrix
        umat, sigma, vtmat = N.linalg.svd(img_flat_norm, 0)

        # Array of 'eigenimages' in normal (not flattened) format
        im_shape = self.image_coll.images[0].shape
        eig_img = N.empty((self.num_images, im_shape[0], im_shape[1]),
                          umat.dtype)
        for i in range(self.num_images):
            eig_img[i] = N.reshape(umat[:, i], im_shape)

        # Store in object all these
        self.sigma = sigma
        self.umat = umat
        self.vtmat = vtmat
        self.im_shape = im_shape
        self.eig_img = ImageCollection(eig_img)
        self.img_flat_norm = img_flat_norm
        self.data_mean = data_mean

        # Truncation index, by default we don't truncate
        self.truncate_idx = self.num_images - 1
Ejemplo n.º 2
0
    def __init__(self,dir_name,verbose=False):
        """Load a collection of images from a directory"""

        self.image_coll = ImageCollection.from_directory(dir_name,
                                                         verbose=verbose)
        self.num_images = self.image_coll.num_images
        iflat = self.image_coll.flat()
        data_mean = iflat.mean(axis=1)
        img_flat_norm = iflat - data_mean[:,N.newaxis]

        # Compute singular values and U matrix
        umat,sigma,vtmat = N.linalg.svd(img_flat_norm,0)

        # Array of 'eigenimages' in normal (not flattened) format
        im_shape = self.image_coll.images[0].shape
        eig_img = N.empty((self.num_images,im_shape[0],im_shape[1]),umat.dtype)
        for i in range(self.num_images):
            eig_img[i] = N.reshape(umat[:,i],im_shape)

        # Store in object all these
        self.sigma = sigma
        self.umat = umat
        self.vtmat = vtmat
        self.im_shape = im_shape
        self.eig_img = ImageCollection(eig_img)
        self.img_flat_norm = img_flat_norm
        self.data_mean = data_mean

        # Truncation index, by default we don't truncate
        self.truncate_idx = self.num_images-1
Ejemplo n.º 3
0
                # Add the weighted eigenfaces to form the face
                eig_composition += test_coeffs[i] * eig_img[i]

            # Add back the data mean (subtracted before composition)
            eig_composition += data_mean.reshape(im_shape)

            P.imshow(eig_composition, P.cm.gray)
        else:
            P.imshow(face, P.cm.gray)
            P.title('Original image')


# 'main' script

# Load the training images from directory 'data_train/'
imtrain = ImageCollection.from_directory('data_train/')

# Compute the data mean and normalise
data_mean = imtrain.flat().mean(axis=1)
img_flat_norm = imtrain.flat() - data_mean[:, N.newaxis]

# Compute singular values and U matrix
umat, sigma, vtmat = N.linalg.svd(img_flat_norm, 0)

# Create an array of 'eigenfaces' in normal (not flattened) format
im_shape = imtrain.im_shape

eig_img = N.empty((imtrain.num_images, im_shape[0], im_shape[1]), umat.dtype)
for i in range(imtrain.num_images):
    eig_img[i] = N.reshape(umat[:, i], im_shape)
Ejemplo n.º 4
0
                # Add the weighted eigenfaces to form the face
                eig_composition += test_coeffs[i]*eig_img[i]

            # Add back the data mean (subtracted before composition)
            eig_composition += data_mean.reshape(imshape)
            
            P.imshow(eig_composition, P.cm.gray)
        else:
            P.imshow(face, P.cm.gray)
            P.title('Original image')
            
# 'main' script
# 'main' script

# Load the training images from directory 'data_train/'
imtrain = ImageCollection.from_directory('data_train/')

# Compute the data mean and normalise
data_mean = imtrain.flat().mean(axis=1)
img_flat_norm = imtrain.flat() - data_mean[:,N.newaxis]

# Compute singular values and U matrix
umat,sigma,vtmat = N.linalg.svd(img_flat_norm,0)

# Create an array of 'eigenfaces' in normal (not flattened) format
im_shape = imtrain.im_shape

eig_img = N.empty((imtrain.num_images,imshape[0],imshape[1]),umat.dtype)
for i in range(imtrain.num_images):
    eig_img[i] = N.reshape(umat[:,i],imshape)
Ejemplo n.º 5
0
#!/usr/bin/env python
"""Simple testing script for imatch.py"""

import pylab as P

import imatch, imatch2

from imtools import ImageCollection

classifier = imatch2.FaceClassifier('data_train/')
classifier.truncate_idx = 12

imtest = ImageCollection.from_directory('data_test/')
i0 = imtest.images[0]
i1 = imtest.images[1]

classifier.verify(classifier.image_coll.images[0], 0)
classifier.verify(classifier.image_coll.images[0], 1)

classifier.identify(imtest.images[0])
classifier.identify(classifier.image_coll.images[1])
classifier.verify(imtest.images[0], 13)
classifier.verify(imtest.images[0], 37)

classifier.plot_eigenfaces()
classifier.plot_sigma()

classifier.decompose(i0)

P.show()