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.image.objectdetection.haar.training;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.util.List;
35  
36  import org.openimaj.image.DisplayUtilities;
37  import org.openimaj.image.FImage;
38  import org.openimaj.image.ImageUtilities;
39  import org.openimaj.image.objectdetection.haar.HaarFeature;
40  import org.openimaj.image.objectdetection.haar.WeightedRectangle;
41  import org.openimaj.image.processing.resize.ResizeProcessor;
42  import org.openimaj.math.geometry.shape.Rectangle;
43  
44  public class DrawingTest {
45  	static FImage loadPositive() throws IOException {
46  		final String base = "/Users/jsh2/Data/att_faces/s%d/%d.pgm";
47  
48  		final FImage image = new FImage(400, 400);
49  		for (int j = 1; j <= 40; j++) {
50  			for (int i = 1; i <= 10; i++) {
51  				final File file = new File(String.format(base, j, i));
52  
53  				FImage img = ImageUtilities.readF(file);
54  				img = img.extractCenter(50, 50);
55  				img = ResizeProcessor.resample(img, 400, 400);
56  				image.addInplace(img);
57  			}
58  		}
59  
60  		return image.normalise();
61  	}
62  
63  	public static FImage drawRects(WeightedRectangle[] rects) {
64  		final FImage image = new FImage(400, 400);
65  		return drawRects(rects, image);
66  	}
67  
68  	public static FImage drawRects(WeightedRectangle[] rects, FImage image) {
69  		final int scale = 20;
70  		for (final WeightedRectangle r : rects) {
71  
72  			final Rectangle rect = new Rectangle(scale * r.x, scale * r.y, scale * r.width, scale * r.height);
73  
74  			image.drawShape(rect, 1F);
75  		}
76  		return image;
77  	}
78  
79  	public static void main(String[] args) throws IOException, InterruptedException {
80  		final List<HaarFeature> features = HaarFeatureType.generateFeatures(20, 20, HaarFeatureType.BASIC);
81  
82  		final FImage img = loadPositive();
83  		for (int i = 0; i < features.size(); i++) {
84  			final HaarFeature f = features.get(i);
85  
86  			DisplayUtilities.displayName(drawRects(f.rects, img.clone()), "foo");
87  			Thread.sleep(100);
88  		}
89  	}
90  }