Facemorph.oesf
Class OESF

java.lang.Object
  extended by Facemorph.oesf.OESF

public class OESF
extends java.lang.Object

Implementation of Optimal Synthetic Exact Filters


Constructor Summary
OESF()
           
 
Method Summary
 FloatImage bootstrapBuildFilter(java.lang.String file, HaarReader haar, boolean orientation, int[] plist)
          Build a set of OESF filters from a set of images and one labeled example
 FloatImage[] buildFilter(java.lang.String file, HaarReader haar, boolean orientation, int[] plist)
          Build a set of OESF filters
 FloatImage buildFilter(java.lang.String file, HaarReader haar, int[] plist)
          Build an OESF filter
 FloatImage buildFilterBoosted(java.lang.String file, HaarReader haar, int point)
          Build a set of OESF filters
 void buildFilterCascade(java.lang.String file, HaarReader haar, int point, java.util.ArrayList<FloatImage> filters, java.util.ArrayList<java.lang.Float> thresholds, java.util.ArrayList<Complex[]> filterFTs, java.util.ArrayList<double[]> priorsParams, java.util.ArrayList<FloatImage> priors)
          Build a set of OESF filters
 java.util.ArrayList<FloatImage> buildFilterMultiply(java.lang.String file, HaarReader haar, int point)
          Build a set of OESF filters
 FloatImage buildFilterWeighted(java.lang.String file, HaarReader haar, int point)
          Build a set of OESF filters
 java.util.ArrayList<java.lang.Float> calculateErrors(java.util.ArrayList<Complex[]> images, java.util.ArrayList<java.awt.geom.Point2D.Float> points, int[] dim, java.util.ArrayList<java.lang.Float> maxResp)
           
static java.util.ArrayList<java.lang.Float> calculateErrors(Complex[] filterFT, FloatImage priorFimg, java.util.ArrayList<Complex[]> images, java.util.ArrayList<java.awt.geom.Point2D.Float> points, int[] dim, java.util.ArrayList<java.lang.Float> maxResp)
           
 java.util.ArrayList<java.lang.Float> calculateMultiplyErrors(java.util.ArrayList<FloatImage> filters, java.util.ArrayList<Complex[]> images, java.util.ArrayList<java.awt.geom.Point2D.Float> points, int[] dim, java.util.ArrayList<java.lang.Float> maxResp)
           
static FloatImage centre(FloatImage fimg)
          Shift the origin to centre for easier human veiwing / interpretation
static FloatImage convert(Complex[] a, int w, int h)
          Converts an array of Complex back into a FloatImage, discarding imaginary components
static Complex[] convert(FloatImage fimg)
          Scan converts a FloatImage to an array of Complex
static Complex[] convert(FloatImage[] fimg)
          Converts a pair of FloatImages {real, imaginary} into a Complex array
static FloatImage[] convertComplex(Complex[] a, int w, int h)
          Convert a Complex array into a pair of FloatImages {real, imaginary}
static java.awt.Rectangle detectFace(java.awt.image.BufferedImage bimg, HaarReader haar)
          Detect a face
static java.awt.Rectangle detectFace(java.awt.image.BufferedImage bimg, HaarReader haar, java.awt.Rectangle prevRect)
          Detect a face
static java.awt.geom.Point2D.Float detectFeature(Complex[] img, Complex[] filt, FloatImage prior, int[] dim)
           
static java.awt.geom.Point2D.Float detectFeature(Complex[] img, Complex[] filt, int[] dim)
           
static FloatImage filter(Complex[] data, Complex[] filt, int[] dim)
          Apply a filter to an image
static FloatImage filter(FloatImage fimg, Complex[] filt, int[] dim)
          Apply a filter to an image
static FloatImage filter(FloatImage fimg, FloatImage filt, int[] dim)
          Apply a filter to an image
static FloatImage[] filterComplex(FloatImage fimg, Complex[] filt, int[] dim)
          Filters a real image with a complex filter
static Complex[] getBoostResponse(float x, float y, int[] dim, double s)
          Create an idealised response image for boosting algorithm
static FloatImage getBoostResponseReal(float x, float y, int[] dim, double s)
          Create an idealised response image for boosting algorithm
static Complex[] getMultiResponse(Template tem, int[] plist, int[] dim)
          Get an idealised response image for all listed features in the Template
