Пример #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
Пример #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
Пример #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)
Пример #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)