Facemorph
Class PCI

java.lang.Object
  extended by Facemorph.PCI

public class PCI
extends java.lang.Object

An object to contain the PCA of a set of images


Field Summary
 float[] maxWeights
          List of the maximum or mean weights that keep the components within a specified range
 
Constructor Summary
PCI()
          Creates a new instance of PCI
 
Method Summary
 float[] analyse(FloatImage[] fimg)
          Analyses the given image to gives the weights of the principal components
 float[] analyse(FloatImage[] fimg, FloatImage mask)
          Analyses the given image to gives the weights of the principal components
static float[] analyseFile(FloatImage[] fimg, FloatImage mask, int pcaCount)
          Analyses the given image to gives the weights of the principal components
 double[] analyseLS(FloatImage[] fimg, FloatImage mask)
          Analyses the given image to gives the weights of the principal components
 float[] analyseMaxWeights(FloatImage[] fimg)
          Analyses the given image to gives the weights of the principal components
 void build(java.util.ArrayList<java.awt.image.BufferedImage> images, double varianceToExplain)
          Method to create a PCI from a set of delineated images.
 void build(java.util.ArrayList<java.awt.image.BufferedImage> images, double varianceToExplain, FloatImage mask)
          Method to create a PCI from a set of delineated images.
 void build(Template avTem, FloatImage[] avImg, java.awt.Image[] images, Template[] templates, FloatImage mask, double varianceToExplain)
          Method to create a PCI from a set of delineated images.
 void build(Template avTem, java.awt.Image avImg, java.awt.Image[] images, Template[] templates, Mask mask, double varianceToExplain)
          Builds a PCI from the data given
 void buildFloatImage(java.util.ArrayList<FloatImage[]> images, FloatImage mask, double varianceToExplain)
          Method to create a PCI from a set of delineated images.
static double[] buildWarpedFiles(int number, double varianceToExplain, FloatImage[] average, FloatImage mask)
          Method to create a PCI from a set of pre-warped images, which are read from files with names of the form "warped"+i+"_"+k+".fimg" where k is 0, 1 or 2 for r, g and b.
 void calculateMaxWeights(int min, int max)
          Calculates the max weights required to keep the component within the (min, max) range
 void calculateMeanWeights(int min, int max)
          Calculates the mean weights required to keep the component within the (min, max) range
 BigMat combine(PCI[] pci, Template targetShape, int targetWidth, int targetHeight, Mask mask)
          Builds a PCI by combining a set of PCIs
 FloatImage[] getAverage()
          Retrieves the average image as an array of FloatImage
 FloatImage[] getComponent(int i)
          Retrieve a specific component
 float[] getComponentMaxShiftScale(int comp, float min, float max)
          Find scale and shift of component comp of this PCI such that alpha*smallest <= min and alpha*biggest<=max
 int getCount()
          Return the number of components in this model
 FloatImage[][] getIntersection(PCI pci, Mask mask)
          Gets the intersection or closest point between two PCIs
 FloatImage[][] getIntersectionIterative(PCI pci, Mask mask)
          Gets the intersection or closest point between two PCIs
 float[][] getMaxShiftScale(float min, float max)
          Get the scale and shift for each component so that the values remain the min-max range
 float[] getMaxWeights(int min, int max)
          Get the mean weights required to keep the component within the (min, max) range
 BigMat getReduced(PCI pci, float[] filter, int m, Mask mask)
          Shrinks by a factor of 2 using filter to downsample
 Template getTemplate()
          Retrieve the average template that defines the shape of this PCI
 double getTotalVariance()
          Return the total variance
 double getVariance(int i)
          Get the variance of component i
static void main(java.lang.String[] args)
          Main method used for testing
 void read(java.lang.String file, int maxComp)
          Reads PCI data from a directory
 void readJpgs(java.lang.String file, int maxComp, java.applet.Applet applet)
          Reads PCI data as a list of image filesspecified in a text file
 FloatImage[] reconstruct(float[] params)
          reconstructs an image given the component weightings
 FloatImage[] reconstruct(float[] params, FloatImage[] avrg)
          reconstructs an image given the component weightings, using avrg instead of the built in average
 java.awt.image.BufferedImage reconstructBufferedImage(float[] params)
          Build an image from PCA components using the weights given
