Fawkes API Fawkes Development Version
circle.cpp
1
2/***************************************************************************
3 * circle.cpp - Implementation of a circle shape finder
4 *
5 * Created: Thu May 16 00:00:00 2005
6 * Copyright 2005 Tim Niemueller [www.niemueller.de]
7 * Hu Yuxiao <Yuxiao.Hu@rwth-aachen.de>
8 *
9 ****************************************************************************/
10
11/* This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version. A runtime exception applies to
15 * this software (see LICENSE.GPL_WRE file mentioned below for details).
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23 */
24
25#include <fvmodels/shape/circle.h>
26
27#include <cmath>
28
29using namespace std;
30using namespace fawkes;
31
32namespace firevision {
33
34/** @class Circle <fvmodels/shape/circle.h>
35 * Circle shape.
36 */
37
38/** Constructor. */
40{
41 center.x = center.y = 0.0f;
42 radius = -1.0f;
43 count = 0;
44}
45
46/** Constructor.
47 * @param c center
48 * @param r radius
49 * @param n number of pixels
50 */
51Circle::Circle(const center_in_roi_t &c, float r, int n)
52{
53 center = c;
54 radius = r;
55 count = n;
56}
57
58/** Print info.
59 * @param stream stream to print to
60 */
61void
62Circle::printToStream(std::ostream &stream)
63{
64 stream << "center=(" << center.x << "," << center.y << ")"
65 << " radius=" << radius << " count= " << count;
66}
67
68/** Fit circle.
69 * Fit a circle through the given points.
70 * @param points points to fit circle through.
71 */
72void
73Circle::fitCircle(vector<upoint_t> &points)
74{
75 // due to fixed width, do not use arrays to save addressing time...
76 double A00 = 0.0, A01 = 0.0, A02 = 0.0;
77 double A10 = 0.0, A11 = 0.0, A12 = 0.0;
78 double A20 = 0.0, A21 = 0.0, A22 = 0.0;
79 double b0 = 0.0, b1 = 0.0, b2 = 0.0;
80
81 // generating A'A and A'b
82 int count = points.size();
83 for (int i = 0; i < count; i++) {
84 upoint_t &t = points[i];
85 double x0 = 2.0f * t.x;
86 double y0 = 2.0f * t.y;
87 double b = (double)(t.x * t.x + t.y * t.y);
88 A00 += x0 * x0;
89 A01 += x0 * y0;
90 A02 += x0;
91 A10 += y0 * x0;
92 A11 += y0 * y0;
93 A12 += y0;
94 A20 += x0;
95 A21 += y0;
96 A22 += 1.0;
97 b0 += x0 * b;
98 b1 += y0 * b;
99 b2 += b;
100 }
101
102 // solve the resulting 3 by 3 equations
103 double delta = +A00 * A11 * A22 + A01 * A12 * A20 + A02 * A10 * A21 - A00 * A12 * A21
104 - A01 * A10 * A22 - A02 * A11 * A20;
105 center.x = (float)((+b0 * A11 * A22 + A01 * A12 * b2 + A02 * b1 * A21 - b0 * A12 * A21
106 - A01 * b1 * A22 - A02 * A11 * b2)
107 / delta);
108 center.y = (float)((+A00 * b1 * A22 + b0 * A12 * A20 + A02 * A10 * b2 - A00 * A12 * b2
109 - b0 * A10 * A22 - A02 * b1 * A20)
110 / delta);
111 radius = (float)sqrtf((+A00 * A11 * b2 + A01 * b1 * A20 + b0 * A10 * A21 - A00 * b1 * A21
112 - A01 * A10 * b2 - b0 * A11 * A20)
113 / delta
114 + (double)center.x * center.x + (double)center.y * center.y);
115 count = points.size();
116}
117
118void
119Circle::setMargin(unsigned int margin)
120{
121 this->margin = margin;
122}
123
124bool
125Circle::isClose(unsigned int in_roi_x, unsigned int in_roi_y)
126{
127 float dx = in_roi_x - center.x;
128 float dy = in_roi_y - center.y;
129
130 float dist = sqrt(dx * dx + dy * dy);
131
132 return ((dist <= (radius + margin)) && (dist >= (radius - margin)));
133}
134
135} // end namespace firevision
Circle()
Constructor.
Definition: circle.cpp:39
void setMargin(unsigned int margin)
Set margin around shape.
Definition: circle.cpp:119
void fitCircle(std::vector< fawkes::upoint_t > &points)
Fit circle.
Definition: circle.cpp:73
float radius
Radius of object.
Definition: circle.h:59
center_in_roi_t center
Center of object in ROI.
Definition: circle.h:57
void printToStream(std::ostream &stream)
Print info.
Definition: circle.cpp:62
bool isClose(unsigned int in_roi_x, unsigned int in_roi_y)
Check if the given point is close to the shape.
Definition: circle.cpp:125
int count
Number of pixels.
Definition: circle.h:61
unsigned int margin
Margin around shape.
Definition: circle.h:63
Fawkes library namespace.
Point with cartesian coordinates as unsigned integers.
Definition: types.h:35
unsigned int x
x coordinate
Definition: types.h:36
unsigned int y
y coordinate
Definition: types.h:37
Center in ROI.
Definition: types.h:38
float y
y in pixels
Definition: types.h:40
float x
x in pixels
Definition: types.h:39