Commit a14a735d authored by Tadej Borovšak's avatar Tadej Borovšak

Add CS optimization

parent cbc1f345
img-*.png
......@@ -2,8 +2,9 @@ CXX := g++
CXXFLAGS := $(shell pkg-config --cflags opencv) -Wall -Wextra
LDFLAGS := $(shell pkg-config --libs opencv) -Wall -Wextra
APPS := \
mean-shift
APPS := \
mean-shift \
cross-entropy
all: $(APPS)
......
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <math.h>
#define SHOW_STEPS 1
using namespace std;
using namespace cv;
static RNG rng (time (NULL));
#define SMOOTH 1
#define MIN_SIGMA 1
#define SHOOW_STEPS 1
static void
cross_entropy (Mat const &res,
int no_samples,
int no_elites,
Scalar &mean,
Scalar &sigma)
{
#if SHOW_STEPS
double m;
minMaxLoc (res, NULL, &m);
Mat out;
res.convertTo (out, CV_8U, 255 / m);
cvtColor (out, out, COLOR_GRAY2BGR);
#endif /* SHOW_STEPS */
Mat rnd = Mat::zeros (1, no_samples, CV_16SC2);
Mat vals = Mat::zeros (1, no_samples, CV_32F);
Vec2s *ptr_p = rnd.ptr<Vec2s> ();
float *ptr_v = vals.ptr<float> ();
Mat order;
Vec2i accum (0, 0);
float old_val, new_val;
new_val = res.at<float> (mean[0], mean[1]);
do
{
old_val = new_val;
ellipse (out, Point (mean[0], mean[1]), Point (sigma[0], sigma[1]),
0, 0, 360, Scalar (0, 0, 255));
rng.fill (rnd, RNG::NORMAL, mean, sigma);
for (int i = 0; i < no_samples; i++)
ptr_v[i] = res.at<float> (ptr_p[i][0], ptr_p[i][1]);
sortIdx (vals, order, CV_SORT_EVERY_ROW | CV_SORT_DESCENDING);
int *ptr_o = order.ptr<int> ();
accum = Vec2i (0, 0);
for (int i = 0; i < no_elites; i++)
accum += rnd.at<Vec2s> (0, ptr_o[i]);
#if SMOOTH
mean = (Scalar (accum[0] / no_elites, accum[1] / no_elites) + mean) / 2;
#else
mean = Scalar (accum[0] / no_elites, accum[1] / no_elites);
#endif /* SMOOTH */
new_val = res.at<float> (mean[0], mean[1]);
float diff = fabs (new_val - old_val) / fabs (new_val + old_val);
cout << diff << endl;
if (diff < 0.02)
break;
accum = Vec2i (0, 0);
for (int i = 0; i < no_elites; i++)
{
Vec2i val = rnd.at<Vec2s> (0, ptr_o[i]) - Vec2s (mean[0], mean[1]);
accum += val.mul(val);
}
sigma = Scalar (sqrt (accum[0] / (float)no_elites),
sqrt (accum[1] / (float)no_elites));
#if MIN_SIGMA
sigma = Scalar (max (sigma[0], 3.0), max (sigma[1], 2.0));
#endif /* MIN_SIGMA */
}
while (true);
#ifdef SHOW_STEPS
char fname[200];
snprintf (fname, 200, "img-%08d.png", time (NULL) % (long)1e8);
imwrite (fname, out);
resize (out, out, Size (), 5, 5, INTER_CUBIC);
imshow ("test", out);
waitKey (0);
#endif /* SHOW_STEPS */
}
int
main (int argc,
char **argv)
{
Size size (100, 100);
Mat res = Mat::zeros (size, CV_32F);
res.at<float> (70, 50) = 1.0f;
res.at<float> (50, 70) = 0.5f;
GaussianBlur (res, res, Size (), 10);
int n = strtol (argv[1], NULL, 10);
int e = strtol (argv[2], NULL, 10);
Point p (strtol (argv[3], NULL, 10),
strtol (argv[4], NULL, 10));
Scalar mean (p.x, p.y);
Scalar sigma (strtod (argv[5], NULL), strtod (argv[6], NULL));
cross_entropy (res, n, e, mean, sigma);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment