Visual Servoing Platform  version 3.6.1 under development (2024-10-15)
testImageNormalizedCorrelation.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test vpImageTools::normalizedCorrelation().
32  */
33 
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>
48 
49 // List of allowed command line options
50 #define GETOPTARGS "cdi:h"
51 
52 #ifdef ENABLE_VISP_NAMESPACE
53 using namespace VISP_NAMESPACE_NAME;
54 #endif
55 
56 namespace
57 {
58 void usage(const char *name, const char *badparam, std::string ipath)
59 {
60  fprintf(stdout, "\n\
61  Test vpImageTools::normalizedCorrelation().\n\
62  \n\
63  SYNOPSIS\n\
64  %s [-i <VISP_IMAGES directory>] \n\
65  [-h]\n \
66  ",
67  name);
68 
69  fprintf(stdout, "\n\
70  OPTIONS: Default\n\
71  -i <VISP_IMAGES directory> %s\n\
72  Set VISP_IMAGES input path.\n\
73  Setting the VISP_INPUT_IMAGE_PATH environment\n\
74  variable produces the same behaviour than using\n\
75  this option.\n\
76  -h\n\
77  Print the help.\n\n",
78  ipath.c_str());
79 
80  if (badparam)
81  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
82 }
83 
84 bool getOptions(int argc, const char **argv, std::string &ipath)
85 {
86  const char *optarg_;
87  int c;
88  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
89 
90  switch (c) {
91  case 'i':
92  ipath = optarg_;
93  break;
94  case 'h':
95  usage(argv[0], nullptr, ipath);
96  return false;
97  break;
98 
99  case 'c':
100  case 'd':
101  break;
102 
103  default:
104  usage(argv[0], optarg_, ipath);
105  return false;
106  break;
107  }
108  }
109 
110  if ((c == 1) || (c == -1)) {
111  // standalone param or error
112  usage(argv[0], nullptr, ipath);
113  std::cerr << "ERROR: " << std::endl;
114  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
115  return false;
116  }
117 
118  return true;
119 }
120 
121 void templateMatching(const vpImage<unsigned char> &I, const vpImage<unsigned char> &I_tpl, vpImage<double> &I_score,
122  unsigned int step_u, unsigned int step_v, bool useOptimized)
123 {
124  unsigned int height_tpl = I_tpl.getHeight(), width_tpl = I_tpl.getWidth();
125  vpImage<double> I_double, I_tpl_double, I_cur;
126  vpImageConvert::convert(I, I_double);
127  vpImageConvert::convert(I_tpl, I_tpl_double);
128  I_score.resize(I.getHeight() - height_tpl, I.getWidth() - width_tpl, 0.0);
129 
130  for (unsigned int i = 0; i < I.getHeight() - height_tpl; i += step_v) {
131  for (unsigned int j = 0; j < I.getWidth() - width_tpl; j += step_u) {
132  vpRect roi(vpImagePoint(i, j), vpImagePoint(i + height_tpl - 1, j + width_tpl - 1));
133  vpImageTools::crop(I_double, roi, I_cur);
134 
135  I_score[i][j] = vpImageTools::normalizedCorrelation(I_cur, I_tpl_double, useOptimized);
136  }
137  }
138 }
139 } // namespace
140 
141 int main(int argc, const char **argv)
142 {
143  try {
144  std::string env_ipath;
145  std::string opt_ipath;
146  std::string ipath;
147  std::string filename;
148 
149 #if VISP_HAVE_DATASET_VERSION >= 0x030600
150  std::string ext("png");
151 #else
152  std::string ext("pgm");
153 #endif
154 
155  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
156  // environment variable value
157  env_ipath = vpIoTools::getViSPImagesDataPath();
158 
159  // Set the default input path
160  if (!env_ipath.empty()) {
161  ipath = env_ipath;
162  }
163 
164  // Read the command line options
165  if (!getOptions(argc, argv, opt_ipath)) {
166  exit(EXIT_FAILURE);
167  }
168 
169  // Get the option values
170  if (!opt_ipath.empty()) {
171  ipath = opt_ipath;
172  }
173 
174  // Compare ipath and env_ipath. If they differ, we take into account
175  // the input path coming from the command line option
176  if (!opt_ipath.empty() && !env_ipath.empty()) {
177  if (ipath != env_ipath) {
178  std::cout << std::endl << "WARNING: " << std::endl;
179  std::cout << " Since -i <visp image path=" << ipath << "> "
180  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
181  << " we skip the environment variable." << std::endl;
182  }
183  }
184 
185  // Test if an input path is set
186  if (opt_ipath.empty() && env_ipath.empty()) {
187  usage(argv[0], nullptr, ipath);
188  std::cerr << std::endl << "ERROR:" << std::endl;
189  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
190  << " environment variable to specify the location of the " << std::endl
191  << " image path where test images are located." << std::endl
192  << std::endl;
193  exit(EXIT_FAILURE);
194  }
195 
196  //
197  // Here starts really the test
198  //
199 
200  // Load cube sequence
201  filename = vpIoTools::createFilePath(ipath, "mbt/cube/image%04d." + ext);
202 
203  vpVideoReader reader;
204  reader.setFileName(filename);
205  vpImage<unsigned char> I, I_template;
206  reader.open(I);
207  vpRect template_roi(vpImagePoint(201, 310), vpImagePoint(201 + 152 - 1, 310 + 138 - 1));
208  vpImageTools::crop(I, template_roi, I_template);
209 
210  vpImage<double> I_score, I_score_gold;
211  const unsigned int step_i = 5, step_j = 5;
212  double t = vpTime::measureTimeMs();
213  templateMatching(I, I_template, I_score, step_i, step_j, true);
214  t = vpTime::measureTimeMs() - t;
215 
216  double t_gold = vpTime::measureTimeMs();
217  templateMatching(I, I_template, I_score_gold, step_i, step_j, false);
218  t_gold = vpTime::measureTimeMs() - t_gold;
219 
220  for (unsigned int i = 0; i < I_score.getHeight(); i++) {
221  for (unsigned int j = 0; j < I_score.getWidth(); j++) {
222  if (!vpMath::equal(I_score[i][j], I_score_gold[i][j], 1e-9)) {
223  std::cerr << "Issue with normalizedCorrelation, gold: " << std::setprecision(17) << I_score_gold[i][j]
224  << " ; compute: " << I_score[i][j] << std::endl;
225  return EXIT_FAILURE;
226  }
227  }
228  }
229 
230  vpImagePoint max_loc, max_loc_gold;
231  double max_correlation = -1.0, max_correlation_gold = -1.0;
232  I_score.getMinMaxLoc(nullptr, &max_loc, nullptr, &max_correlation);
233  I_score_gold.getMinMaxLoc(nullptr, &max_loc_gold, nullptr, &max_correlation_gold);
234 
235  std::cout << "Compare regular and SSE version of vpImageTools::normalizedCorrelation()" << std::endl;
236  std::cout << "vpImageTools::normalizedCorrelation(): " << max_correlation << " ; " << t << " ms" << std::endl;
237  std::cout << "Gold normalizedCorrelation(): " << max_correlation_gold << " ; " << t_gold << " ms" << std::endl;
238 
239  std::cerr << "\nTrue template position: " << template_roi.getTopLeft() << std::endl;
240  std::cerr << "Found template position: " << max_loc << std::endl;
241  if (vpImagePoint::distance(max_loc, template_roi.getTopLeft()) > step_i) {
242  std::cerr << "Issue with vpImageTools::normalizedCorrelation:" << std::endl;
243  return EXIT_FAILURE;
244  }
245 
246  }
247  catch (const vpException &e) {
248  std::cerr << "\nCatch an exception: " << e << std::endl;
249  return EXIT_FAILURE;
250  }
251  return EXIT_SUCCESS;
252 }
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
static void crop(const vpImage< Type > &I, double roi_top, double roi_left, unsigned int roi_height, unsigned int roi_width, vpImage< Type > &crop, unsigned int v_scale=1, unsigned int h_scale=1)
Definition: vpImageTools.h:315
static double normalizedCorrelation(const vpImage< double > &I1, const vpImage< double > &I2, bool useOptimized=true)
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
Definition: vpImage.h:242
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:542
unsigned int getHeight() const
Definition: vpImage.h:181
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
static bool equal(double x, double y, double threshold=0.001)
Definition: vpMath.h:459
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
Defines a rectangle in the plane.
Definition: vpRect.h:79
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
VISP_EXPORT double measureTimeMs()