SampleOptimization.cpp
Go to the documentation of this file.
1 #include "Optimization.h"
2 #include "cv.h"
3 #include "highgui.h"
4 #include <time.h>
5 #include <vector>
6 #include <iostream>
7 #include <string>
8 using namespace std;
9 using namespace alvar;
10 
11 const int res=640;
12 const double poly_res=8.0;
13 
14 double random(int dist_type, double param1, double param2) {
15  static CvRNG rng=0;
16  if (rng == 0) rng = cvRNG(time(0));
17  double m_data;
18  CvMat m = cvMat(1, 1, CV_64F, &m_data);
19  cvRandArr(&rng, &m, dist_type, cvScalar(param1), cvScalar(param2));
20  return m_data;
21 }
22 
23 double get_y(double x, double a, double b, double c, double d, double e) {
24  return (a*x*x*x*x + b*x*x*x + c*x*x + d*x + e);
25 }
26 
27 // Just generate some random data that can be used as sensor input
28 bool get_measurement(double *x, double *y, double a, double b, double c, double d, double e) {
29  double xx = random(CV_RAND_UNI, -(poly_res/2), +(poly_res/2)); //(rand()*poly_res/RAND_MAX)-(poly_res/2);
30  double yy = get_y(xx, a, b, c, d, e);
31  double ry = random(CV_RAND_NORMAL, 0, poly_res/8); //(rand()*(poly_res/4)/RAND_MAX)-(poly_res/8);
32  yy += ry;
33  *x = xx;
34  *y = yy;
35  if (*y < -(poly_res/2)) return false;
36  if (*y >= (poly_res/2)) return false;
37  return true;
38 }
39 
40 void Estimate(CvMat* state, CvMat *projection, void *param) {
41  double *measx=(double *)param;
42  int data_degree = state->rows-1;
43  double a = (data_degree >= 4? cvmGet(state, 4, 0) : 0);
44  double b = (data_degree >= 3? cvmGet(state, 3, 0) : 0);
45  double c = (data_degree >= 2? cvmGet(state, 2, 0) : 0);
46  double d = (data_degree >= 1? cvmGet(state, 1, 0) : 0);
47  double e = (data_degree >= 0? cvmGet(state, 0, 0) : 0);
48  for (int i=0; i<projection->rows; i++) {
49  cvmSet(projection, i, 0, get_y(measx[i], a, b, c, d, e));
50  }
51 }
52 
53 int main(int argc, char *argv[])
54 {
55  try {
56  // Output usage message
57  std::string filename(argv[0]);
58  filename = filename.substr(filename.find_last_of('\\') + 1);
59  std::cout << "SampleOptimization" << std::endl;
60  std::cout << "==================" << std::endl;
61  std::cout << std::endl;
62  std::cout << "Description:" << std::endl;
63  std::cout << " This is an example of how to use the 'Optimization' class. Random data" << std::endl;
64  std::cout << " is generated and approximated using curves of increasing degrees." << std::endl;
65  std::cout << std::endl;
66  std::cout << "Usage:" << std::endl;
67  std::cout << " " << filename << std::endl;
68  std::cout << std::endl;
69  std::cout << "Keyboard Shortcuts:" << std::endl;
70  std::cout << " any key: cycle through datasets" << std::endl;
71  std::cout << " q: quit" << std::endl;
72  std::cout << std::endl;
73 
74  // Processing loop
75  IplImage *img = cvCreateImage(cvSize(res,res), IPL_DEPTH_8U, 3);
76  cvNamedWindow("SampleOptimization");
77  for (int data_degree=0; data_degree<5; data_degree++) {
78  double a = (data_degree >= 4? random(CV_RAND_UNI, -0.5, 0.5) : 0);
79  double b = (data_degree >= 3? random(CV_RAND_UNI, -0.5, 0.5) : 0);
80  double c = (data_degree >= 2? random(CV_RAND_UNI, -0.5, 0.5) : 0);
81  double d = (data_degree >= 1? random(CV_RAND_UNI, -0.5, 0.5) : 0);
82  double e = (data_degree >= 0? random(CV_RAND_UNI, -0.5, 0.5) : 0);
83  cvZero(img);
84  vector<CvPoint2D32f> measvec;
85  for (int i=0; i<1000; i++) {
86  double x, y;
87  if (get_measurement(&x, &y, a, b, c, d, e)) {
88  measvec.push_back(cvPoint2D32f(x, y));
89  x = (x*res/poly_res)+(res/2);
90  y = (y*res/poly_res)+(res/2);
91  cvCircle(img, cvPoint(int(x), int(y)), 1, CV_RGB(0,255,0));
92  }
93  }
94  cvShowImage("SampleOptimization", img);
95  cvWaitKey(10);
96  double measx[1000];
97  CvMat *meas = cvCreateMat(measvec.size(), 1, CV_64F);
98  for (size_t i=0; i<measvec.size(); i++) {
99  measx[i] = measvec[i].x;
100  cvmSet(meas, i, 0, measvec[i].y);
101  }
102  for (int degree=0; degree<5; degree++)
103  {
104  double param_data[5]={0};
105  CvMat param = cvMat(degree+1, 1, CV_64F, param_data);
106  Optimization opt(param.rows, meas->rows);
107  opt.Optimize(&param, meas, 0.1, 100, Estimate, measx);
108  double a = (degree >= 4? cvmGet(&param, 4, 0) : 0);
109  double b = (degree >= 3? cvmGet(&param, 3, 0) : 0);
110  double c = (degree >= 2? cvmGet(&param, 2, 0) : 0);
111  double d = (degree >= 1? cvmGet(&param, 1, 0) : 0);
112  double e = (degree >= 0? cvmGet(&param, 0, 0) : 0);
113  const int step=5;
114  for (int x2=step; x2<res; x2+=step) {
115  int x1 = x2-step;
116  double xx1 = (x1*poly_res/res)-(poly_res/2);
117  double xx2 = (x2*poly_res/res)-(poly_res/2);
118  double yy1 = get_y(xx1, a, b, c, d, e);
119  double yy2 = get_y(xx2, a, b, c, d, e);
120  int y1 = int((yy1*res/poly_res)+(res/2));
121  int y2 = int((yy2*res/poly_res)+(res/2));
122  cvLine(img, cvPoint(x1,y1), cvPoint(x2,y2), CV_RGB(degree*50,255-(degree*50),255));
123  }
124  cvShowImage("SampleOptimization", img);
125  cvWaitKey(10);
126  }
127  cvReleaseMat(&meas);
128  cvShowImage("SampleOptimization", img);
129  int key = cvWaitKey(0);
130  if (key == 'q') {
131  break;
132  }
133  }
134  cvReleaseImage(&img);
135  return 0;
136  }
137  catch (const std::exception &e) {
138  std::cout << "Exception: " << e.what() << endl;
139  }
140  catch (...) {
141  std::cout << "Exception: unknown" << std::endl;
142  }
143 }
Main ALVAR namespace.
Definition: Alvar.h:174
filename
bool param(const std::string &param_name, T &param_val, const T &default_val)
void Estimate(CvMat *state, CvMat *projection, void *param)
bool get_measurement(double *x, double *y, double a, double b, double c, double d, double e)
double Optimize(CvMat *parameters, CvMat *measurements, double stop, int max_iter, EstimateCallback Estimate, void *param=0, OptimizeMethod method=LEVENBERGMARQUARDT, CvMat *parameters_mask=0, CvMat *J_mat=0, CvMat *weights=0)
Runs the optimization loop with selected parameters.
TFSIMD_FORCE_INLINE const tfScalar & y() const
Non-linear optimization routines. There are three methods implemented that include Gauss-Newton...
Definition: Optimization.h:44
const double poly_res
double random(int dist_type, double param1, double param2)
This file implements several optimization algorithms.
TFSIMD_FORCE_INLINE const tfScalar & x() const
Drawable d[32]
unsigned int step
const int res
double get_y(double x, double a, double b, double c, double d, double e)
int main(int argc, char *argv[])


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Mon Jun 10 2019 12:47:04