1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 }