#include <math.h>
#include <stdio.h>
#include <string.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};
array dt = constant(1, 1, 2) / 4.0;
Ix = convolve(I1, dx) + convolve(I2, dx);
Iy = convolve(I1, dy) + convolve(I2, dy);
It = convolve(I2, dt) - convolve(I1, dt);
}
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};
loadImage(ASSETS_DIR
"/examples/images/circle_left.ppm");
array I2 =
loadImage(ASSETS_DIR
"/examples/images/circle_center.ppm");
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;
max_rate = std::max(max_rate, rate);
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.
A multi dimensional data container.
dim4 dims() const
Get dimensions of the array.
Generic object that represents size and shape.
An ArrayFire exception class.
virtual const char * what() const
Returns an error message for the exception in a string format.
@ AF_COLORMAP_COLORS
Colors, aka. Rainbow.
array constant(T val, const dim4 &dims, const dtype ty=(af_dtype) dtype_traits< T >::ctype)
C++ Interface to generate an array with elements set to a specified value.
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.
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.