14 double random(
int dist_type,
double param1,
double param2) {
16 if (rng == 0) rng = cvRNG(time(0));
18 CvMat m = cvMat(1, 1, CV_64F, &m_data);
19 cvRandArr(&rng, &m, dist_type, cvScalar(param1), cvScalar(param2));
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);
28 bool get_measurement(
double *x,
double *y,
double a,
double b,
double c,
double d,
double e) {
30 double yy =
get_y(xx, a, b, c, d, e);
35 if (*y < -(
poly_res/2))
return false;
36 if (*y >= (
poly_res/2))
return false;
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));
53 int main(
int argc,
char *argv[])
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;
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);
84 vector<CvPoint2D32f> measvec;
85 for (
int i=0; i<1000; i++) {
88 measvec.push_back(cvPoint2D32f(x, y));
91 cvCircle(img, cvPoint(
int(x),
int(y)), 1, CV_RGB(0,255,0));
94 cvShowImage(
"SampleOptimization", img);
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);
102 for (
int degree=0; degree<5; degree++)
104 double param_data[5]={0};
105 CvMat
param = cvMat(degree+1, 1, CV_64F, param_data);
108 double a = (degree >= 4? cvmGet(¶m, 4, 0) : 0);
109 double b = (degree >= 3? cvmGet(¶m, 3, 0) : 0);
110 double c = (degree >= 2? cvmGet(¶m, 2, 0) : 0);
111 double d = (degree >= 1? cvmGet(¶m, 1, 0) : 0);
112 double e = (degree >= 0? cvmGet(¶m, 0, 0) : 0);
114 for (
int x2=step; x2<
res; x2+=step) {
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));
124 cvShowImage(
"SampleOptimization", img);
128 cvShowImage(
"SampleOptimization", img);
129 int key = cvWaitKey(0);
134 cvReleaseImage(&img);
137 catch (
const std::exception &e) {
138 std::cout <<
"Exception: " << e.what() << endl;
141 std::cout <<
"Exception: unknown" << std::endl;
bool param(const std::string ¶m_name, T ¶m_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...
double random(int dist_type, double param1, double param2)
This file implements several optimization algorithms.
TFSIMD_FORCE_INLINE const tfScalar & x() const
double get_y(double x, double a, double b, double c, double d, double e)
int main(int argc, char *argv[])