Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
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
39
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
53using namespace VISP_NAMESPACE_NAME;
54#endif
55
56namespace
57{
58void 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
84bool 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
98 case 'c':
99 case 'd':
100 break;
101
102 default:
103 usage(argv[0], optarg_, ipath);
104 return false;
105 }
106 }
107
108 if ((c == 1) || (c == -1)) {
109 // standalone param or error
110 usage(argv[0], nullptr, ipath);
111 std::cerr << "ERROR: " << std::endl;
112 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
113 return false;
114 }
115
116 return true;
117}
118
119void templateMatching(const vpImage<unsigned char> &I, const vpImage<unsigned char> &I_tpl, vpImage<double> &I_score,
120 unsigned int step_u, unsigned int step_v, bool useOptimized)
121{
122 unsigned int height_tpl = I_tpl.getHeight(), width_tpl = I_tpl.getWidth();
123 vpImage<double> I_double, I_tpl_double, I_cur;
124 vpImageConvert::convert(I, I_double);
125 vpImageConvert::convert(I_tpl, I_tpl_double);
126 I_score.resize(I.getHeight() - height_tpl, I.getWidth() - width_tpl, 0.0);
127
128 for (unsigned int i = 0; i < I.getHeight() - height_tpl; i += step_v) {
129 for (unsigned int j = 0; j < I.getWidth() - width_tpl; j += step_u) {
130 vpRect roi(vpImagePoint(i, j), vpImagePoint(i + height_tpl - 1, j + width_tpl - 1));
131 vpImageTools::crop(I_double, roi, I_cur);
132
133 I_score[i][j] = vpImageTools::normalizedCorrelation(I_cur, I_tpl_double, useOptimized);
134 }
135 }
136}
137} // namespace
138
139int main(int argc, const char **argv)
140{
141 try {
142 std::string env_ipath;
143 std::string opt_ipath;
144 std::string ipath;
145 std::string filename;
146
147#if defined(VISP_HAVE_DATASET)
148#if VISP_HAVE_DATASET_VERSION >= 0x030600
149 std::string ext("png");
150#else
151 std::string ext("pgm");
152#endif
153#else
154 // We suppose that the user will download a recent dataset
155 std::string ext("png");
156#endif
157
158 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
159 // environment variable value
161
162 // Set the default input path
163 if (!env_ipath.empty()) {
164 ipath = env_ipath;
165 }
166
167 // Read the command line options
168 if (!getOptions(argc, argv, opt_ipath)) {
169 exit(EXIT_FAILURE);
170 }
171
172 // Get the option values
173 if (!opt_ipath.empty()) {
174 ipath = opt_ipath;
175 }
176
177 // Compare ipath and env_ipath. If they differ, we take into account
178 // the input path coming from the command line option
179 if (!opt_ipath.empty() && !env_ipath.empty()) {
180 if (ipath != env_ipath) {
181 std::cout << std::endl << "WARNING: " << std::endl;
182 std::cout << " Since -i <visp image path=" << ipath << "> "
183 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
184 << " we skip the environment variable." << std::endl;
185 }
186 }
187
188 // Test if an input path is set
189 if (opt_ipath.empty() && env_ipath.empty()) {
190 usage(argv[0], nullptr, ipath);
191 std::cerr << std::endl << "ERROR:" << std::endl;
192 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
193 << " environment variable to specify the location of the " << std::endl
194 << " image path where test images are located." << std::endl
195 << std::endl;
196 exit(EXIT_FAILURE);
197 }
198
199 //
200 // Here starts really the test
201 //
202
203 // Load cube sequence
204 filename = vpIoTools::createFilePath(ipath, "mbt/cube/image%04d." + ext);
205
206 vpVideoReader reader;
207 reader.setFileName(filename);
208 vpImage<unsigned char> I, I_template;
209 reader.open(I);
210 vpRect template_roi(vpImagePoint(201, 310), vpImagePoint(201 + 152 - 1, 310 + 138 - 1));
211 vpImageTools::crop(I, template_roi, I_template);
212
213 vpImage<double> I_score, I_score_gold;
214 const unsigned int step_i = 5, step_j = 5;
215 double t = vpTime::measureTimeMs();
216 templateMatching(I, I_template, I_score, step_i, step_j, true);
218
219 double t_gold = vpTime::measureTimeMs();
220 templateMatching(I, I_template, I_score_gold, step_i, step_j, false);
221 t_gold = vpTime::measureTimeMs() - t_gold;
222
223 for (unsigned int i = 0; i < I_score.getHeight(); i++) {
224 for (unsigned int j = 0; j < I_score.getWidth(); j++) {
225 if (!vpMath::equal(I_score[i][j], I_score_gold[i][j], 1e-9)) {
226 std::cerr << "Issue with normalizedCorrelation, gold: " << std::setprecision(17) << I_score_gold[i][j]
227 << " ; compute: " << I_score[i][j] << std::endl;
228 return EXIT_FAILURE;
229 }
230 }
231 }
232
233 vpImagePoint max_loc, max_loc_gold;
234 double max_correlation = -1.0, max_correlation_gold = -1.0;
235 I_score.getMinMaxLoc(nullptr, &max_loc, nullptr, &max_correlation);
236 I_score_gold.getMinMaxLoc(nullptr, &max_loc_gold, nullptr, &max_correlation_gold);
237
238 std::cout << "Compare regular and SSE version of vpImageTools::normalizedCorrelation()" << std::endl;
239 std::cout << "vpImageTools::normalizedCorrelation(): " << max_correlation << " ; " << t << " ms" << std::endl;
240 std::cout << "Gold normalizedCorrelation(): " << max_correlation_gold << " ; " << t_gold << " ms" << std::endl;
241
242 std::cerr << "\nTrue template position: " << template_roi.getTopLeft() << std::endl;
243 std::cerr << "Found template position: " << max_loc << std::endl;
244 if (vpImagePoint::distance(max_loc, template_roi.getTopLeft()) > step_i) {
245 std::cerr << "Issue with vpImageTools::normalizedCorrelation:" << std::endl;
246 return EXIT_FAILURE;
247 }
248
249 }
250 catch (const vpException &e) {
251 std::cerr << "\nCatch an exception: " << e << std::endl;
252 return EXIT_FAILURE;
253 }
254 return EXIT_SUCCESS;
255}
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 ...
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)
static double normalizedCorrelation(const vpImage< double > &I1, const vpImage< double > &I2, bool useOptimized=true)
Definition of the vpImage class member functions.
Definition vpImage.h:131
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:544
unsigned int getHeight() const
Definition vpImage.h:181
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)
static bool equal(double x, double y, double threshold=0.001)
Definition vpMath.h:470
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
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) VP_OVERRIDE
void setFileName(const std::string &filename)
VISP_EXPORT double measureTimeMs()