Fawkes API  Fawkes Development Version
ht_lines.cpp
1 
2 /***************************************************************************
3  * ht_lines.cpp - Implementation of a lines shape finder
4  * with Randomized Hough Transform
5  *
6  * Created: Fri Jan 13 12:42:53 2006
7  * Copyright 2005-2006 Tim Niemueller [www.niemueller.de]
8  * Hu Yuxiao <Yuxiao.Hu@rwth-aachen.de>
9  *
10  ****************************************************************************/
11 
12 /* This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version. A runtime exception applies to
16  * this software (see LICENSE.GPL_WRE file mentioned below for details).
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Library General Public License for more details.
22  *
23  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
24  */
25 
26 #include <fvmodels/shape/ht_lines.h>
27 #include <sys/time.h>
28 #include <utils/math/angle.h>
29 
30 #include <cmath>
31 
32 using namespace std;
33 using namespace fawkes;
34 
35 namespace firevision {
36 
37 #define TEST_IF_IS_A_PIXEL(x) ((x) > 230)
38 
39 /** @class HtLinesModel <fvmodels/shape/ht_lines.h>
40  * Hough-Transform line matcher.
41  */
42 
43 /** Constructor.
44  * @param nr_candidates number of candidates
45  * @param angle_from slope of lines from
46  * @param angle_range angle range
47  * @param r_scale r scale
48  * @param min_votes_ratio min votes ratio
49  * @param min_votes minimum number of votes for a candidate to consider
50  */
51 HtLinesModel::HtLinesModel(unsigned int nr_candidates,
52  float angle_from,
53  float angle_range,
54  int r_scale,
55  float min_votes_ratio,
56  int min_votes)
57 {
58  RHT_NR_CANDIDATES = nr_candidates;
59 
60  RHT_R_SCALE = r_scale;
61 
62  RHT_MIN_VOTES = min_votes;
63  RHT_MIN_VOTES_RATIO = min_votes_ratio;
64 
65  RHT_ANGLE_FROM = angle_from - (floor(angle_from / (2 * M_PI)) * (2 * M_PI));
66  RHT_ANGLE_RANGE = angle_range - (floor(angle_range / (2 * M_PI)) * (2 * M_PI));
67  RHT_ANGLE_INCREMENT = RHT_ANGLE_RANGE / RHT_NR_CANDIDATES;
68 }
69 
70 /** Destructor. */
71 HtLinesModel::~HtLinesModel(void)
72 {
73  m_Lines.clear();
74 }
75 
76 int
77 HtLinesModel::parseImage(unsigned char *buf, ROI *roi)
78 {
79  unsigned char *buffer = roi->get_roi_buffer_start(buf);
80 
81  // clear the accumulator
82  accumulator.reset();
83 
84  // clear all the remembered lines
85  m_Lines.clear();
86 
87  // First, find all the edge pixels,
88  // and store them in the 'pixels' vector.
89  unsigned char * line_start = buffer;
90  unsigned int x, y;
91  vector<upoint_t> pixels;
92 
93  for (y = 0; y < roi->height; ++y) {
94  for (x = 0; x < roi->width; ++x) {
95  if (TEST_IF_IS_A_PIXEL(*buffer)) {
96  upoint_t pt = {x, y};
97  pixels.push_back(pt);
98  }
99  // NOTE: this assumes roi->pixel_step == 1
100  ++buffer;
101  }
102  line_start += roi->line_step;
103  buffer = line_start;
104  }
105 
106  // Then perform the RHT algorithm
107  upoint_t p;
108  float r, phi; // used for line representation
109  vector<upoint_t>::iterator pos;
110  if (pixels.size() == 0) {
111  // No edge pixels found => no lines
112  return 0;
113  }
114 
115  while (pixels.size() > 0) {
116  p = pixels.back();
117  pixels.pop_back();
118 
119  for (unsigned int i = 0; i < RHT_NR_CANDIDATES; ++i) {
120  phi = RHT_ANGLE_FROM + i * RHT_ANGLE_INCREMENT;
121  r = p.x * cos(phi) + p.y * sin(phi);
122 
123  int angle = (int)round(fawkes::rad2deg(phi));
124 
125  accumulator.accumulate((int)round(r / RHT_R_SCALE), angle, 0);
126  }
127  }
128 
129  // Find the most dense region, and decide on the lines
130  int max, r_max, phi_max, any_max;
131  max = accumulator.getMax(r_max, phi_max, any_max);
132 
133  roi_width = roi->width;
134  roi_height = roi->height;
135 
136  LineShape l(roi->width, roi->height);
137  l.r = r_max * RHT_R_SCALE;
138  l.phi = phi_max;
139  l.count = max;
140  m_Lines.push_back(l);
141 
142  return 1;
143 }
144 
145 int
146 HtLinesModel::getShapeCount(void) const
147 {
148  return m_Lines.size();
149 }
150 
151 LineShape *
152 HtLinesModel::getShape(int id) const
153 {
154  if (id < 0 || (unsigned int)id >= m_Lines.size()) {
155  return NULL;
156  } else {
157  return const_cast<LineShape *>(&m_Lines[id]); // or use const Shape* def?!...
158  }
159 }
160 
161 LineShape *
162 HtLinesModel::getMostLikelyShape(void) const
163 {
164  if (m_Lines.size() == 0) {
165  return NULL;
166  } else if (m_Lines.size() == 1) {
167  return const_cast<LineShape *>(&m_Lines[0]); // or use const Shape* def?!...
168  } else {
169  int cur = 0;
170  for (unsigned int i = 1; i < m_Lines.size(); ++i) {
171  if (m_Lines[i].count > m_Lines[cur].count) {
172  cur = i;
173  }
174  }
175  return const_cast<LineShape *>(&m_Lines[cur]); // or use const Shape* definition?!...
176  }
177 }
178 
179 /** Get all lines found.
180  * @return vector with all line shapes.
181  */
182 vector<LineShape> *
183 HtLinesModel::getShapes()
184 {
185  int votes = (int)(accumulator.getNumVotes() * (float)RHT_MIN_VOTES_RATIO);
186 
187  if (RHT_MIN_VOTES > votes) {
188  votes = RHT_MIN_VOTES;
189  }
190 
191  vector<LineShape> *rv = new vector<LineShape>();
192 
193  vector<vector<int>> * rht_nodes = accumulator.getNodes(votes);
194  vector<vector<int>>::iterator node_it;
195 
196  LineShape l(roi_width, roi_height);
197 
198  for (node_it = rht_nodes->begin(); node_it != rht_nodes->end(); ++node_it) {
199  l.r = node_it->at(0) * RHT_R_SCALE;
200  l.phi = node_it->at(1);
201  // we do not use val 2 here!
202  l.count = node_it->at(3);
203  l.calcPoints();
204  rv->push_back(l);
205  }
206 
207  return rv;
208 }
209 
210 } // end namespace firevision
Fawkes library namespace.
unsigned int y
y coordinate
Definition: types.h:37
unsigned int x
x coordinate
Definition: types.h:36
unsigned int width
ROI width.
Definition: roi.h:117
Region of interest.
Definition: roi.h:54
void calcPoints()
Calc points for line.
Definition: line.cpp:96
unsigned char * get_roi_buffer_start(unsigned char *buffer) const
Get ROI buffer start.
Definition: roi.cpp:526
Line shape.
Definition: line.h:40
Point with cartesian coordinates as unsigned integers.
Definition: types.h:34
float rad2deg(float rad)
Convert an angle given in radians to degrees.
Definition: angle.h:46
unsigned int height
ROI height.
Definition: roi.h:119
unsigned int line_step
line step
Definition: roi.h:125