static FloatImage[] reconstructFile(float[] params, FloatImage[] avrg, int pcaCount)
          reconstructs an image given the component weightings, using avrg instead of the built in average
 java.awt.Image reconstructImage(float[] params)
          Build an image from PCA components using the weights given
 FloatImage[] reconstructMaxWeights(float[] params)
          Build an image from PCA components using the weights given after rescaling them by the maxWeights array
 FloatImage[] reconstructMaxWeights(float[] params, FloatImage[] avrg)
          Build an image from PCA components using the weights given after rescaling them by the maxWeights array.
 void replaceMean(java.awt.Image newMean, Template temp, boolean project)
          Replace the mean image with that specified
 void setAverage(FloatImage red, FloatImage green, FloatImage blue)
          Sets the values of the mean image
 void setComponent(int i, FloatImage[] fimg)
          Set one component
 void setComponents(FloatImage[][] comps, double[] variances)
          Sets the components and their variances
 void setMaxCount(int c)
          Sets the maximum number of components to use
 BigMat warp(Template targetShape, int targetWidth, int targetHeight, Mask mask)
          Warps this PCI into targetShape
 void write(java.lang.String file)
          Writes PCI data to a directory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

maxWeights

public float[] maxWeights
List of the maximum or mean weights that keep the components within a specified range

Constructor Detail

PCI

public PCI()
Creates a new instance of PCI

Method Detail

getReduced

public BigMat getReduced(PCI pci,
                         float[] filter,
                         int m,
                         Mask mask)
Shrinks by a factor of 2 using filter to downsample

Parameters:
pci - The PCI to copy and shrink
filter - The downsampling filter
m - The midpoint of the downsampling filter
mask - The mask to use
Returns:
Returns the matrix of PCA weightings

warp

public BigMat warp(Template targetShape,
                   int targetWidth,
                   int targetHeight,
                   Mask mask)
Warps this PCI into targetShape

Parameters:
targetShape - The desired new shape of the PCI
targetWidth - the desired new width of the PCI
targetHeight - the desired new height of the PCI
mask - The mask to use
Returns:
returns the matrix of weights of the principal component vectors

build

public void build(Template avTem,
                  java.awt.Image avImg,
                  java.awt.Image[] images,
                  Template[] templates,
                  Mask mask,
                  double varianceToExplain)
           throws Facemorph.PCI.AverageException
Builds a PCI from the data given

Parameters:
avTem - The average Template to use
avImg - The average imaage to use
images - The set of images to use
templates - The set of Templates to use
mask - The mask to use
varianceToExplain - The fraction of variance to explain in this model
Throws:
Facemorph.PCI.AverageException

build

public void build(Template avTem,
                  FloatImage[] avImg,
                  java.awt.Image[] images,
                  Template[] templates,
                  FloatImage mask,
                  double varianceToExplain)
           throws Facemorph.PCI.AverageException
Method to create a PCI from a set of delineated images.

Parameters:
avTem - The average Template
avImg - The average image as an array of 3 FloatImage
images - The array of images to be averaged.
templates - The array of templates outlining the features of the corresponding templates.
mask - The FLoatImage to use as a mask, values should be between 0 and 1
varianceToExplain - This controls the number of components output.
Throws:
AverageException - Thrown if a problem arises in building this PCI
Facemorph.PCI.AverageException

build

public void build(java.util.ArrayList<java.awt.image.BufferedImage> images,
                  double varianceToExplain)
           throws Facemorph.PCI.AverageException
Method to create a PCI from a set of delineated images.

Parameters:
images - The array of images to be averaged.
varianceToExplain - This controls the number of components output.
Throws:
AverageException - Thrown if a problem arises in building this PCI
Facemorph.PCI.AverageException

build

public void build(java.util.ArrayList<java.awt.image.BufferedImage> images,
                  double varianceToExplain,
                  FloatImage mask)
           throws Facemorph.PCI.AverageException
Method to create a PCI from a set of delineated images.

Parameters:
images - The array of images to be averaged.
varianceToExplain - This controls the number of components output.
mask - mask image to use when building the PCI
Throws:
AverageException - Thrown if a problem arises in building this PCI
Facemorph.PCI.AverageException

buildWarpedFiles

public static double[] buildWarpedFiles(int number,
                                        double varianceToExplain,
                                        FloatImage[] average,
                                        FloatImage mask)
                                 throws Facemorph.PCI.AverageException
Method to create a PCI from a set of pre-warped images, which are read from files with names of the form "warped"+i+"_"+k+".fimg" where k is 0, 1 or 2 for r, g and b.

