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>
50 #define GETOPTARGS "cdi:th"
52 #ifdef ENABLE_VISP_NAMESPACE
58 void usage(
const char *name,
const char *badparam, std::string ipath)
61 Test vpImageTools::templateMatching().\n\
64 %s [-i <VISP_IMAGES directory>] \n\
72 -i <VISP_IMAGES directory> %s\n\
73 Set VISP_IMAGES input path.\n\
74 Setting the VISP_INPUT_IMAGE_PATH environment\n\
75 variable produces the same behaviour than using\n\
81 Perform template matching on cube sequence.\n\
87 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
90 bool getOptions(
int argc,
const char **argv, std::string &ipath,
bool &click,
bool &doTemplateMatching)
101 usage(argv[0],
nullptr, ipath);
105 doTemplateMatching =
true;
115 usage(argv[0], optarg_, ipath);
121 if ((c == 1) || (c == -1)) {
123 usage(argv[0],
nullptr, ipath);
124 std::cerr <<
"ERROR: " << std::endl;
125 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
133 int main(
int argc,
const char **argv)
135 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGPROC)
137 const int h = 5, w = 5;
167 std::cout <<
"I:\n" << I << std::endl;
168 std::cout <<
"II:\n" << II << std::endl;
169 std::cout <<
"IIsq:\n" << IIsq << std::endl;
171 cv::Mat mat(h, w, CV_64F);
172 for (
int i = 0; i < h; i++) {
173 for (
int j = 0; j < w; j++) {
174 mat.at<
double>(i, j) = I[i][j];
179 cv::integral(mat, sum, sqsum);
180 std::cout <<
"mat:\n" << mat << std::endl;
181 std::cout <<
"sum:\n" << sum << std::endl;
182 std::cout <<
"sqsum:\n" << sqsum << std::endl;
184 for (
int i = 0; i < h; i++) {
185 for (
int j = 0; j < w; j++) {
186 if (!
vpMath::equal(II[i][j], sum.at<
double>(i, j), std::numeric_limits<double>::epsilon())) {
187 std::cerr <<
"Error vpImageTools::integralImage(II), reference: " << std::setprecision(17)
188 << sum.at<
double>(i, j) <<
" ; compute: " << II[i][j] << std::endl;
192 if (!
vpMath::equal(IIsq[i][j], sqsum.at<
double>(i, j), std::numeric_limits<double>::epsilon())) {
193 std::cerr <<
"Error vpImageTools::integralImage(IIsq), reference: " << std::setprecision(17)
194 << sqsum.at<
double>(i, j) <<
" ; compute: " << IIsq[i][j] << std::endl;
203 std::string env_ipath;
204 std::string opt_ipath;
206 std::string filename;
208 bool doTemplateMatching =
false;
210 #if VISP_HAVE_DATASET_VERSION >= 0x030600
211 std::string ext(
"png");
213 std::string ext(
"pgm");
221 if (!env_ipath.empty()) {
226 if (!getOptions(argc, argv, opt_ipath, click, doTemplateMatching)) {
231 if (!opt_ipath.empty()) {
237 if (!opt_ipath.empty() && !env_ipath.empty()) {
238 if (ipath != env_ipath) {
239 std::cout << std::endl <<
"WARNING: " << std::endl;
240 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
241 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
242 <<
" we skip the environment variable." << std::endl;
247 if (opt_ipath.empty() && env_ipath.empty()) {
248 usage(argv[0],
nullptr, ipath);
249 std::cerr << std::endl <<
"ERROR:" << std::endl;
250 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
251 <<
" environment variable to specify the location of the " << std::endl
252 <<
" image path where test images are located." << std::endl
271 if (doTemplateMatching) {
272 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
274 #if defined(VISP_HAVE_X11)
276 #elif defined(VISP_HAVE_GDI)
278 #elif defined(HAVE_OPENCV_HIGHGUI)
282 d.
init(I, 0, 0,
"Image");
285 std::vector<double> benchmark_vec;
287 while (!reader.
end() && !quit) {
292 std::stringstream ss;
298 const unsigned int step_u = 5, step_v = 5;
302 double max_correlation = -1.0;
303 I_score.
getMinMaxLoc(
nullptr, &max_loc,
nullptr, &max_correlation);
305 benchmark_vec.push_back(t_proc);
308 ss <<
"Template matching: " << t_proc <<
" ms";
312 ss <<
"Max correlation: " << max_correlation;
336 if (!benchmark_vec.empty()) {
337 std::cout <<
"Processing time, Mean: " <<
vpMath::getMean(benchmark_vec)
346 const unsigned int step_u = 5, step_v = 5;
357 std::cout <<
"Template matching: " << t <<
" ms" << std::endl;
358 std::cout <<
"Template matching (gold): " << t_gold <<
" ms" << std::endl;
360 for (
unsigned int i = 0; i < I_score.
getHeight(); i++) {
361 for (
unsigned int j = 0; j < I_score.
getWidth(); j++) {
362 if (!
vpMath::equal(I_score[i][j], I_score_gold[i][j], 1e-9)) {
363 std::cerr <<
"Issue with template matching, gold: " << std::setprecision(17) << I_score_gold[i][j]
364 <<
" ; compute: " << I_score[i][j] << std::endl;
373 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...
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &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()