31 #ifndef OPENCV_FLANN_SIMPLEX_DOWNHILL_H_
32 #define OPENCV_FLANN_SIMPLEX_DOWNHILL_H_
43 void addValue(
int pos,
float val,
float* vals,
T* point,
T* points,
int n)
46 for (
int i=0; i<n; ++i) {
47 points[
pos*n+i] = point[i];
52 while (j>0 && vals[j]<vals[j-1]) {
53 swap(vals[j],vals[j-1]);
54 for (
int i=0; i<n; ++i) {
55 swap(points[j*n+i],points[(j-1)*n+i]);
70 template <
typename T,
typename F>
71 float optimizeSimplexDownhill(
T* points,
int n,
F func,
float* vals = NULL )
73 const int MAX_ITERATIONS = 10;
88 vals =
new float[n+1];
89 for (
int i=0; i<n+1; ++i) {
90 float val =
func(points+i*n);
91 addValue(i, val, vals, points+i*n, points, n);
98 if (iterations++ > MAX_ITERATIONS)
break;
101 for (
int j=0; j<n; ++j) {
103 for (
int i=0; i<n; ++i) {
104 p_o[i] += points[j*n+i];
107 for (
int i=0; i<n; ++i) {
111 bool converged =
true;
112 for (
int i=0; i<n; ++i) {
113 if (p_o[i] != points[nn+i]) {
117 if (converged)
break;
120 for (
int i=0; i<n; ++i) {
121 p_r[i] = p_o[i] +
alpha*(p_o[i]-points[nn+i]);
123 float val_r =
func(p_r);
125 if ((val_r>=vals[0])&&(val_r<vals[n])) {
128 Logger::info(
"Choosing reflection\n");
129 addValue(n, val_r,vals, p_r, points, n);
137 for (
int i=0; i<n; ++i) {
138 p_e[i] = 2*p_r[i]-p_o[i];
140 float val_e =
func(p_e);
143 Logger::info(
"Choosing reflection and expansion\n");
144 addValue(n, val_e,vals,p_e,points,n);
147 Logger::info(
"Choosing reflection\n");
148 addValue(n, val_r,vals,p_r,points,n);
152 if (val_r>=vals[n]) {
153 for (
int i=0; i<n; ++i) {
154 p_e[i] = (p_o[i]+points[nn+i])/2;
156 float val_e =
func(p_e);
159 Logger::info(
"Choosing contraction\n");
160 addValue(n,val_e,vals,p_e,points,n);
165 Logger::info(
"Full contraction\n");
166 for (
int j=1; j<=n; ++j) {
167 for (
int i=0; i<n; ++i) {
168 points[j*n+i] = (points[j*n+i]+points[i])/2;
170 float val =
func(points+j*n);
171 addValue(j,val,vals,points+j*n,points,n);
176 float bestVal = vals[0];
181 if (ownVals)
delete[] vals;
InputArrayOfArrays InputArrayOfArrays InputOutputArray InputOutputArray InputOutputArray InputOutputArray Size InputOutputArray InputOutputArray T
Definition: calib3d.hpp:1867
InputArrayOfArrays InputArrayOfArrays InputOutputArray InputOutputArray InputOutputArray InputOutputArray Size InputOutputArray InputOutputArray OutputArray OutputArray F
Definition: calib3d.hpp:1867
CvCmpFunc func
Definition: core_c.h:1712
CvMemStoragePos * pos
Definition: core_c.h:1573
double alpha
Definition: core_c.h:1093
#define CV_DbgAssert(expr)
Definition: base.hpp:375
CV_EXPORTS void swap(Mat &a, Mat &b)
Swaps two matrices.