static Complex[] getResponse(float x, float y, double angle, int[] dim)
          Get the response image with orientation information (trying to detect orientation doesn't seem to work all that well)
static Complex[] getResponse(float x, float y, int[] dim)
          Create an idealised response image
static boolean hasNextQuotedString(java.util.Scanner sc)
          Returns true if the next token is the beginning of a quoted string value.
 FloatImage iterateBoosted(java.util.ArrayList<Complex[]> imagesFT, java.util.ArrayList<Complex[]> responsesFT, java.util.ArrayList<java.awt.geom.Point2D.Float> points, java.util.ArrayList<Complex[]> imagesFTTest, java.util.ArrayList<java.awt.geom.Point2D.Float> pointsTest, int[] dim)
          Build a set of OESF filters
static void main(java.lang.String[] args)
          Main method used for testing
static java.lang.String nextQuotedString(java.util.Scanner sc)
          Scans a quoted string value from the scanner input.
static void preprocess(FloatImage fimg)
          Preprocess the image using a log transform (y=log(1+x)), normalis mean and sd and sine window
 void read(java.lang.String dir)
          Read an OESF filter from file
static void testFilter(java.lang.String file, java.util.ArrayList<FloatImage> filters, FloatImage prior, HaarReader haar, int[] plist)
          Test the filter
static void testFilterComplex(java.lang.String file, java.util.ArrayList<FloatImage[]> filters, FloatImage prior, HaarReader haar)
          Test the complex filter
 void write(java.lang.String dir)
          Writes the filters and prior data to file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OESF

public OESF()
Method Detail

convert

public static Complex[] convert(FloatImage fimg)
Scan converts a FloatImage to an array of Complex

Parameters:
fimg - the FloatImage to convert
Returns:
returns an array of Complex

convert

public static Complex[] convert(FloatImage[] fimg)
Converts a pair of FloatImages {real, imaginary} into a Complex array

Parameters:
fimg - the pair of images to convert
Returns:
returns an array of Complex

convert

public static FloatImage convert(Complex[] a,
                                 int w,
                                 int h)
Converts an array of Complex back into a FloatImage, discarding imaginary components

Parameters:
a - the complex array of data
w - the output width
h - the output height
Returns:
returns the real part as a FloatImage

convertComplex

public static FloatImage[] convertComplex(Complex[] a,
                                          int w,
                                          int h)
Convert a Complex array into a pair of FloatImages {real, imaginary}

Parameters:
a - the Complex array
w - the output width
h - the output height
Returns:
returns 2 FloatImages, one for the real parts and one for the complex

preprocess

public static void preprocess(FloatImage fimg)
Preprocess the image using a log transform (y=log(1+x)), normalis mean and sd and sine window

Parameters:
fimg - the image to preprocess

getResponse

public static Complex[] getResponse(float x,
                                    float y,
                                    int[] dim)
Create an idealised response image

Parameters:
x - the x-coord of the feature
y - the y-coord of the feature
dim - the dimensions of the Complex array, should be {128, 128}
Returns:
returns the complex array

getBoostResponse

public static Complex[] getBoostResponse(float x,
                                         float y,
                                         int[] dim,
                                         double s)
Create an idealised response image for boosting algorithm

Parameters:
x - the x-coord of the feature
y - the y-coord of the feature
dim - the dimensions of the Complex array, should be {128, 128}
Returns:
returns the complex array

getBoostResponseReal

public static FloatImage getBoostResponseReal(float x,
                                              float y,
                                              int[] dim,
                                              double s)
Create an idealised response image for boosting algorithm

Parameters:
x - the x-coord of the feature
y - the y-coord of the feature
dim - the dimensions of the Complex array, should be {128, 128}
Returns:
returns the complex array

getMultiResponse

public static Complex[] getMultiResponse(Template tem,
                                         int[] plist,
                                         int[] dim)
Get an idealised response image for all listed features in the Template

Parameters:
tem - the Template
plist - the list of points from the Template to use
dim - the dimension (should be {128, 128}
Returns:
return the response image for all the features

getResponse

public static Complex[] getResponse(float x,
                                    float y,
                                    double angle,
                                    int[] dim)
Get the response image with orientation information (trying to detect orientation doesn't seem to work all that well)

Parameters:
x - the x-coord
y - the y-coord
angle - the angle of the feature rotation
dim - the dimensionality should be {128, 128}
Returns:
returns the response image

buildFilter

public FloatImage buildFilter(java.lang.String file,
                              HaarReader haar,
                              int[] plist)
                       throws java.io.FileNotFoundException,
                              java.io.IOException
Build an OESF filter

Parameters:
file - list of images in a file
haar - face detector
plist - list of points to use from each template (only the first point is returned)
Returns:
returns the optimal filter
Throws:
java.io.FileNotFoundException
java.io.IOException

buildFilter

public FloatImage[] buildFilter(java.lang.String file,
                                HaarReader haar,
                                boolean orientation,
                                int[] plist)
                         throws java.io.FileNotFoundException,
                                java.io.IOException
Build a set of OESF filters

Parameters:
file - the name of the file containing the list of images and template
haar - the face detector
orientation - idicates that we want to try and learn detectors that give us the orientation
plist - the list of points to use from the templates
Returns:
return the set of filters, one for each point
Throws:
java.io.FileNotFoundException
java.io.IOException

buildFilterCascade

public void buildFilterCascade(java.lang.String file,
                               HaarReader haar,
                               int point,
                               java.util.ArrayList<FloatImage> filters,
                               java.util.ArrayList<java.lang.Float> thresholds,
                               java.util.ArrayList<Complex[]> filterFTs,
                               java.util.ArrayList<double[]> priorsParams,
                               java.util.ArrayList<FloatImage> priors)
                        throws java.io.FileNotFoundException,
                               java.io.IOException
Build a set of OESF filters

Parameters:
file - the name of the file containing the list of images and template
haar - the face detector
orientation - idicates that we want to try and learn detectors that give us the orientation
plist - the list of points to use from the templates
Throws:
java.io.FileNotFoundException
java.io.IOException

buildFilterWeighted

public FloatImage buildFilterWeighted(java.lang.String file,
                                      HaarReader haar,
                                      int point)
                               throws java.io.FileNotFoundException,
                                      java.io.IOException
Build a set of OESF filters

Parameters:
file - the name of the file containing the list of images and template
haar - the face detector
orientation - idicates that we want to try and learn detectors that give us the orientation
plist - the list of points to use from the templates
Returns:
return the set of filters, one for each point
Throws:
java.io.FileNotFoundException
java.io.IOException

buildFilterBoosted

public FloatImage buildFilterBoosted(java.lang.String file,
                                     HaarReader haar,
                                     int point)
                              throws java.io.FileNotFoundException,
                                     java.io.IOException
Build a set of OESF filters

Parameters:
file - the name of the file containing the list of images and template
haar - the face detector
orientation - idicates that we want to try and learn detectors that give us the orientation
plist - the list of points to use from the templates
Returns:
return the set of filters, one for each point
Throws:
java.io.FileNotFoundException
java.io.IOException

iterateBoosted

public FloatImage iterateBoosted(java.util.ArrayList<Complex[]> imagesFT,
                                 java.util.ArrayList<Complex[]> responsesFT,
                                 java.util.ArrayList<java.awt.geom.Point2D.Float> points,
                                 java.util.ArrayList<Complex[]> imagesFTTest,
                                 java.util.ArrayList<java.awt.geom.Point2D.Float> pointsTest,
                                 int[] dim)
Build a set of OESF filters

Parameters:
file - the name of the file containing the list of images and template
haar - the face detector
orientation - idicates that we want to try and learn detectors that give us the orientation
plist - the list of points to use from the templates
Returns:
return the set of filters, one for each point
Throws:
java.io.FileNotFoundException
java.io.IOException

buildFilterMultiply

public java.util.ArrayList<FloatImage> buildFilterMultiply(java.lang.String file,
                                                           HaarReader haar,
                                                           int point)
                                                    throws java.io.FileNotFoundException,
                                                           java.io.IOException
Build a set of OESF filters

Parameters:
file - the name of the file containing the list of images and template
haar - the face detector
orientation - idicates that we want to try and learn detectors that give us the orientation
plist - the list of points to use from the templates
Returns:
return the set of filters, one for each point
Throws:
java.io.FileNotFoundException
java.io.IOException

calculateErrors

public java.util.ArrayList<java.lang.Float> calculateErrors(java.util.ArrayList<Complex[]> images,
                                                            java.util.ArrayList<java.awt.geom.Point2D.Float> points,
                                                            int[] dim,
                                                            java.util.ArrayList<java.lang.Float> maxResp)

calculateErrors

public static java.util.ArrayList<java.lang.Float> calculateErrors(Complex[] filterFT,
                                                                   FloatImage priorFimg,
                                                                   java.util.ArrayList<Complex[]> images,
                                                                   java.util.ArrayList<java.awt.geom.Point2D.Float> points,
                                                                   int[] dim,
                                                                   java.util.ArrayList<java.lang.Float> maxResp)

calculateMultiplyErrors

public java.util.ArrayList<java.lang.Float> calculateMultiplyErrors(java.util.ArrayList<FloatImage> filters,
                                                                    java.util.ArrayList<Complex[]> images,
                                                                    java.util.ArrayList<java.awt.geom.Point2D.Float> points,
                                                                    int[] dim,
                                                                    java.util.ArrayList<java.lang.Float> maxResp)

bootstrapBuildFilter

public FloatImage bootstrapBuildFilter(java.lang.String file,
                                       HaarReader haar,
                                       boolean orientation,
                                       int[] plist)
                                throws java.io.FileNotFoundException,
                                       java.io.IOException
Build a set of OESF filters from a set of images and one labeled example

Parameters:
file - the name of the file containing the list of images and template
haar - the face detector
orientation - idicates that we want to try and learn detectors that give us the orientation
plist - the list of points to use from the templates
Returns:
return the set of filters, one for each point
Throws:
java.io.FileNotFoundException
java.io.IOException

detectFeature

public static java.awt.geom.Point2D.Float detectFeature(Complex[] img,
                                                        Complex[] filt,
                                                        int[] dim)

detectFeature

public static java.awt.geom.Point2D.Float detectFeature(Complex[] img,
                                                        Complex[] filt,
                                                        FloatImage prior,
                                                        int[] dim)

write

public void write(java.lang.String dir)
           throws java.io.IOException
Writes the filters and prior data to file

Parameters:
dir - the folder to write to (filter.fimg, prior.fimg and data.txt are written to this file)
Throws:
java.io.IOException

read

public void read(java.lang.String dir)
          throws java.io.IOException
Read an OESF filter from file

Parameters:
dir - the directory to read from (reads filter.fimg, prior.fimg and data.txt)
Throws:
java.io.IOException

centre

public static FloatImage centre(FloatImage fimg)
Shift the origin to centre for easier human veiwing / interpretation

Parameters:
fimg - the filter image to centre
Returns:
returns the fiter image centred on its midpoint

filter

public static FloatImage filter(FloatImage fimg,
                                Complex[] filt,
                                int[] dim)
Apply a filter to an image

Parameters:
fimg - the image to apply the filter to
filt - the FFT of the filter
dim - the dimensionality data {128, 128}
Returns:
return the filtered image (spatial domain)

filter

public static FloatImage filter(FloatImage fimg,
                                FloatImage filt,
                                int[] dim)
Apply a filter to an image

Parameters:
fimg - the image to apply the filter to
filt - the filter
dim - the dimensionality data {128, 128}
Returns:
return the filtered image (spatial domain)

filter

public static FloatImage filter(Complex[] data,
                                Complex[] filt,
                                int[] dim)
Apply a filter to an image

Parameters:
fimg - the image to apply the filter to
filt - the FFT of the filter
dim - the dimensionality data {128, 128}
Returns:
return the filtered image (spatial domain)

filterComplex

public static FloatImage[] filterComplex(FloatImage fimg,
                                         Complex[] filt,
                                         int[] dim)
Filters a real image with a complex filter

Parameters:
fimg - the image to filter
filt - the FFT of the complex filter
dim - the dimensionality
Returns:
returns the real and imaginary components of the filtered image

detectFace

public static java.awt.Rectangle detectFace(java.awt.image.BufferedImage bimg,
                                            HaarReader haar)
Detect a face

Parameters:
bimg - the image to look for faces in
haar -
Returns:
return the biggest face detected or null

detectFace

public static java.awt.Rectangle detectFace(java.awt.image.BufferedImage bimg,
                                            HaarReader haar,
                                            java.awt.Rectangle prevRect)
Detect a face

Parameters:
bimg - the image to look for faces in
haar - the face detector
prevRect - the previous rectangle, if not null it is added to list of candidate rectangles
Returns:
return the biggest face detected or null

testFilter

public static void testFilter(java.lang.String file,
                              java.util.ArrayList<FloatImage> filters,
                              FloatImage prior,
                              HaarReader haar,
                              int[] plist)
                       throws java.io.FileNotFoundException,
                              java.io.IOException
Test the filter

Parameters:
file - the file to read image and template names from
filters - the filters to use
prior - the prior data
haar - the face detector
Throws:
java.io.FileNotFoundException
java.io.IOException

testFilterComplex

public static void testFilterComplex(java.lang.String file,
                                     java.util.ArrayList<FloatImage[]> filters,
                                     FloatImage prior,
                                     HaarReader haar)
                              throws java.io.FileNotFoundException,
                                     java.io.IOException
Test the complex filter

Parameters:
file - the file to read image and template names from
filters - the filters to use
prior - the prior data
haar - the face detector
Throws:
java.io.FileNotFoundException
java.io.IOException

main

public static void main(java.lang.String[] args)
                 throws java.io.FileNotFoundException,
                        java.io.IOException
Main method used for testing

Parameters:
args - the program args, args[0] = list of images and template to build from, args[1] = test list
Throws:
java.io.FileNotFoundException
java.io.IOException

hasNextQuotedString

public static boolean hasNextQuotedString(java.util.Scanner sc)
Returns true if the next token is the beginning of a quoted string value.

Parameters:
sc - the Scanner to read from
Returns:
returns true if the Scanner has a next quoted string

nextQuotedString

public static java.lang.String nextQuotedString(java.util.Scanner sc)
Scans a quoted string value from the scanner input.

Parameters:
sc - the Scanner to read from
Returns:
returns the next quoted string