41 #include <visp3/core/vpImage.h>
42 #include <visp3/core/vpImageTools.h>
43 #include <visp3/core/vpIoTools.h>
44 #include <visp3/gui/vpDisplayGDI.h>
45 #include <visp3/gui/vpDisplayOpenCV.h>
46 #include <visp3/gui/vpDisplayX.h>
47 #include <visp3/io/vpParseArgv.h>
48 #include <visp3/io/vpVideoReader.h>
51 #define GETOPTARGS "cdi:th"
55 void usage(
const char *name,
const char *badparam, std::string ipath)
58 Test vpImageTools::templateMatching().\n\
61 %s [-i <VISP_IMAGES directory>] \n\
69 -i <VISP_IMAGES directory> %s\n\
70 Set VISP_IMAGES input path.\n\
71 Setting the VISP_INPUT_IMAGE_PATH environment\n\
72 variable produces the same behaviour than using\n\
78 Perform template matching on cube sequence.\n\
84 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
87 bool getOptions(
int argc,
const char **argv, std::string &ipath,
bool &click,
bool &doTemplateMatching)
98 usage(argv[0],
nullptr, ipath);
102 doTemplateMatching =
true;
112 usage(argv[0], optarg_, ipath);
118 if ((c == 1) || (c == -1)) {
120 usage(argv[0],
nullptr, ipath);
121 std::cerr <<
"ERROR: " << std::endl;
122 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
130 int main(
int argc,
const char **argv)
132 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGPROC)
134 const int h = 5, w = 5;
164 std::cout <<
"I:\n" << I << std::endl;
165 std::cout <<
"II:\n" << II << std::endl;
166 std::cout <<
"IIsq:\n" << IIsq << std::endl;
168 cv::Mat mat(h, w, CV_64F);
169 for (
int i = 0; i < h; i++) {
170 for (
int j = 0; j < w; j++) {
171 mat.at<
double>(i, j) = I[i][j];
176 cv::integral(mat, sum, sqsum);
177 std::cout <<
"mat:\n" << mat << std::endl;
178 std::cout <<
"sum:\n" << sum << std::endl;
179 std::cout <<
"sqsum:\n" << sqsum << std::endl;
181 for (
int i = 0; i < h; i++) {
182 for (
int j = 0; j < w; j++) {
183 if (!
vpMath::equal(II[i][j], sum.at<
double>(i, j), std::numeric_limits<double>::epsilon())) {
184 std::cerr <<
"Error vpImageTools::integralImage(II), reference: " << std::setprecision(17)
185 << sum.at<
double>(i, j) <<
" ; compute: " << II[i][j] << std::endl;
189 if (!
vpMath::equal(IIsq[i][j], sqsum.at<
double>(i, j), std::numeric_limits<double>::epsilon())) {
190 std::cerr <<
"Error vpImageTools::integralImage(IIsq), reference: " << std::setprecision(17)
191 << sqsum.at<
double>(i, j) <<
" ; compute: " << IIsq[i][j] << std::endl;
200 std::string env_ipath;
201 std::string opt_ipath;
203 std::string filename;
205 bool doTemplateMatching =
false;
207 #if VISP_HAVE_DATASET_VERSION >= 0x030600
208 std::string ext(
"png");
210 std::string ext(
"pgm");
218 if (!env_ipath.empty()) {
223 if (!getOptions(argc, argv, opt_ipath, click, doTemplateMatching)) {
228 if (!opt_ipath.empty()) {
234 if (!opt_ipath.empty() && !env_ipath.empty()) {
235 if (ipath != env_ipath) {
236 std::cout << std::endl <<
"WARNING: " << std::endl;
237 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
238 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
239 <<
" we skip the environment variable." << std::endl;
244 if (opt_ipath.empty() && env_ipath.empty()) {
245 usage(argv[0],
nullptr, ipath);
246 std::cerr << std::endl <<
"ERROR:" << std::endl;
247 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
248 <<
" environment variable to specify the location of the " << std::endl
249 <<
" image path where test images are located." << std::endl
268 if (doTemplateMatching) {
269 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
271 #if defined(VISP_HAVE_X11)
273 #elif defined(VISP_HAVE_GDI)
275 #elif defined(HAVE_OPENCV_HIGHGUI)
279 d.
init(I, 0, 0,
"Image");
282 std::vector<double> benchmark_vec;
284 while (!reader.
end() && !quit) {
289 std::stringstream ss;
295 const unsigned int step_u = 5, step_v = 5;
299 double max_correlation = -1.0;
300 I_score.
getMinMaxLoc(
nullptr, &max_loc,
nullptr, &max_correlation);
302 benchmark_vec.push_back(t_proc);
305 ss <<
"Template matching: " << t_proc <<
" ms";
309 ss <<
"Max correlation: " << max_correlation;
333 if (!benchmark_vec.empty()) {
334 std::cout <<
"Processing time, Mean: " <<
vpMath::getMean(benchmark_vec)
342 const unsigned int step_u = 5, step_v = 5;
353 std::cout <<
"Template matching: " << t <<
" ms" << std::endl;
354 std::cout <<
"Template matching (gold): " << t_gold <<
" ms" << std::endl;
356 for (
unsigned int i = 0; i < I_score.
getHeight(); i++) {
357 for (
unsigned int j = 0; j < I_score.
getWidth(); j++) {
358 if (!
vpMath::equal(I_score[i][j], I_score_gold[i][j], 1e-9)) {
359 std::cerr <<
"Issue with template matching, gold: " << std::setprecision(17) << I_score_gold[i][j]
360 <<
" ; compute: " << I_score[i][j] << std::endl;
368 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...
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="") vp_override
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 ...
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 acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
long getFrameIndex() const
VISP_EXPORT double measureTimeMs()