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

Finish this crap

parent b91aa6e4
...@@ -13,8 +13,8 @@ using namespace cv; ...@@ -13,8 +13,8 @@ using namespace cv;
static RNG rng (time (NULL)); static RNG rng (time (NULL));
#define SMOOTH 0 #define SMOOTH 1
#define MIN_SIGMA 0 #define MIN_SIGMA 1
#define SHOOW_STEPS 1 #define SHOOW_STEPS 1
......
...@@ -97,7 +97,7 @@ main (int argc, ...@@ -97,7 +97,7 @@ main (int argc,
kernel.convertTo (kernel, CV_32F); kernel.convertTo (kernel, CV_32F);
#endif #endif
Point p (60, 40); Point p (80, 30);
mean_shift (res, kernel, p); mean_shift (res, kernel, p);
cout << p << endl; cout << p << endl;
......
...@@ -18,6 +18,8 @@ using namespace cv; ...@@ -18,6 +18,8 @@ using namespace cv;
#define WIN "test" #define WIN "test"
#define HIST_BINS 16 #define HIST_BINS 16
static RNG rng (time (NULL));
typedef struct typedef struct
{ {
Point tl, br; Point tl, br;
...@@ -85,7 +87,8 @@ get_region (Mat const &img) ...@@ -85,7 +87,8 @@ get_region (Mat const &img)
int key = waitKey (); int key = waitKey ();
while (key != 1048586) //while (key != (int)'d')
while (key != 1048676)
key = waitKey (); key = waitKey ();
destroyWindow (WIN); destroyWindow (WIN);
...@@ -209,12 +212,12 @@ backproject (Mat const &img, ...@@ -209,12 +212,12 @@ backproject (Mat const &img,
static Mat static Mat
epach_kernel (Size const &size) epach_kernel (Size const &size)
{ {
/* Optimize for cca. 50 x 50 patches */ int full = size.width < size.height ? size.height : size.width;
int def_size = 51; int half = (full - 1) / 2;
int half = (def_size - 1) / 2;
int half2 = half * half; int half2 = half * half;
full = 2 * half + 1;
Mat kernel (def_size, def_size, CV_32F, Scalar::all (0)); Mat kernel (full, full, CV_32F, Scalar::all (0));
float *data = kernel.ptr<float> (); float *data = kernel.ptr<float> ();
int i = 0; int i = 0;
...@@ -223,38 +226,71 @@ epach_kernel (Size const &size) ...@@ -223,38 +226,71 @@ epach_kernel (Size const &size)
data[i++] = max (half2 - row * row - col * col, 0); data[i++] = max (half2 - row * row - col * col, 0);
/* Scale kernel to required size */ /* Scale kernel to required size */
resize (kernel, kernel, size, 0, 0, INTER_AREA); resize (kernel, kernel, size);
return kernel / sum (kernel)[0]; return kernel / sum (kernel)[0];
} }
typedef struct typedef struct
{ {
Mat kernel; Mat kernel; /* Histogram kernel */
Mat hist; Mat hist; /* Template histogram */
Rect region; Rect region; /* Current region */
Scalar mean; /* Mean value */
Scalar sigma; /* Standard deviation */
} }
State; State;
static Rect static Rect
tracker_mean_shift_init (Mat const &img, trackers_init (Mat const &img,
Rect const &region, Rect const &region,
void **d) void **d)
{ {
static State data; static State data = {Mat (), Mat (), region, Scalar (), Scalar ()};
Mat kernel = epach_kernel (region.size ()); Mat tmp;
Mat hist = histogram (img (region), kernel, HIST_BINS);
data.kernel = kernel; data.kernel = epach_kernel (region.size ());
data.hist = hist; data.hist = histogram (img (region), data.kernel, HIST_BINS);
data.region = region; data.mean = Scalar (region.y, region.x);
data.sigma = Scalar (region.height / 10, region.width / 10);
*d = (void *)&data; *d = (void *)&data;
return region; return region;
} }
static Rect
tracker_mean_shift_simple_update (Mat const &img,
void *d)
{
State *data = (State *)d;
Rect region = data->region;
Point center = (region.tl () + region.br ()) / 2;
Mat hist, w, bp, xx, yy;
bp = backproject (img, data->hist, HIST_BINS);
while (true)
{
meshgrid (region, xx, yy);
xx = xx.mul (bp (region));
yy = yy.mul (bp (region));
float den = sum (bp (region))[0];
Point tmp (sum (xx)[0] / den, sum (yy)[0] / den);
if (tmp == center)
break;
region += tmp - center;
center = tmp;
}
data->region = region;
return region;
}
static Rect static Rect
tracker_mean_shift_update (Mat &img, tracker_mean_shift_update (Mat &img,
void *d) void *d)
...@@ -267,10 +303,11 @@ tracker_mean_shift_update (Mat &img, ...@@ -267,10 +303,11 @@ tracker_mean_shift_update (Mat &img,
Mat hist, w, bp, xx, yy; Mat hist, w, bp, xx, yy;
int i = 0; int i = 0;
while (true) while (i < 20)
{ {
hist = histogram (img (region), data->kernel, HIST_BINS); hist = histogram (img (region), data->kernel, HIST_BINS);
sqrt (hist / (data->hist + 1e-10), w); w = data->hist / (hist + 1e-10);
sqrt (w, w);
bp = backproject (img (region), w, HIST_BINS); bp = backproject (img (region), w, HIST_BINS);
meshgrid (region, xx, yy); meshgrid (region, xx, yy);
xx = xx.mul (bp); xx = xx.mul (bp);
...@@ -279,20 +316,82 @@ tracker_mean_shift_update (Mat &img, ...@@ -279,20 +316,82 @@ tracker_mean_shift_update (Mat &img,
Point tmp (sum (xx)[0] / den, sum (yy)[0] / den); Point tmp (sum (xx)[0] / den, sum (yy)[0] / den);
if (tmp == center) if (tmp == center)
break; break;
region += tmp - center;
center = tmp;
i++;
}
double m; data->region = region;
minMaxLoc (bp, NULL, &m); return region;
imshow ("test", bp); }
waitKey ();
static Rect
tracker_cc_update (Mat &img,
void *d)
{
State *data = (State *)d;
region += tmp - center; Rect region = data->region;
cout << tmp - center << endl; Mat hist;
cout << region << endl;
center = tmp; int no_samples = 40;
int no_elites = 10;
Scalar mean = data->mean;
Scalar sigma = data->sigma;
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;
hist = histogram (img (region), data->kernel, HIST_BINS);
new_val = compareHist (data->hist, hist, HISTCMP_HELLINGER);
do
{
old_val = new_val;
rng.fill (rnd, RNG::NORMAL, mean, sigma);
for (int i = 0; i < no_samples; i++)
{
Rect area (Point (ptr_p[i][1], ptr_p[i][0]), region.size ());
hist = histogram (img (area), data->kernel, HIST_BINS);
ptr_v[i] = compareHist (data->hist, hist, HISTCMP_HELLINGER);
}
sortIdx (vals, order, CV_SORT_EVERY_ROW | CV_SORT_ASCENDING);
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]);
mean = (Scalar (accum[0] / no_elites, accum[1] / no_elites) + mean) / 2;
region = Rect (Point (round (mean[1]), round (mean[0])), region.size ());
hist = histogram (img (region), data->kernel, HIST_BINS);
new_val = compareHist (data->hist, hist, HISTCMP_HELLINGER);
float diff = fabs (new_val - old_val) / fabs (new_val + old_val);
if (diff < 0.002)
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));
sigma = Scalar (max (sigma[0], 3.0), max (sigma[1], 3.0));
} }
while (true);
data->region = region; data->region = region;
data->mean = mean;
data->sigma = sigma;
return region; return region;
} }
...@@ -314,14 +413,16 @@ main (int argc, ...@@ -314,14 +413,16 @@ main (int argc,
strtol (argv[5], NULL, 10)); strtol (argv[5], NULL, 10));
void *state = NULL; void *state = NULL;
Rect loc = tracker_mean_shift_init (img, init, &state); Rect loc = trackers_init (img, init, &state);
cout << loc << endl; cout << loc << endl;
//for (int i = 1; i < seq.size (); i++) for (unsigned int i = 1; i < seq.size (); i++)
for (int i = 1; i < 2; i++)
{ {
cout << seq[i] << endl;
img = imread (seq[i]); img = imread (seq[i]);
loc = tracker_mean_shift_update (img, state); //loc = tracker_mean_shift_simple_update (img, state);
//loc = tracker_mean_shift_update (img, state);
loc = tracker_cc_update (img, state);
update_display (img, loc);
waitKey (10);
} }
return 0; 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