Print corner coodinates in decimal form for more precision.
[gocam.git] / gocam_test.cc
blob7a0294fdebd6d9364a3f943b6484c160a666b08e
1 #include <CImg.h>
2 #include "gocam.hh"
3 #include "conf.hh"
5 const char *program = "gocam_test";
7 /** The configuration options of the program. */
8 conf::Config config;
9 gocam::Analyser analyser;
10 std::vector<CImgDisplay*> displays;
12 void
13 print_usage(FILE *file)
15 fprintf(file, "usage: %s [OPTIONS...] IMAGE\n", program);
16 fprintf(file, "%s", config.help_string().c_str());
20 void
21 parse_command_line(int argc, char *argv[])
23 config
24 ('h', "help", "", "", "display help")
25 ('i', "intermediate", "", "", "show also intermediate results")
26 ('s', "save-hough=FILE", "arg", "", "save the hough image")
27 ('u', "use-hough=FILE", "arg", "", "load a pre-computed hough image")
28 ('n', "no-display", "", "", "do not display the results")
29 ('c', "corners", "", "", "print out corner coordinates")
31 config.parse(argc, argv);
32 config.check_required();
35 void
36 draw_grid(CImg<float> &img, float *rgb)
38 for (int s = 0; s < 2; s++) {
39 for (int l = 0; l < (int)analyser.lines[s].size(); l++) {
40 geom::Line line = analyser.lines[s][l];
41 line.add(geom::Point(0.5, 0.5));
42 img.draw_line((int)line.a.x, (int)line.a.y,
43 (int)line.b.x, (int)line.b.y, rgb);
48 void
49 dump_grid()
51 for (int s = 0; s < 2; s++) {
52 printf ("%c: ", "xy"[s]);
53 for (int l = 0; l < (int)analyser.lines[s].size(); l++) {
54 geom::Line line = analyser.lines[s][l];
55 printf("(%d , %d) - (%d - %d) ",
56 (int)line.a.x, (int)line.a.y,
57 (int)line.b.x, (int)line.b.y);
59 printf("\n");
63 void
64 dump_corners()
66 int n = (int)analyser.lines[0].size() - 1;
67 printf("%f %f %f %f %f %f %f %f\n",
68 analyser.lines[0][0].b.x, analyser.lines[0][0].b.y,
69 analyser.lines[0][0].a.x, analyser.lines[0][0].a.y,
70 analyser.lines[0][n].b.x, analyser.lines[0][n].b.y,
71 analyser.lines[0][n].a.x, analyser.lines[0][n].a.y);
74 void
75 analyse_step_by_step()
77 float white[1] = {1};
79 // We create pointers to CImgDisplay classes instead of using local
80 // variables, because the CImg library seems to hang if the local
81 // display classes are destroyed in the end of the function. FIXME:
82 // currently we do not even bother to free them, because this is
83 // just a test program.
85 analyser.compute_line_images();
86 new CImgDisplay(analyser.line_image, "The line image");
87 new CImgDisplay(analyser.weighted_line_image, "The weighted line image");
89 analyser.compute_hough_image();
90 new CImgDisplay(analyser.hough_image, "The hough image");
92 analyser.compute_initial_grid();
93 analyser.tune_grid();
94 analyser.fix_end_points();
95 CImg<float> tmp = analyser.line_image;
96 draw_grid(tmp, white);
97 new CImgDisplay(tmp, "The initial grid lines");
99 while ((int)analyser.lines[0].size() < analyser.board_size ||
100 (int)analyser.lines[1].size() < analyser.board_size)
102 analyser.add_best_line(0);
103 analyser.add_best_line(1);
104 analyser.tune_grid();
105 tmp = analyser.line_image;
106 draw_grid(tmp, white);
107 new CImgDisplay(tmp, "Growing the grid");
112 main(int argc, char *argv[])
114 cimg::info();
116 // Parse command line arguments
117 parse_command_line(argc, argv);
118 if (config.arguments.empty() || config["help"].specified) {
119 print_usage(stdout);
120 exit(0);
123 // Load image file and possible pre-computed hough image
124 CImg<float> original_image(config.arguments[0].c_str());
125 original_image.normalize(0, 1);
126 analyser.reset(original_image);
127 if (config["use-hough"].specified)
128 // FIXME: should really find how to use load_png() instead
129 analyser.hough_image =
130 CImg<float>::get_load_png(config["use-hough"].value.c_str());
132 // Analyse the image
133 analyser.verbose = 1;
134 if (config["intermediate"].specified)
135 analyse_step_by_step();
136 else
137 analyser.analyse();
139 // Save the hough image if reguested
140 if (config['s'].specified)
141 analyser.hough_image.save_png(config['s'].value.c_str());
143 if (config["corners"].specified)
144 dump_corners();
146 if (config["no-display"].specified)
147 exit(0);
149 // Display result
150 float blue[3] = {0, 0, 1};
151 draw_grid(original_image, blue);
152 CImgDisplay display(original_image, "The final analysis");
153 while (!display.is_closed) {
154 display.wait(100);
155 if (display.button)
156 exit(0);