#include "cv.h"
#include "highgui.h"
#include <vector>
#include <iostream>
#include <string>
double random(
int dist_type,
double param1,
double param2) {
static CvRNG rng=0;
if (rng == 0) rng = cvRNG(time(0));
double m_data;
CvMat m = cvMat(1, 1, CV_64F, &m_data);
cvRandArr(&rng, &m, dist_type, cvScalar(param1), cvScalar(param2));
return m_data;
}
double get_y(
double x,
double a,
double b,
double c,
double d,
double e) {
}
double yy =
get_y(xx,
a, b, c,
d, e);
yy += ry;
return true;
}
double *measx=(double *)param;
int data_degree = state->rows-1;
double a = (data_degree >= 4? cvmGet(state, 4, 0) : 0);
double b = (data_degree >= 3? cvmGet(state, 3, 0) : 0);
double c = (data_degree >= 2? cvmGet(state, 2, 0) : 0);
double d = (data_degree >= 1? cvmGet(state, 1, 0) : 0);
double e = (data_degree >= 0? cvmGet(state, 0, 0) : 0);
for (int i=0; i<projection->rows; i++) {
cvmSet(projection, i, 0,
get_y(measx[i], a, b, c, d, e));
}
}
int main(
int argc,
char *argv[])
{
try {
filename = filename.substr(filename.find_last_of('\\') + 1);
std::cout << "SampleOptimization" << std::endl;
std::cout << "==================" << std::endl;
std::cout << std::endl;
std::cout << "Description:" << std::endl;
std::cout << " This is an example of how to use the 'Optimization' class. Random data" << std::endl;
std::cout << " is generated and approximated using curves of increasing degrees." << std::endl;
std::cout << std::endl;
std::cout << "Usage:" << std::endl;
std::cout << " " << filename << std::endl;
std::cout << std::endl;
std::cout << "Keyboard Shortcuts:" << std::endl;
std::cout << " any key: cycle through datasets" << std::endl;
std::cout << " q: quit" << std::endl;
std::cout << std::endl;
IplImage *img = cvCreateImage(cvSize(
res,
res), IPL_DEPTH_8U, 3);
cvNamedWindow("SampleOptimization");
for (int data_degree=0; data_degree<5; data_degree++) {
double a = (data_degree >= 4?
random(CV_RAND_UNI, -0.5, 0.5) : 0);
double b = (data_degree >= 3?
random(CV_RAND_UNI, -0.5, 0.5) : 0);
double c = (data_degree >= 2?
random(CV_RAND_UNI, -0.5, 0.5) : 0);
double d = (data_degree >= 1?
random(CV_RAND_UNI, -0.5, 0.5) : 0);
double e = (data_degree >= 0?
random(CV_RAND_UNI, -0.5, 0.5) : 0);
cvZero(img);
vector<CvPoint2D32f> measvec;
for (int i=0; i<1000; i++) {
measvec.push_back(cvPoint2D32f(x, y));
cvCircle(img, cvPoint(int(x), int(y)), 1, CV_RGB(0,255,0));
}
}
cvShowImage("SampleOptimization", img);
cvWaitKey(10);
double measx[1000];
CvMat *meas = cvCreateMat(measvec.size(), 1, CV_64F);
for (size_t i=0; i<measvec.size(); i++) {
measx[i] = measvec[i].x;
cvmSet(meas, i, 0, measvec[i].
y);
}
for (int degree=0; degree<5; degree++)
{
double param_data[5]={0};
CvMat
param = cvMat(degree+1, 1, CV_64F, param_data);
double a = (degree >= 4? cvmGet(¶m, 4, 0) : 0);
double b = (degree >= 3? cvmGet(¶m, 3, 0) : 0);
double c = (degree >= 2? cvmGet(¶m, 2, 0) : 0);
double d = (degree >= 1? cvmGet(¶m, 1, 0) : 0);
double e = (degree >= 0? cvmGet(¶m, 0, 0) : 0);
for (
int x2=step; x2<
res; x2+=step) {
int x1 = x2-step;
double yy1 =
get_y(xx1, a, b, c, d, e);
double yy2 =
get_y(xx2, a, b, c, d, e);
int y1 = int((yy1*res/
poly_res)+(res/2));
int y2 = int((yy2*res/
poly_res)+(res/2));
cvLine(img, cvPoint(x1,y1), cvPoint(x2,y2), CV_RGB(degree*50,255-(degree*50),255));
}
cvShowImage("SampleOptimization", img);
cvWaitKey(10);
}
cvReleaseMat(&meas);
cvShowImage("SampleOptimization", img);
int key = cvWaitKey(0);
if (key == 'q') {
break;
}
}
cvReleaseImage(&img);
return 0;
}
catch (const std::exception &e) {
std::cout << "Exception: " << e.what() << endl;
}
catch (...) {
std::cout << "Exception: unknown" << std::endl;
}
}