40#include <visp3/core/vpImage.h>
41#include <visp3/core/vpImageTools.h>
42#include <visp3/core/vpIoTools.h>
43#include <visp3/gui/vpDisplayGDI.h>
44#include <visp3/gui/vpDisplayOpenCV.h>
45#include <visp3/gui/vpDisplayX.h>
46#include <visp3/io/vpParseArgv.h>
47#include <visp3/io/vpVideoReader.h>
50#define GETOPTARGS "cdi:th"
52#ifdef ENABLE_VISP_NAMESPACE
58void usage(
const char *name,
const char *badparam, std::string ipath)
61 Test vpImageTools::templateMatching().\n\
64 %s [-i <VISP_IMAGES directory>] \n\
72 -i <VISP_IMAGES directory> %s\n\
73 Set VISP_IMAGES input path.\n\
74 Setting the VISP_INPUT_IMAGE_PATH environment\n\
75 variable produces the same behaviour than using\n\
81 Perform template matching on cube sequence.\n\
87 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
90bool getOptions(
int argc,
const char **argv, std::string &ipath,
bool &click,
bool &doTemplateMatching)
101 usage(argv[0],
nullptr, ipath);
104 doTemplateMatching =
true;
114 usage(argv[0], optarg_, ipath);
119 if ((c == 1) || (c == -1)) {
121 usage(argv[0],
nullptr, ipath);
122 std::cerr <<
"ERROR: " << std::endl;
123 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
131int main(
int argc,
const char **argv)
133#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGPROC)
135 const int h = 5,
w = 5;
165 std::cout <<
"I:\n" << I << std::endl;
166 std::cout <<
"II:\n" << II << std::endl;
167 std::cout <<
"IIsq:\n" << IIsq << std::endl;
169 cv::Mat mat(h, w, CV_64F);
170 for (
int i = 0;
i <
h;
i++) {
171 for (
int j = 0;
j <
w;
j++) {
172 mat.at<
double>(
i,
j) = I[i][j];
177 cv::integral(mat, sum, sqsum);
178 std::cout <<
"mat:\n" << mat << std::endl;
179 std::cout <<
"sum:\n" << sum << std::endl;
180 std::cout <<
"sqsum:\n" << sqsum << std::endl;
182 for (
int i = 0;
i <
h;
i++) {
183 for (
int j = 0;
j <
w;
j++) {
184 if (!
vpMath::equal(II[i][j], sum.at<
double>(i, j), std::numeric_limits<double>::epsilon())) {
185 std::cerr <<
"Error vpImageTools::integralImage(II), reference: " << std::setprecision(17)
186 << sum.at<
double>(
i,
j) <<
" ; compute: " << II[i][j] << std::endl;
190 if (!
vpMath::equal(IIsq[i][j], sqsum.at<
double>(i, j), std::numeric_limits<double>::epsilon())) {
191 std::cerr <<
"Error vpImageTools::integralImage(IIsq), reference: " << std::setprecision(17)
192 << sqsum.at<
double>(
i,
j) <<
" ; compute: " << IIsq[i][j] << std::endl;
201 std::string env_ipath;
202 std::string opt_ipath;
206 bool doTemplateMatching =
false;
208#if defined(VISP_HAVE_DATASET)
209#if VISP_HAVE_DATASET_VERSION >= 0x030600
210 std::string ext(
"png");
212 std::string ext(
"pgm");
216 std::string ext(
"png");
224 if (!env_ipath.empty()) {
229 if (!getOptions(argc, argv, opt_ipath, click, doTemplateMatching)) {
234 if (!opt_ipath.empty()) {
240 if (!opt_ipath.empty() && !env_ipath.empty()) {
241 if (ipath != env_ipath) {
242 std::cout << std::endl <<
"WARNING: " << std::endl;
243 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
244 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
245 <<
" we skip the environment variable." << std::endl;
250 if (opt_ipath.empty() && env_ipath.empty()) {
251 usage(argv[0],
nullptr, ipath);
252 std::cerr << std::endl <<
"ERROR:" << std::endl;
253 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
254 <<
" environment variable to specify the location of the " << std::endl
255 <<
" image path where test images are located." << std::endl
274 if (doTemplateMatching) {
275#if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
277#if defined(VISP_HAVE_X11)
279#elif defined(VISP_HAVE_GDI)
281#elif defined(HAVE_OPENCV_HIGHGUI)
285 d.init(I, 0, 0,
"Image");
288 std::vector<double> benchmark_vec;
290 while (!reader.
end() && !quit) {
295 std::stringstream ss;
301 const unsigned int step_u = 5, step_v = 5;
305 double max_correlation = -1.0;
306 I_score.
getMinMaxLoc(
nullptr, &max_loc,
nullptr, &max_correlation);
308 benchmark_vec.push_back(t_proc);
311 ss <<
"Template matching: " << t_proc <<
" ms";
315 ss <<
"Max correlation: " << max_correlation;
339 if (!benchmark_vec.empty()) {
340 std::cout <<
"Processing time, Mean: " <<
vpMath::getMean(benchmark_vec)
349 const unsigned int step_u = 5, step_v = 5;
360 std::cout <<
"Template matching: " <<
t <<
" ms" << std::endl;
361 std::cout <<
"Template matching (gold): " << t_gold <<
" ms" << std::endl;
363 for (
unsigned int i = 0;
i < I_score.
getHeight();
i++) {
364 for (
unsigned int j = 0;
j < I_score.
getWidth();
j++) {
365 if (!
vpMath::equal(I_score[i][j], I_score_gold[i][j], 1e-9)) {
366 std::cerr <<
"Issue with template matching, gold: " << std::setprecision(17) << I_score_gold[
i][
j]
367 <<
" ; compute: " << I_score[
i][
j] << std::endl;
376 std::cerr <<
"\nCatch an exception: " <<
e << std::endl;
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
void getMinMaxLoc(vpImagePoint *minLoc, vpImagePoint *maxLoc, Type *minVal=nullptr, Type *maxVal=nullptr) const
Get the position of the minimum and/or the maximum pixel value within the bitmap and the correspondin...
unsigned int getWidth() const
unsigned int getHeight() const
static double getMedian(const std::vector< double > &v)
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
static bool equal(double x, double y, double threshold=0.001)
static double getMean(const std::vector< double > &v)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Defines a rectangle in the plane.
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void open(vpImage< vpRGBa > &I) VP_OVERRIDE
void setFileName(const std::string &filename)
long getFrameIndex() const
void acquire(vpImage< vpRGBa > &I) VP_OVERRIDE
VISP_EXPORT double measureTimeMs()