75bcbc9461128043121c29a2a72555947c814386
[svrt.git] / vision_problem_2.cc
1 /*
2  *  svrt is the ``Synthetic Visual Reasoning Test'', an image
3  *  generator for evaluating classification performance of machine
4  *  learning systems, humans and primates.
5  *
6  *  Copyright (c) 2009 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of svrt.
10  *
11  *  svrt is free software: you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  svrt is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with selector.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "vision_problem_2.h"
26 #include "shape.h"
27
28 VisionProblem_2::VisionProblem_2() { }
29
30 void VisionProblem_2::generate(int label, Vignette *vignette) {
31   int x_big, y_big, x_small, y_small;
32   Shape big_shape, small_shape;
33   Vignette mask;
34   int nb_attempts, max_nb_attempts = 10;
35   int dist_min = Vignette::width/8;
36
37   do {
38     vignette->clear();
39     mask.clear();
40
41     big_shape.randomize(big_part_size / 2, big_part_hole_size / 2);
42
43     do {
44       x_big = int(drand48() * Vignette::width);
45       y_big = int(drand48() * Vignette::height);
46     } while(big_shape.overwrites(vignette, x_big, y_big));
47
48     // The mask will encode either a thin area the small shape should
49     // intersect with (class 1) or a thick one it should not (class 0)
50
51     big_shape.draw(&mask, x_big, y_big);
52
53     if(label) {
54       mask.grow();
55     } else {
56       for(int k = 0; k < dist_min; k++) {
57         mask.grow();
58       }
59     }
60
61     big_shape.draw(vignette, x_big, y_big);
62     vignette->fill(x_big, y_big, 128);
63     vignette->switch_values(128, 255);
64
65     nb_attempts = 0;
66     do {
67       do {
68         small_shape.randomize(small_part_size / 2, small_part_hole_size / 2);
69         x_small = x_big + int((drand48() - 0.5) * big_part_size);
70         y_small = y_big + int((drand48() - 0.5) * big_part_size);
71       } while(small_shape.overwrites(vignette, x_small, y_small)); // ||
72       nb_attempts++;
73     } while(nb_attempts < max_nb_attempts &&
74             ((label && !small_shape.overwrites(&mask, x_small, y_small)) ||
75              (!label && small_shape.overwrites(&mask, x_small, y_small))));
76
77     vignette->replace_value(128, 255);
78     small_shape.draw(vignette, x_small, y_small);
79   } while(nb_attempts >= max_nb_attempts);
80 }