#include <string.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
{
float dx_kernel[] = { -1.0f / 6.0f, -1.0f / 6.0f, -1.0f / 6.0f,
0.0f / 6.0f, 0.0f / 6.0f, 0.0f / 6.0f,
1.0f / 6.0f, 1.0f / 6.0f, 1.0f / 6.0f
};
float dy_kernel[] = { -1.0f / 6.0f, 0.0f / 6.0f, 1.0f / 6.0f,
-1.0f / 6.0f, 0.0f / 6.0f, 1.0f / 6.0f,
-1.0f / 6.0f, 0.0f / 6.0f, 1.0f / 6.0f
};
}
static void optical_flow_demo(bool console)
{
double time_total = 10;
const float h_mean_kernel[] = {1.0f / 12.0f, 2.0f / 12.0f, 1.0f / 12.0f,
2.0f / 12.0f, 0.0f, 2.0f / 12.0f,
1.0f / 12.0f, 2.0f / 12.0f, 1.1f / 12.0f
};
array I2 =
loadImage(ASSETS_DIR
"/examples/images/circle_center.ppm");
array Ix, Iy, It; diffs(Ix, Iy, It, I1, I2);
timer time_start, time_last;
time_start = time_last = timer::start();
int iter = 0, iter_last = 0;
double max_rate = 0;
while (true) {
iter++;
const float alphasq = 0.1f;
array num = Ix * u_ + Iy * v_ + It;
array den = alphasq + Ix * Ix + Iy * Iy;
u = u_ - (Ix * tmp) / den;
v = v_ - (Iy * tmp) / den;
if (!console) {
wnd.grid(2,2);
wnd(0, 0).image(I1, "I1");
wnd(1, 0).image(I2, "I2");
wnd(0, 1).image(u, "u");
wnd(1, 1).image(v, "v");
wnd.show();
}
double elapsed = timer::stop(time_last);
if (elapsed > 1) {
double rate = (iter - iter_last) / elapsed;
double total_elapsed = timer::stop(time_start);
time_last = timer::start();
iter_last = iter;
if (total_elapsed >= time_total) {
break;
}
if (!console)
printf(" iterations per second: %.0f (progress %.0f%%)\n",
rate, 100.0f * total_elapsed / time_total);
}
}
if (console) {
printf(" ### optical_flow %f iterations per second (max)\n", max_rate);
}
}
int main(int argc, char* argv[])
{
int device = argc > 1 ? atoi(argv[1]) : 0;
bool console = argc > 2 ? argv[2][0] == '-' : false;
try {
printf("Horn-Schunck optical flow\n");
optical_flow_demo(console);
fprintf(stderr,
"%s\n", e.
what());
throw;
}
return 0;
}
Window object to render af::arrays.
Definition: graphics.h:37
A multi dimensional data container.
Definition: array.h:27
Definition: exception.h:20
virtual const char * what() const
Definition: exception.h:34
@ AF_COLORMAP_COLORS
Colors.
Definition: defines.h:335
@ afHost
Host pointer.
Definition: defines.h:216
array constant(T val, const dim4 &dims, const dtype ty=(af_dtype) dtype_traits< T >::ctype)
AFAPI void setDevice(const int device)
Sets the current device.
AFAPI array loadImage(const char *filename, const bool is_color=false)
C++ Interface for loading an image.
dim4 dims() const
Get dimensions of the array.
AFAPI array max(const array &in, const int dim=-1)
C++ Interface for maximum values in an array.
AFAPI array convolve(const array &signal, const array &filter, const convMode mode=AF_CONV_DEFAULT, const convDomain domain=AF_CONV_AUTO)
C++ Interface for convolution any(one through three) dimensional signals.
Definition: algorithm.h:15
static af::array array(af::dim4 idims, cl_mem buf, af::dtype type, bool retain=false)
Create an af::array object from an OpenCL cl_mem buffer.
Definition: opencl.h:329
Internal timer object.
Definition: timing.h:29