Parameters:
number - the number of images to be used
varianceToExplain - This controls the number of components output.
average - average image, seems to be only used for its size
mask - a mask to use or null if no masking needed
Returns:
returns the array of variances of each component
Throws:
AverageException - Thrown if a problem arises in building this PCI
Facemorph.PCI.AverageException

buildFloatImage

public void buildFloatImage(java.util.ArrayList<FloatImage[]> images,
                            FloatImage mask,
                            double varianceToExplain)
                     throws Facemorph.PCI.AverageException
Method to create a PCI from a set of delineated images.

Parameters:
images - The array of images to be averaged.
mask - selection function for points to use
varianceToExplain - This controls the number of components output.
Throws:
AverageException - Thrown if a problem arises in building this PCI
Facemorph.PCI.AverageException

combine

public BigMat combine(PCI[] pci,
                      Template targetShape,
                      int targetWidth,
                      int targetHeight,
                      Mask mask)
Builds a PCI by combining a set of PCIs

Parameters:
pci - An array of PCIs to combine
targetShape - the desired output shape
targetWidth - the desired output width
targetHeight - the desired output height
mask - the mask to use
Returns:
returns the matrix of component weights

getIntersection

public FloatImage[][] getIntersection(PCI pci,
                                      Mask mask)
                               throws BigMatException
Gets the intersection or closest point between two PCIs

Parameters:
pci - the PCI to find the intersection with
mask - the mask to use
Returns:
returns the intesection point in this and the intersection point in pci
Throws:
BigMatException

getIntersectionIterative

public FloatImage[][] getIntersectionIterative(PCI pci,
                                               Mask mask)
Gets the intersection or closest point between two PCIs

Parameters:
pci - the PCI to find the intersection with
mask - the mask to use
Returns:
returns the intesection point in this and the intersection point in pci

setAverage

public void setAverage(FloatImage red,
                       FloatImage green,
                       FloatImage blue)
Sets the values of the mean image

Parameters:
red - The red component of the average
green - The green component of the average
blue - The blue component of the average

replaceMean

public void replaceMean(java.awt.Image newMean,
                        Template temp,
                        boolean project)
Replace the mean image with that specified

Parameters:
newMean - the new mean to use
temp - the shape of the new mean
project - boolean indicating if the mean should be projected back into the new PCI and used as the mean

readJpgs

public void readJpgs(java.lang.String file,
                     int maxComp,
                     java.applet.Applet applet)
Reads PCI data as a list of image filesspecified in a text file

Parameters:
file - The text file containing the names of the image component files and their variance
maxComp - The maximum number of components to read
applet - The applet that is reading the PCI from Jpgs

read

public void read(java.lang.String file,
                 int maxComp)
          throws java.io.IOException
Reads PCI data from a directory

Parameters:
file - The text file containing the names of the image component files and their variance
maxComp - The maximum number of components to read
Throws:
java.io.IOException - if the file can't be read

write

public void write(java.lang.String file)
           throws java.io.IOException
Writes PCI data to a directory

Parameters:
file - The text file containing the names of the image component files and their variance
Throws:
java.io.IOException - throws IOException if problem writing file

getComponent

public FloatImage[] getComponent(int i)
Retrieve a specific component

Parameters:
i - The index of the component
Returns:
the red, green and blue components in an array

setComponents

public void setComponents(FloatImage[][] comps,
                          double[] variances)
Sets the components and their variances

Parameters:
comps - The new components to use
variances - the new variances to use

setComponent

public void setComponent(int i,
                         FloatImage[] fimg)
Set one component

Parameters:
i - The index of the component
fimg - the red, green and blue components in an array of 3 FloatImage

setMaxCount

public void setMaxCount(int c)
Sets the maximum number of components to use

Parameters:
c - the new maximum count

reconstruct

public FloatImage[] reconstruct(float[] params)
reconstructs an image given the component weightings

Parameters:
params - the component weightings
Returns:
the reconstructed image as an array of FloatImages in the order R,G,B

reconstructImage

public java.awt.Image reconstructImage(float[] params)
Build an image from PCA components using the weights given

Parameters:
params - The weights of each component
Returns:
Return the reconstructed image

reconstructBufferedImage

public java.awt.image.BufferedImage reconstructBufferedImage(float[] params)
Build an image from PCA components using the weights given

Parameters:
params - The weights of each component
Returns:
Return the reconstructed image

reconstruct

public FloatImage[] reconstruct(float[] params,
                                FloatImage[] avrg)
reconstructs an image given the component weightings, using avrg instead of the built in average

