26#include <fvmodels/shape/rht_lines.h>
28#include <utils/math/angle.h>
33#define TEST_IF_IS_A_PIXEL(x) ((x) > 230)
44 unsigned int nr_candidates,
48 float min_votes_ratio,
51 RHT_MAX_TIME = max_time;
52 RHT_MAX_ITER = max_iter;
54 RHT_NR_CANDIDATES = nr_candidates;
56 RHT_R_SCALE = r_scale;
58 RHT_MIN_VOTES = min_votes;
59 RHT_MIN_VOTES_RATIO = min_votes_ratio;
61 RHT_ANGLE_FROM = angle_from - (floor(angle_from / (2 * M_PI)) * (2 * M_PI));
62 RHT_ANGLE_RANGE = angle_range - (floor(angle_range / (2 * M_PI)) * (2 * M_PI));
63 RHT_ANGLE_INCREMENT = RHT_ANGLE_RANGE / RHT_NR_CANDIDATES;
80 struct timeval start, now;
90 unsigned char * line_start = buffer;
92 vector<upoint_t> pixels;
94 gettimeofday(&start, NULL);
96 for (y = 0; y < roi->
height; ++y) {
97 for (x = 0; x < roi->
width; ++x) {
98 if (TEST_IF_IS_A_PIXEL(*buffer)) {
100 pixels.push_back(pt);
112 vector<upoint_t>::iterator pos;
114 if (pixels.size() == 0) {
121 if (pixels.size() > 0) {
122 int ri = rand() % pixels.size();
123 pos = pixels.begin() + ri;
127 for (
unsigned int i = 0; i < RHT_NR_CANDIDATES; ++i) {
128 phi = RHT_ANGLE_FROM + i * RHT_ANGLE_INCREMENT;
129 r = p.
x * cos(phi) + p.
y * sin(phi);
133 accumulator.
accumulate((
int)round(r / RHT_R_SCALE), angle, 0);
136 gettimeofday(&now, NULL);
138 diff_sec = now.tv_sec - start.tv_sec;
139 diff_usec = now.tv_usec - start.tv_usec;
142 diff_usec += 1000000;
145 f_diff_sec = diff_sec + diff_usec / 1000000.f;
148 }
while ((++num_iter < RHT_MAX_ITER) && (f_diff_sec < RHT_MAX_TIME));
151 int max, r_max, phi_max, any_max;
152 max = accumulator.
getMax(r_max, phi_max, any_max);
154 roi_width = roi->
width;
158 l.r = r_max * RHT_R_SCALE;
161 m_Lines.push_back(l);
169 return m_Lines.size();
175 if (
id < 0 || (
unsigned int)
id >= m_Lines.size()) {
178 return const_cast<LineShape *
>(&m_Lines[id]);
185 if (m_Lines.size() == 0) {
187 }
else if (m_Lines.size() == 1) {
188 return const_cast<LineShape *
>(&m_Lines[0]);
191 for (
unsigned int i = 1; i < m_Lines.size(); ++i) {
192 if (m_Lines[i].count > m_Lines[cur].count) {
196 return const_cast<LineShape *
>(&m_Lines[cur]);
206 int votes = (int)(accumulator.
getNumVotes() * (float)RHT_MIN_VOTES_RATIO);
208 if (RHT_MIN_VOTES > votes) {
209 votes = RHT_MIN_VOTES;
212 vector<LineShape> *rv =
new vector<LineShape>();
214 vector<vector<int>> * rht_nodes = accumulator.
getNodes(votes);
215 vector<vector<int>>::iterator node_it;
219 for (node_it = rht_nodes->begin(); node_it != rht_nodes->end(); ++node_it) {
220 l.r = node_it->at(0) * RHT_R_SCALE;
221 l.phi = node_it->at(1);
223 l.count = node_it->at(3);
void calcPoints()
Calc points for line.
unsigned int height
ROI height.
unsigned char * get_roi_buffer_start(unsigned char *buffer) const
Get ROI buffer start.
unsigned int line_step
line step
unsigned int width
ROI width.
unsigned int getNumVotes() const
Get number of votes.
int getMax(int &x, int &y, int &r) const
Get maximum.
std::vector< std::vector< int > > * getNodes(int min_count)
Get nodes.
int accumulate(int x, int y, int r)
Accumulate new candidate.
RhtLinesModel(float max_time=0.005, int max_iter=1000, unsigned int nr_candidates=40, float angle_from=0, float angle_range=2 *M_PI, int r_scale=1, float min_votes_ratio=0.2f, int min_votes=-1)
Creates a new RhtLinesModel instance.
int getShapeCount(void) const
Get number of shapes.
std::vector< LineShape > * getShapes()
Get shapes.
int parseImage(unsigned char *buffer, ROI *roi)
Parse image for given ROI.
LineShape * getShape(int id) const
Get specific shape.
virtual ~RhtLinesModel(void)
Destructor.
LineShape * getMostLikelyShape(void) const
Get best candidate.
Fawkes library namespace.
float rad2deg(float rad)
Convert an angle given in radians to degrees.
Point with cartesian coordinates as unsigned integers.
unsigned int x
x coordinate
unsigned int y
y coordinate