View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.demos;
31  
32  import java.io.File;
33  import java.io.IOException;
34  
35  import org.openimaj.image.DisplayUtilities;
36  import org.openimaj.image.FImage;
37  import org.openimaj.io.IOUtils;
38  import org.openimaj.math.geometry.point.Point2dImpl;
39  import org.openimaj.math.geometry.shape.Ellipse;
40  import org.openimaj.math.geometry.shape.EllipseUtilities;
41  import org.openimaj.math.statistics.distribution.MixtureOfGaussians;
42  import org.openimaj.math.statistics.distribution.MultivariateGaussian;
43  
44  import Jama.Matrix;
45  
46  public class FVFWDSiftPCAAugmentGMM {
47  	/**
48  	 * @param args
49  	 * @throws IOException
50  	 */
51  	public static void main(String[] args) throws IOException {
52  		// final File indir = new
53  		// File("/Volumes/Raid/face_databases/lfw-centre-affine-pdsift-pca64/");
54  		//
55  		// final Matrix samples = FVFWDSiftPCAAugment.sample(indir, 100000);
56  		//
57  		// final GaussianMixtureModelEM gmmem = new GaussianMixtureModelEM(512,
58  		// CovarianceType.Diagonal);
59  		// final MixtureOfGaussians mog = gmmem.estimate(samples);
60  		//
61  		// IOUtils.writeToFile(mog, new
62  		// File("/Volumes/Raid/face_databases/lfw-centre-affine-pdsift-pca64-augm-gmm512.bin"));
63  		final MixtureOfGaussians mog = IOUtils.readFromFile(new File(
64  				"/Volumes/Raid/face_databases/lfw-centre-affine-pdsift-pca64-augm-gmm512.bin"));
65  
66  		final int w = 500;
67  		final int h = (int) (w * 160.0 / 125.0);
68  
69  		final FImage img = new FImage(w, h);
70  		for (final MultivariateGaussian g : mog.gaussians) {
71  			final double[] mv = g.getMean().getArray()[0];
72  			final double x = mv[mv.length - 2] * w + (w / 2);
73  			final double y = mv[mv.length - 1] * h + (h / 2);
74  
75  			final double xc = g.getCovariance(mv.length - 2, mv.length - 2);
76  			final double yc = g.getCovariance(mv.length - 1, mv.length - 1);
77  
78  			final Matrix sm = new Matrix(new double[][] { { xc, 0 }, { 0, yc }
79  			});
80  
81  			final Ellipse e = EllipseUtilities.ellipseFromCovariance((float) x,
82  					(float) y, sm, 500f);
83  
84  			img.drawPoint(new Point2dImpl(x, y), 1f, 1);
85  			img.drawShape(e, 1f);
86  		}
87  		DisplayUtilities.display(img);
88  	}
89  }