Parameters:
params - the component weightings
avrg - the average image to use as an {R,G,B} array of FloatImage
Returns:
the reconstructed image as an array of FloatImages in the order R,G,B

reconstructFile

public static FloatImage[] reconstructFile(float[] params,
                                           FloatImage[] avrg,
                                           int pcaCount)
reconstructs an image given the component weightings, using avrg instead of the built in average

Parameters:
params - the component weightings
avrg - the average image to use as an {R,G,B} array of FloatImage
pcaCount - the number of components to use in the reconstruction
Returns:
the reconstructed image as an array of FloatImages in the order R,G,B

reconstructMaxWeights

public FloatImage[] reconstructMaxWeights(float[] params)
Build an image from PCA components using the weights given after rescaling them by the maxWeights array

Parameters:
params - The scaled PCA weights
Returns:
The reconstructed image

reconstructMaxWeights

public FloatImage[] reconstructMaxWeights(float[] params,
                                          FloatImage[] avrg)
Build an image from PCA components using the weights given after rescaling them by the maxWeights array. Uses the average given instead of the built in average.

Parameters:
params - The parameters weights
avrg - The average image to use as the base
Returns:
Return the reconstructed image

getVariance

public double getVariance(int i)
Get the variance of component i

Parameters:
i - The component whose variance you want to know
Returns:
The variance of the component

getTotalVariance

public double getTotalVariance()
Return the total variance

Returns:
returns the total variance

analyse

public float[] analyse(FloatImage[] fimg)
Analyses the given image to gives the weights of the principal components

Parameters:
fimg - The FloatImage to analyse
Returns:
the component weights as a float array

analyse

public float[] analyse(FloatImage[] fimg,
                       FloatImage mask)
Analyses the given image to gives the weights of the principal components

Parameters:
fimg - The FloatImage to analyse
mask - the mask to use
Returns:
the component weights as a float array

analyseFile

public static float[] analyseFile(FloatImage[] fimg,
                                  FloatImage mask,
                                  int pcaCount)
Analyses the given image to gives the weights of the principal components

Parameters:
fimg - The FloatImage to analyse
mask - the mask to use
pcaCount - the number of pca components to use
Returns:
the component weights as a float array

analyseLS

public double[] analyseLS(FloatImage[] fimg,
                          FloatImage mask)
Analyses the given image to gives the weights of the principal components

Parameters:
fimg - The FloatImage to analyse
mask - the mask to use
Returns:
the component weights as a float array

analyseMaxWeights

public float[] analyseMaxWeights(FloatImage[] fimg)
Analyses the given image to gives the weights of the principal components

Parameters:
fimg - The FloatImage to analyse
Returns:
the component weights as a float array

getCount

public int getCount()
Return the number of components in this model

Returns:
The number of components

getAverage

public FloatImage[] getAverage()
Retrieves the average image as an array of FloatImage

Returns:
The average image as an array of FloatImage in RGB order

getTemplate

public Template getTemplate()
Retrieve the average template that defines the shape of this PCI

Returns:
The average template

calculateMaxWeights

public void calculateMaxWeights(int min,
                                int max)
Calculates the max weights required to keep the component within the (min, max) range

Parameters:
min - The minimum value you want it to attain
max - The maximum value you want it to attain

calculateMeanWeights

public void calculateMeanWeights(int min,
                                 int max)
Calculates the mean weights required to keep the component within the (min, max) range

Parameters:
min - The minimum value you want it to attain
max - The maximum value you want it to attain

getMaxWeights

public float[] getMaxWeights(int min,
                             int max)
Get the mean weights required to keep the component within the (min, max) range

Parameters:
min - The minimum value you want it to attain
max - The maximum value you want it to attain
Returns:
The weights

getComponentMaxShiftScale

public float[] getComponentMaxShiftScale(int comp,
                                         float min,
                                         float max)
Find scale and shift of component comp of this PCI such that alpha*smallest <= min and alpha*biggest<=max

Parameters:
comp - the component to test
min - the minimum value
max - the maximum value
Returns:
returns the scale and shift needed to get the component in the min, max range

getMaxShiftScale

public float[][] getMaxShiftScale(float min,
                                  float max)
Get the scale and shift for each component so that the values remain the min-max range

Parameters:
min - the minimum value needed
max - the maximum value needed
Returns:
returns an array of {scale, shift} pairs for each component

main

public static void main(java.lang.String[] args)
Main method used for testing

Parameters:
args - the arguments to use