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