#include <cstdio>
#include <cstdlib>
#include <iostream>
float mx = af::max<float>(a);
float mn = af::min<float>(a);
return (a - mn) / (mx - mn);
}
void drawRectangle(
array& out,
unsigned x,
unsigned y,
unsigned dim0,
unsigned dim1) {
printf("\nMatching patch origin = (%u, %u)\n\n", x, y);
seq col_span(x, x + dim0, 1);
seq row_span(y, y + dim1, 1);
out(col_span, y, 0) = 0.f;
out(col_span, y, 1) = 0.f;
out(col_span, y, 2) = 1.f;
out(col_span, y + dim1, 0) = 0.f;
out(col_span, y + dim1, 1) = 0.f;
out(col_span, y + dim1, 2) = 1.f;
out(x, row_span, 0) = 0.f;
out(x, row_span, 1) = 0.f;
out(x, row_span, 2) = 1.f;
out(x + dim0, row_span, 0) = 0.f;
out(x + dim0, row_span, 1) = 0.f;
out(x + dim0, row_span, 2) = 1.f;
}
static void templateMatchingDemo(bool console) {
if (console)
img_color =
loadImage(ASSETS_DIR
"/examples/images/square.png",
true);
else
img_color =
loadImage(ASSETS_DIR
"/examples/images/man.jpg",
true);
std::cout << "Input image dimensions: " << iDims << std::endl << std::endl;
unsigned patch_size = 100;
img(
seq(100, 100 + patch_size, 1.0),
seq(100, 100 + patch_size, 1.0));
array disp_img = img / 255.0f;
array disp_tmp = tmp_img / 255.0f;
array disp_res = normalize(result);
unsigned minLoc;
float minVal;
min<float>(&minVal, &minLoc, disp_res);
std::cout << "Location(linear index) of minimum disparity value = "
<< minLoc << std::endl;
if (!console) {
drawRectangle(marked_res, minLoc % iDims[0], minLoc / iDims[0],
patch_size, patch_size);
std::cout << "Note: Based on the disparity metric option provided to "
"matchTemplate function\n"
"either minimum or maximum disparity location is the "
"starting corner\n"
"of our best matching patch to template image in the "
"search image"
<< std::endl;
wnd(0, 0).
image(disp_img,
"Search Image");
wnd(0, 1).
image(disp_tmp,
"Template Patch");
wnd(1, 0).
image(marked_res,
"Best Match");
wnd(1, 1).
image(disp_res,
"Disparity values");
}
}
}
int main(int argc, char** argv) {
int device = argc > 1 ? atoi(argv[1]) : 0;
bool console = argc > 2 ? argv[2][0] == '-' : false;
try {
std::cout << "** ArrayFire template matching Demo **" << std::endl
<< std::endl;
templateMatchingDemo(console);
std::cerr << ae.
what() << std::endl;
throw;
}
return 0;
}