Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
qa_lineht.cpp
1 
2 /***************************************************************************
3  * qa_lineht.cpp - QA for Line Hough Transform
4  *
5  * Created: Wed Dec 30 12:00:00 2009
6  * Copyright 2005-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 /// @cond QA
25 
26 #include "../hough_transform.h"
27 #include <utils/time/tracker.h>
28 #include <utils/math/angle.h>
29 #include <utils/math/coord.h>
30 
31 #include <cstdio>
32 #include <unistd.h>
33 
34 using namespace fawkes;
35 
36 int
37 main(int argc, char **argv)
38 {
39  HoughTransform *ht = new HoughTransform(2);
40 
41  unsigned int num_vals = 24;
42  int angle_step = 360 / num_vals;
43  float r_scale = 100.;
44 
45  int **values = new int*[num_vals];
46  for (unsigned int i = 0; i < num_vals; ++i) {
47  values[i] = new int[2];
48  }
49 
50  float samples[][2] =
51  { { 0, 1}, { 1, 0},
52  { 0, 1}, {-1, 0},
53  {-1, 0}, { 0, -1},
54  { 1, 0}, { 0, -1},
55  { 0, 1}, { 1, 1},
56  { 1, 0}, { 1, 1},
57  { 0, -1}, { 1, -1},
58  {-1, 0}, {-1, 1}
59  };
60 
61  printf("Num samples: %zu\n", (sizeof(samples)/sizeof(float *))/2);
62 
63  for (size_t S = 0; S < (sizeof(samples)/sizeof(float *))/2; ++S) {
64  float x[2], y[2];
65  x[0] = samples[2 * S ][0]; y[0] = samples[2 * S ][1];
66  x[1] = samples[2 * S + 1][0]; y[1] = samples[2 * S + 1][1];
67 
68  ht->reset();
69 
70  for (unsigned int i = 0; i < 2; ++i) {
71  for (unsigned int j = 0; j < num_vals; ++j) {
72  float theta = deg2rad(j * angle_step);
73  float r = x[i] * cos(theta) + y[i] * sin(theta);
74  r *= r_scale;
75  values[j][0] = (int)roundf(r);
76  values[j][1] = j * angle_step;
77  //printf("i=%u j=%u theta=%f r=%f v[0]=%i v[1]=%i\n",
78  // i, j, theta, r, values[j][0], values[j][1]);
79  }
80  ht->process(values, num_vals);
81  }
82 
83  int max_values[2];
84  unsigned int max_count = ht->max(max_values);
85  printf("Max count: %u (%i, %i)\n", max_count, max_values[0],
86  max_values[1]);
87 
88  float phi = deg2rad(max_values[1]);
89  float r = max_values[0] / r_scale;
90  float x1, y1, x2, y2;
91  polar2cart2d(phi, r, &x1, &y1);
92 
93  float y_factor = 1;
94  float alpha; // = deg2rad((max_values[1] % 90));
95  if ( ((max_values[1] >= 0) && (max_values[1] < 90)) ||
96  (max_values[1] >= 270) ) {
97  y_factor = -1;
98  alpha = deg2rad(90 - (max_values[1] % 90));
99  } else {
100  alpha = deg2rad((max_values[1] % 90));
101  }
102  float dx = 1 * cos(alpha);
103  float dy = 1 * y_factor * sin(alpha);
104  x2 = x1 + dx;
105  y2 = y1 + dy;
106 
107  printf("p1=(%f,%f) p2=(%f, %f)\n", x[0], y[0], x[1], y[1]);
108  printf("r=%f phi=%f alpha=%f dx=%f dy=%f p1=(%f,%f) p2=(%f,%f)\n\n",
109  r, phi, alpha, dx, dy, x1, y1, x2, y2);
110 
111  }
112 
113  delete ht;
114  for (unsigned int i = 0; i < num_vals; ++i) {
115  delete[] values[i];
116  }
117  delete[] values;
118 
119  return 0;
120 }
121 
122 /// @endcond
void polar2cart2d(float polar_phi, float polar_dist, float *cart_x, float *cart_y)
Convert a 2D polar coordinate to a 2D cartesian coordinate.
Definition: coord.h:54
void reset()
Reset Hough transform.
Hough Transformation for N-dimensional representations.
void process(int **values, unsigned int num_values)
Process some samples.
float deg2rad(float deg)
Convert an angle given in degrees to radians.
Definition: angle.h:37
unsigned int max(int *values) const
Get maximum values.