Visual Servoing Platform version 3.5.0
testImageNormalizedCorrelation.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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 http://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/vpIoTools.h>
43#include <visp3/core/vpImageTools.h>
44#include <visp3/io/vpVideoReader.h>
45#include <visp3/io/vpParseArgv.h>
46#include <visp3/gui/vpDisplayX.h>
47#include <visp3/gui/vpDisplayGDI.h>
48#include <visp3/gui/vpDisplayOpenCV.h>
49
50// List of allowed command line options
51#define GETOPTARGS "cdi:h"
52
53namespace
54{
55 void 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 ", name);
64
65 fprintf(stdout, "\n\
66 OPTIONS: Default\n\
67 -i <VISP_IMAGES directory> %s\n\
68 Set VISP_IMAGES input path.\n\
69 Setting the VISP_INPUT_IMAGE_PATH environment\n\
70 variable produces the same behaviour than using\n\
71 this option.\n\
72 -h\n\
73 Print the help.\n\n", ipath.c_str());
74
75 if (badparam)
76 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
77 }
78
79 bool getOptions(int argc, const char **argv, std::string &ipath)
80 {
81 const char *optarg_;
82 int c;
83 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
84
85 switch (c) {
86 case 'i':
87 ipath = optarg_;
88 break;
89 case 'h':
90 usage(argv[0], NULL, ipath);
91 return false;
92 break;
93
94 case 'c':
95 case 'd':
96 break;
97
98 default:
99 usage(argv[0], optarg_, ipath);
100 return false;
101 break;
102 }
103 }
104
105 if ((c == 1) || (c == -1)) {
106 // standalone param or error
107 usage(argv[0], NULL, ipath);
108 std::cerr << "ERROR: " << std::endl;
109 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
110 return false;
111 }
112
113 return true;
114 }
115
116 void templateMatching(const vpImage<unsigned char> &I, const vpImage<unsigned char> &I_tpl,
117 vpImage<double> &I_score, unsigned int step_u,
118 unsigned int step_v, bool useOptimized)
119 {
120 unsigned int height_tpl = I_tpl.getHeight(), width_tpl = I_tpl.getWidth();
121 vpImage<double> I_double, I_tpl_double, I_cur;
122 vpImageConvert::convert(I, I_double);
123 vpImageConvert::convert(I_tpl, I_tpl_double);
124 I_score.resize(I.getHeight() - height_tpl, I.getWidth() - width_tpl, 0.0);
125
126 for (unsigned int i = 0; i < I.getHeight()-height_tpl; i += step_v) {
127 for (unsigned int j = 0; j < I.getWidth()-width_tpl; j += step_u) {
128 vpRect roi( vpImagePoint(i, j), vpImagePoint(i+height_tpl-1, j+width_tpl-1) );
129 vpImageTools::crop(I_double, roi, I_cur);
130
131 I_score[i][j] = vpImageTools::normalizedCorrelation(I_cur, I_tpl_double, useOptimized);
132 }
133 }
134 }
135}
136
137int main(int argc, const char **argv)
138{
139 try {
140 std::string env_ipath;
141 std::string opt_ipath;
142 std::string ipath;
143 std::string filename;
144
145 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
146 // environment variable value
148
149 // Set the default input path
150 if (!env_ipath.empty()) {
151 ipath = env_ipath;
152 }
153
154 // Read the command line options
155 if (!getOptions(argc, argv, opt_ipath)) {
156 exit(EXIT_FAILURE);
157 }
158
159 // Get the option values
160 if (!opt_ipath.empty()) {
161 ipath = opt_ipath;
162 }
163
164 // Compare ipath and env_ipath. If they differ, we take into account
165 // the input path comming from the command line option
166 if (!opt_ipath.empty() && !env_ipath.empty()) {
167 if (ipath != env_ipath) {
168 std::cout << std::endl << "WARNING: " << std::endl;
169 std::cout << " Since -i <visp image path=" << ipath << "> "
170 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
171 << " we skip the environment variable." << std::endl;
172 }
173 }
174
175 // Test if an input path is set
176 if (opt_ipath.empty() && env_ipath.empty()) {
177 usage(argv[0], NULL, ipath);
178 std::cerr << std::endl << "ERROR:" << std::endl;
179 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
180 << " environment variable to specify the location of the " << std::endl
181 << " image path where test images are located." << std::endl
182 << std::endl;
183 exit(EXIT_FAILURE);
184 }
185
186 //
187 // Here starts really the test
188 //
189
190 // Load cube sequence
191 filename = vpIoTools::createFilePath(ipath, "mbt/cube/image%04d.pgm");
192
193 vpVideoReader reader;
194 reader.setFileName(filename);
195 vpImage<unsigned char> I, I_template;
196 reader.open(I);
197 vpRect template_roi( vpImagePoint(201, 310), vpImagePoint(201+152-1, 310+138-1) );
198 vpImageTools::crop(I, template_roi, I_template);
199
200 vpImage<double> I_score, I_score_gold;
201 const unsigned int step_i = 5, step_j = 5;
202 double t = vpTime::measureTimeMs();
203 templateMatching(I, I_template, I_score, step_i, step_j, true);
204 t = vpTime::measureTimeMs() - t;
205
206 double t_gold = vpTime::measureTimeMs();
207 templateMatching(I, I_template, I_score_gold, step_i, step_j, false);
208 t_gold = vpTime::measureTimeMs() - t_gold;
209
210 for (unsigned int i = 0; i < I_score.getHeight(); i++) {
211 for (unsigned int j = 0; j < I_score.getWidth(); j++) {
212 if ( !vpMath::equal(I_score[i][j], I_score_gold[i][j], 1e-9) ) {
213 std::cerr << "Issue with normalizedCorrelation, gold: " << std::setprecision(17)
214 << I_score_gold[i][j] << " ; compute: " << I_score[i][j] << std::endl;
215 return EXIT_FAILURE;
216 }
217 }
218 }
219
220 vpImagePoint max_loc, max_loc_gold;
221 double max_correlation = -1.0, max_correlation_gold = -1.0;
222 I_score.getMinMaxLoc(NULL, &max_loc, NULL, &max_correlation);
223 I_score_gold.getMinMaxLoc(NULL, &max_loc_gold, NULL, &max_correlation_gold);
224
225 std::cout << "Compare regular and SSE version of vpImageTools::normalizedCorrelation()" << std::endl;
226 std::cout << "vpImageTools::normalizedCorrelation(): " << max_correlation << " ; " << t << " ms" << std::endl;
227 std::cout << "Gold normalizedCorrelation(): " << max_correlation_gold << " ; " << t_gold << " ms" << std::endl;
228
229 std::cerr << "\nTrue template position: " << template_roi.getTopLeft() << std::endl;
230 std::cerr << "Found template position: " << max_loc << std::endl;
231 if ( vpImagePoint::distance(max_loc, template_roi.getTopLeft()) > step_i ) {
232 std::cerr << "Issue with vpImageTools::normalizedCorrelation:" << std::endl;
233 return EXIT_FAILURE;
234 }
235
236 } catch (const vpException &e) {
237 std::cerr << "\nCatch an exception: " << e << std::endl;
238 return EXIT_FAILURE;
239 }
240 return EXIT_SUCCESS;
241}
error that can be emited by ViSP classes.
Definition: vpException.h:72
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:88
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:305
static double normalizedCorrelation(const vpImage< double > &I1, const vpImage< double > &I2, bool useOptimized=true)
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:974
unsigned int getWidth() const
Definition: vpImage.h:246
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:800
unsigned int getHeight() const
Definition: vpImage.h:188
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1365
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1670
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:295
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Defines a rectangle in the plane.
Definition: vpRect.h:80
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()