40 #include <visp3/core/vpCameraParameters.h> 41 #include <visp3/core/vpConfig.h> 42 #include <visp3/core/vpMomentBasic.h> 43 #include <visp3/core/vpMomentObject.h> 44 #include <visp3/core/vpPixelMeterConversion.h> 49 #ifdef VISP_HAVE_OPENMP 63 double vpMomentObject::calc_mom_polygon(
unsigned int p,
unsigned int q,
const std::vector<vpPoint> &points)
71 den =
static_cast<double>((p + q + 2) * (p + q + 1) *
vpMath::comb(p + q, p));
74 for (i = 1; i <= points.size() - 1; i++) {
77 for (k = 0; k <= p; k++) {
79 x_p_k = pow(points[i - 1].get_x(), (
int)(p - k));
80 for (l = 0; l <= q; l++) {
81 y_q_l = pow(points[i - 1].get_y(), (
int)(q - l));
86 y_l *= points[i].get_y();
88 x_k *= points[i].get_x();
91 s *= ((points[i - 1].get_x()) * (points[i].get_y()) - (points[i].get_x()) * (points[i - 1].get_y()));
115 for (
unsigned int i = 1; i <
order; i++)
116 cache[i] = cache[i - 1] * x;
118 for (
unsigned int j = order; j < order *
order; j +=
order)
119 cache[j] = cache[j - order] * y;
121 for (
unsigned int j = 1; j <
order; j++) {
122 for (
unsigned int i = 1; i < order - j; i++) {
123 cache[j * order + i] = cache[j *
order] * cache[i];
135 cache[0] = IntensityNormalized;
137 double invIntensityNormalized = 0.;
138 if (std::fabs(IntensityNormalized) >= std::numeric_limits<double>::epsilon())
139 invIntensityNormalized = 1.0 / IntensityNormalized;
141 for (
unsigned int i = 1; i <
order; i++)
142 cache[i] = cache[i - 1] * x;
144 for (
unsigned int j = order; j < order *
order; j +=
order)
145 cache[j] = cache[j - order] * y;
147 for (
unsigned int j = 1; j <
order; j++) {
148 for (
unsigned int i = 1; i < order - j; i++) {
149 cache[j * order + i] = cache[j *
order] * cache[i] * invIntensityNormalized;
233 if (std::fabs(points.rbegin()->get_x() - points.begin()->get_x()) > std::numeric_limits<double>::epsilon() ||
234 std::fabs(points.rbegin()->get_y() - points.begin()->get_y()) > std::numeric_limits<double>::epsilon()) {
235 points.resize(points.size() + 1);
236 points[points.size() - 1] = points[0];
238 for (
unsigned int j = 0; j <
order *
order; j++) {
239 values[j] = calc_mom_polygon(j % order, j / order, points);
243 values.assign(order * order, 0);
244 for (
unsigned int i = 0; i < points.size(); i++) {
245 cacheValues(cache, points[i].get_x(), points[i].get_y());
246 for (
unsigned int j = 0; j <
order; j++) {
247 for (
unsigned int k = 0; k < order - j; k++) {
248 values[j * order + k] += cache[j * order + k];
292 #ifdef VISP_HAVE_OPENMP 293 #pragma omp parallel shared(threshold) 296 curvals.assign(order * order, 0.);
298 #pragma omp for nowait // automatically organize loop counter between threads 299 for (
int i = 0; i < (int)image.
getCols(); i++) {
300 for (
int j = 0; j < (int)image.
getRows(); j++) {
301 unsigned int i_ =
static_cast<unsigned int>(i);
302 unsigned int j_ =
static_cast<unsigned int>(j);
303 if (image[j_][i_] > threshold) {
309 for (
unsigned int k = 0; k <
order; k++) {
311 for (
unsigned int l = 0; l < order - k; l++) {
312 curvals[(k * order + l)] += (xval * yval);
321 #pragma omp master // only set this variable in master thread 323 values.assign(order * order, 0.);
327 for (
unsigned int k = 0; k <
order; k++) {
328 for (
unsigned int l = 0; l < order - k; l++) {
330 values[k * order + l] += curvals[k * order + l];
336 values.assign(order * order, 0);
337 for (
unsigned int i = 0; i < image.
getCols(); i++) {
338 for (
unsigned int j = 0; j < image.
getRows(); j++) {
339 if (image[j][i] > threshold) {
344 for (
unsigned int k = 0; k <
order; k++) {
345 for (
unsigned int l = 0; l < order - k; l++) {
346 values[k * order + l] += cache[k * order + l];
355 double norm_factor = 1. / (cam.
get_px() * cam.
get_py());
356 for (std::vector<double>::iterator it =
values.begin(); it !=
values.end(); ++it) {
357 *it = (*it) * norm_factor;
377 values.assign(order * order, 0);
383 unsigned int idx = 0;
384 unsigned int kidx = 0;
386 double intensity = 0;
399 for (
unsigned int j = 0; j < image.
getRows(); j++) {
400 for (
unsigned int i = 0; i < image.
getCols(); i++) {
403 intensity = (double)(image[j][i]) * iscale;
404 double intensity_white = 1. - intensity;
412 for (
unsigned int k = 0; k <
order; k++) {
414 for (
unsigned int l = 0; l < order - k; l++) {
416 values[idx] += cache[idx];
423 for (
unsigned int j = 0; j < image.
getRows(); j++) {
424 for (
unsigned int i = 0; i < image.
getCols(); i++) {
427 intensity = (double)(image[j][i]) * iscale;
435 for (
unsigned int k = 0; k <
order; k++) {
437 for (
unsigned int l = 0; l < order - k; l++) {
439 values[idx] += cache[idx];
446 if (normalize_with_pix_size) {
448 double norm_factor = 1. / (cam.
get_px() * cam.
get_py());
449 for (std::vector<double>::iterator it =
values.begin(); it !=
values.end(); ++it) {
450 *it = (*it) * norm_factor;
461 order = orderinp + 1;
545 "been computed, you should " 546 "specify a higher order.");
562 "a higher order for the moment object.");
584 for (
unsigned int i = 0; i < m.
values.size(); i++) {
586 if (i % (m.
order) == 0)
607 std::vector<double> moment = momobj.
get();
608 os << std::endl <<
"Order of vpMomentObject: " << momobj.
getOrder() << std::endl;
610 for (
unsigned int k = 0; k <= momobj.
getOrder(); k++) {
611 for (
unsigned int l = 0; l < (momobj.
getOrder() + 1) - k; l++) {
612 os <<
"m[" << l <<
"," << k <<
"] = " << moment[k * (momobj.
getOrder() + 1) + l] <<
"\t";
648 std::vector<double> moment = momobj.
get();
651 for (
unsigned int k = 0; k <=
order; k++) {
652 for (
unsigned int l = 0; l < (order + 1) - k; l++) {
653 M[l][k] = moment[k * (order + 1) + l];
Used to indicate that a value is not in the allowed range.
Implementation of a matrix and operations on matrices.
vpMomentObject(unsigned int order)
unsigned int getRows() const
static vpMatrix convertTovpMatrix(const vpMomentObject &momobj)
unsigned int getOrder() const
static void printWithIndices(const vpMomentObject &momobj, std::ostream &os)
error that can be emited by ViSP classes.
Class for generic objects.
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
virtual ~vpMomentObject()
friend VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpMomentObject &v)
void fromImage(const vpImage< unsigned char > &image, unsigned char threshold, const vpCameraParameters &cam)
void init(unsigned int orderinp)
void cacheValues(std::vector< double > &cache, double x, double y)
unsigned int getCols() const
void set(unsigned int i, unsigned int j, const double &value_ij)
Generic class defining intrinsic camera parameters.
const std::vector< double > & get() const
bool flg_normalize_intensity
vpObjectType getType() const
void fromVector(std::vector< vpPoint > &points)
std::vector< double > values
static long double comb(unsigned int n, unsigned int p)