Visual Servoing Platform version 3.5.0
testImageFilter.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 some functions from vpImageFilter class.
33 *
34 *****************************************************************************/
41#include <iostream>
42#include <visp3/core/vpImageConvert.h>
43#include <visp3/core/vpImageFilter.h>
44#include <visp3/core/vpRGBa.h>
45#include <visp3/core/vpIoTools.h>
46#include <visp3/io/vpImageIo.h>
47#include <visp3/io/vpParseArgv.h>
48
49#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
50#include <opencv2/imgproc/imgproc.hpp>
51#endif
52
53// List of allowed command line options
54#define GETOPTARGS "cdi:p:h"
55
56namespace
57{
58/*
59 Print the program options.
60
61 \param name : Program name.
62 \param badparam : Bad parameter name.
63 \param ipath: Input image path.
64 */
65void usage(const char *name, const char *badparam, std::string ipath)
66{
67 fprintf(stdout, "\n\
68 Test vpImageFilter class.\n\
69 \n\
70 SYNOPSIS\n\
71 %s [-i <input image path>] [-p <personal image path>]\n\
72 [-h]\n \
73 ", name);
74
75 fprintf(stdout, "\n\
76 OPTIONS: Default\n\
77 -i <input image path> %s\n\
78 Set image input path.\n\
79 From this path read \"Klimt/Klimt.pgm,\n\
80 .ppm, .jpeg and .png images.\n\
81 Setting the VISP_INPUT_IMAGE_PATH environment\n\
82 variable produces the same behaviour than using\n\
83 this option.\n\
84 \n\
85 -p <personal image path> \n\
86 Path to an image used to test image reading function.\n\
87 Example: -p /my_path_to/image.png\n\
88 \n\
89 -h\n\
90 Print the help.\n\n", ipath.c_str());
91
92 if (badparam)
93 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
94}
95
105bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath)
106{
107 const char *optarg_;
108 int c;
109 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
110
111 switch (c) {
112 case 'i':
113 ipath = optarg_;
114 break;
115 case 'p':
116 ppath = optarg_;
117 break;
118 case 'h':
119 usage(argv[0], NULL, ipath);
120 return false;
121 break;
122
123 case 'c':
124 case 'd':
125 break;
126
127 default:
128 usage(argv[0], optarg_, ipath);
129 return false;
130 break;
131 }
132 }
133
134 if ((c == 1) || (c == -1)) {
135 // standalone param or error
136 usage(argv[0], NULL, ipath);
137 std::cerr << "ERROR: " << std::endl;
138 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
139 return false;
140 }
141
142 return true;
143}
144
145#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
146bool check_results(const cv::Mat &mat, const vpImage<double> &I, unsigned int half_size_y,
147 unsigned int half_size_x)
148{
149 for (unsigned int i = half_size_y; i < I.getHeight() - half_size_y; i++) {
150 for (unsigned int j = half_size_x; j < I.getWidth() - half_size_x; j++) {
151 if (!vpMath::equal(mat.at<double>(static_cast<int>(i), static_cast<int>(j)), I[i][j], std::numeric_limits<double>::epsilon())) {
152 return false;
153 }
154 }
155 }
156
157 return true;
158}
159
160bool check_results(const cv::Mat &mat, const vpImage<double> &I, unsigned int margin, double threshold)
161{
162 for (unsigned int i = margin; i < I.getHeight() - margin; i++) {
163 for (unsigned int j = margin; j < I.getWidth() - margin; j++) {
164 if (!vpMath::equal(mat.at<unsigned char>(static_cast<int>(i), static_cast<int>(j)), I[i][j], threshold)) {
165 return false;
166 }
167 }
168 }
169
170 return true;
171}
172
173bool check_results(const cv::Mat &mat, const vpImage<vpRGBa> &I, unsigned int margin, double threshold)
174{
175 for (unsigned int i = margin; i < I.getHeight() - margin; i++) {
176 for (unsigned int j = margin; j < I.getWidth() - margin; j++) {
177 if (!vpMath::equal(static_cast<double>(mat.at<cv::Vec3b>(static_cast<int>(i), static_cast<int>(j))[2]), I[i][j].R, threshold)) {
178 return false;
179 }
180 if (!vpMath::equal(static_cast<double>(mat.at<cv::Vec3b>(static_cast<int>(i), static_cast<int>(j))[1]), I[i][j].G, threshold)) {
181 return false;
182 }
183 if (!vpMath::equal(static_cast<double>(mat.at<cv::Vec3b>(static_cast<int>(i), static_cast<int>(j))[0]), I[i][j].B, threshold)) {
184 return false;
185 }
186 }
187 }
188
189 return true;
190}
191#endif
192}
193
194int main(int argc, const char *argv[])
195{
196 try {
197 std::string env_ipath;
198 std::string opt_ipath;
199 std::string opt_ppath;
200 std::string ipath;
201 std::string filename;
202
203 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
204 // environment variable value
206
207 // Set the default input path
208 if (!env_ipath.empty())
209 ipath = env_ipath;
210
211 // Read the command line options
212 if (getOptions(argc, argv, opt_ipath, opt_ppath) == false) {
213 exit(EXIT_FAILURE);
214 }
215
216 // Get the option values
217 if (!opt_ipath.empty())
218 ipath = opt_ipath;
219
220 // Compare ipath and env_ipath. If they differ, we take into account
221 // the input path comming from the command line option
222 if (!opt_ipath.empty() && !env_ipath.empty()) {
223 if (ipath != env_ipath) {
224 std::cout << std::endl << "WARNING: " << std::endl;
225 std::cout << " Since -i <visp image path=" << ipath << "> "
226 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
227 << " we skip the environment variable." << std::endl;
228 }
229 }
230
231 //
232 // Here starts really the test
233 //
234 vpMatrix kernel_1(2, 2);
235 for (unsigned int i = 0, cpt = 1; i < kernel_1.getRows(); i++) {
236 for (unsigned int j = 0; j < kernel_1.getCols(); j++, cpt++) {
237 kernel_1[i][j] = cpt;
238 }
239 }
240 std::cout << "kernel_1:\n" << kernel_1 << std::endl;
241
242 vpMatrix kernel_2(3, 3);
243 for (unsigned int i = 0, cpt = 1; i < kernel_2.getRows(); i++) {
244 for (unsigned int j = 0; j < kernel_2.getCols(); j++, cpt++) {
245 kernel_2[i][j] = cpt;
246 }
247 }
248 std::cout << "kernel_2:\n" << kernel_2 << std::endl;
249
250 vpMatrix kernel_3(2, 3);
251 for (unsigned int i = 0, cpt = 1; i < kernel_3.getRows(); i++) {
252 for (unsigned int j = 0; j < kernel_3.getCols(); j++, cpt++) {
253 kernel_3[i][j] = cpt;
254 }
255 }
256 std::cout << "kernel_3:\n" << kernel_3 << std::endl;
257
258 {
259 // Test on small images first
261 for (unsigned int i = 0; i < I.getSize(); i++) {
262 I.bitmap[i] = (unsigned char)i;
263 }
264 std::cout << "I:\n" << I << std::endl;
265
266 // Test correlation
267 vpImage<double> I_correlation_1, I_correlation_2, I_correlation_3;
268 vpImageFilter::filter(I, I_correlation_1, kernel_1);
269 vpImageFilter::filter(I, I_correlation_2, kernel_2);
270 vpImageFilter::filter(I, I_correlation_3, kernel_3);
271
272 std::cout << "\nI_correlation_1:\n" << I_correlation_1 << std::endl;
273 std::cout << "I_correlation_2:\n" << I_correlation_2 << std::endl;
274 std::cout << "I_correlation_3:\n" << I_correlation_3 << std::endl;
275
276#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
277 cv::Mat matImg;
278 vpImageConvert::convert(I, matImg);
279
280 cv::Mat mat_kernel_1(2, 2, CV_64F);
281 for (int i = 0, cpt = 1; i < mat_kernel_1.rows; i++) {
282 for (int j = 0; j < mat_kernel_1.cols; j++, cpt++) {
283 mat_kernel_1.at<double>(i, j) = cpt;
284 }
285 }
286
287 cv::Mat mat_kernel_2(3, 3, CV_64F);
288 for (int i = 0, cpt = 1; i < mat_kernel_2.rows; i++) {
289 for (int j = 0; j < mat_kernel_2.cols; j++, cpt++) {
290 mat_kernel_2.at<double>(i, j) = cpt;
291 }
292 }
293
294 cv::Mat mat_kernel_3(2, 3, CV_64F);
295 for (int i = 0, cpt = 1; i < mat_kernel_3.rows; i++) {
296 for (int j = 0; j < mat_kernel_3.cols; j++, cpt++) {
297 mat_kernel_3.at<double>(i, j) = cpt;
298 }
299 }
300
301 cv::Mat matImg_correlation_1, matImg_correlation_2, matImg_correlation_3;
302 cv::filter2D(matImg, matImg_correlation_1, CV_64F, mat_kernel_1);
303 cv::filter2D(matImg, matImg_correlation_2, CV_64F, mat_kernel_2);
304 cv::filter2D(matImg, matImg_correlation_3, CV_64F, mat_kernel_3);
305
306 std::cout << "\nTest correlation on small image:" << std::endl;
307 std::cout << "(I_correlation_1 == matImg_correlation_1)? "
308 << check_results(matImg_correlation_1, I_correlation_1, kernel_1.getRows() / 2, kernel_1.getCols() / 2)
309 << std::endl;
310 std::cout << "(I_correlation_2 == matImg_correlation_2)? "
311 << check_results(matImg_correlation_2, I_correlation_2, kernel_2.getRows() / 2, kernel_2.getCols() / 2)
312 << std::endl;
313 std::cout << "(I_correlation_3 == matImg_correlation_3)? "
314 << check_results(matImg_correlation_3, I_correlation_3, kernel_3.getRows() / 2, kernel_3.getCols() / 2)
315 << std::endl;
316#endif
317
318 // Test convolution
319 vpImage<double> I_convolution_1, I_convolution_2, I_convolution_3;
320 vpImageFilter::filter(I, I_convolution_1, kernel_1, true);
321 vpImageFilter::filter(I, I_convolution_2, kernel_2, true);
322 vpImageFilter::filter(I, I_convolution_3, kernel_3, true);
323
324 std::cout << "\nI_convolution_1:\n" << I_convolution_1 << std::endl;
325 std::cout << "I_convolution_2:\n" << I_convolution_2 << std::endl;
326 std::cout << "I_convolution_3:\n" << I_convolution_3 << std::endl;
327
328#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
329 cv::Mat mat_kernel_1_flip, mat_kernel_2_flip, mat_kernel_3_flip;
330 cv::flip(mat_kernel_1, mat_kernel_1_flip, -1);
331 cv::flip(mat_kernel_2, mat_kernel_2_flip, -1);
332 cv::flip(mat_kernel_3, mat_kernel_3_flip, -1);
333
334 cv::Mat matImg_convolution_1, matImg_convolution_2, matImg_convolution_3;
335
336 cv::Point anchor1(mat_kernel_1_flip.cols - mat_kernel_1_flip.cols / 2 - 1,
337 mat_kernel_1_flip.rows - mat_kernel_1_flip.rows / 2 - 1);
338 cv::filter2D(matImg, matImg_convolution_1, CV_64F, mat_kernel_1_flip, anchor1);
339
340 cv::Point anchor2(mat_kernel_2_flip.cols - mat_kernel_2_flip.cols / 2 - 1,
341 mat_kernel_2_flip.rows - mat_kernel_2_flip.rows / 2 - 1);
342 cv::filter2D(matImg, matImg_convolution_2, CV_64F, mat_kernel_2_flip, anchor2);
343
344 cv::Point anchor3(mat_kernel_3_flip.cols - mat_kernel_3_flip.cols / 2 - 1,
345 mat_kernel_3_flip.rows - mat_kernel_3_flip.rows / 2 - 1);
346 cv::filter2D(matImg, matImg_convolution_3, CV_64F, mat_kernel_3_flip, anchor3);
347
348 std::cout << "\nTest convolution on small image:" << std::endl;
349 std::cout << "(I_convolution_1 == matImg_convolution_1)? "
350 << check_results(matImg_convolution_1, I_convolution_1, kernel_1.getRows() / 2, kernel_1.getCols() / 2)
351 << std::endl;
352 std::cout << "(I_convolution_2 == matImg_convolution_2)? "
353 << check_results(matImg_convolution_2, I_convolution_2, kernel_2.getRows() / 2, kernel_2.getCols() / 2)
354 << std::endl;
355 std::cout << "(I_convolution_3 == matImg_convolution_3)? "
356 << check_results(matImg_convolution_3, I_convolution_3, kernel_3.getRows() / 2, kernel_3.getCols() / 2)
357 << std::endl;
358#endif
359 if (opt_ppath.empty()) {
360 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
361 vpImageIo::read(I, filename);
362 } else {
363 filename = opt_ppath;
364 vpImageIo::read(I, filename);
365 printf("Image \"%s\" read successfully\n", filename.c_str());
366 }
367
368 // Test correlation
369 double t = vpTime::measureTimeMs();
370 vpImageFilter::filter(I, I_correlation_1, kernel_1);
371 vpImageFilter::filter(I, I_correlation_2, kernel_2);
372 vpImageFilter::filter(I, I_correlation_3, kernel_3);
373 t = vpTime::measureTimeMs() - t;
374 std::cout << "\nTime to do 3 correlation filtering: " << t << " ms ; Mean: " << t / 3.0 << " ms" << std::endl;
375
376#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
377 vpImageConvert::convert(I, matImg);
378
380 cv::filter2D(matImg, matImg_correlation_1, CV_64F, mat_kernel_1);
381 cv::filter2D(matImg, matImg_correlation_2, CV_64F, mat_kernel_2);
382 cv::filter2D(matImg, matImg_correlation_3, CV_64F, mat_kernel_3);
383 t = vpTime::measureTimeMs() - t;
384 std::cout << "Time to do 3 cv::filter2D: " << t << " ms ; Mean: " << t / 3.0 << " ms" << std::endl;
385
386 std::cout << "\nTest correlation on Klimt image:" << std::endl;
387 bool test = check_results(matImg_correlation_1, I_correlation_1, kernel_1.getRows() / 2, kernel_1.getCols() / 2);
388 std::cout << "(I_correlation_1 == matImg_correlation_1)? " << test << std::endl;
389 if (!test) {
390 std::cerr << "Failed test1 correlation with vpImageFilter::filter()!" << std::endl;
391 return EXIT_FAILURE;
392 }
393
394 test = check_results(matImg_correlation_2, I_correlation_2, kernel_2.getRows() / 2, kernel_2.getCols() / 2);
395 std::cout << "(I_correlation_2 == matImg_correlation_2)? " << test << std::endl;
396 if (!test) {
397 std::cerr << "Failed test2 correlation with vpImageFilter::filter()!" << std::endl;
398 return EXIT_FAILURE;
399 }
400
401 test = check_results(matImg_correlation_3, I_correlation_3, kernel_3.getRows() / 2, kernel_3.getCols() / 2);
402 std::cout << "(I_correlation_3 == matImg_correlation_3)? " << test << std::endl;
403 if (!test) {
404 std::cerr << "Failed test3 correlation with vpImageFilter::filter()!" << std::endl;
405 return EXIT_FAILURE;
406 }
407#endif
408
409 // Test convolution
411 vpImageFilter::filter(I, I_convolution_1, kernel_1, true);
412 vpImageFilter::filter(I, I_convolution_2, kernel_2, true);
413 vpImageFilter::filter(I, I_convolution_3, kernel_3, true);
414 t = vpTime::measureTimeMs() - t;
415 std::cout << "\nTime to do 3 convolution filtering: " << t << " ms ; Mean: " << t / 3.0 << " ms" << std::endl;
416
417#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
418
420 cv::filter2D(matImg, matImg_convolution_1, CV_64F, mat_kernel_1_flip, anchor1);
421 cv::filter2D(matImg, matImg_convolution_2, CV_64F, mat_kernel_2_flip, anchor2);
422 cv::filter2D(matImg, matImg_convolution_3, CV_64F, mat_kernel_3_flip, anchor3);
423 t = vpTime::measureTimeMs() - t;
424 std::cout << "Time to do 3 cv::filter2D: " << t << " ms ; Mean: " << t / 3.0 << " ms" << std::endl;
425
426 std::cout << "\nTest convolution on Klimt image:" << std::endl;
427 test = check_results(matImg_convolution_1, I_convolution_1, kernel_1.getRows() / 2, kernel_1.getCols() / 2);
428 std::cout << "(I_convolution_1 == matImg_convolution_1)? " << test << std::endl;
429 if (!test) {
430 std::cerr << "Failed test1 convolution with vpImageFilter::filter()!" << std::endl;
431 return EXIT_FAILURE;
432 }
433
434 test = check_results(matImg_convolution_2, I_convolution_2, kernel_2.getRows() / 2, kernel_2.getCols() / 2);
435 std::cout << "(I_convolution_2 == matImg_convolution_2)? " << test << std::endl;
436 if (!test) {
437 std::cerr << "Failed test2 convolution with vpImageFilter::filter()!" << std::endl;
438 return EXIT_FAILURE;
439 }
440
441 test = check_results(matImg_convolution_3, I_convolution_3, kernel_3.getRows() / 2, kernel_3.getCols() / 2);
442 std::cout << "(I_convolution_3 == matImg_convolution_3)? " << test << std::endl;
443 if (!test) {
444 std::cerr << "Failed test3 convolution with vpImageFilter::filter()!" << std::endl;
445 return EXIT_FAILURE;
446 }
447#endif
448
449 // Test Sobel
450 vpMatrix kernel_sobel_x_flip(5, 5);
451 vpImageFilter::getSobelKernelX(kernel_sobel_x_flip.data, 2);
452 vpMatrix kernel_sobel_x(5, 5);
453 for (unsigned int i = 0; i < kernel_sobel_x.getRows(); i++) {
454 for (unsigned int j = 0; j < kernel_sobel_x.getCols(); j++) {
455 kernel_sobel_x[i][j] = kernel_sobel_x_flip[i][kernel_sobel_x.getCols()-1-j];
456 }
457 }
458
459 vpImage<double> I_sobel_x;
461 vpImageFilter::filter(I, I_sobel_x, kernel_sobel_x, true);
462 t = vpTime::measureTimeMs() - t;
463 std::cout << "\nTime to do Sobel: " << t << " ms" << std::endl;
464
465#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
466 cv::Mat matImg_sobel_x;
468 cv::Sobel(matImg, matImg_sobel_x, CV_64F, 1, 0, 5);
469 t = vpTime::measureTimeMs() - t;
470 std::cout << "Time to do cv::Sobel: " << t << " ms" << std::endl;
471
472 std::cout << "\nTest Sobel on Klimt image:" << std::endl;
473 std::cout << "(I_sobel_x == matImg_sobel_x)? "
474 << check_results(matImg_sobel_x, I_sobel_x, kernel_sobel_x.getRows() / 2, kernel_sobel_x.getCols() / 2)
475 << std::endl;
476#endif
477
478 vpImage<double> I_double, Iu, Iv;
479 vpImageConvert::convert(I, I_double);
481 vpImageFilter::filter(I_double, Iu, Iv, kernel_sobel_x, true);
482 t = vpTime::measureTimeMs() - t;
483 std::cout << "\nTime to do Sobel Iu and Iv: " << t << " ms" << std::endl;
484
485#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
486 cv::Mat matImg_sobel_y;
487 cv::Sobel(matImg, matImg_sobel_y, CV_64F, 0, 1, 5);
488
489 std::cout << "(Iu == matImg_sobel_x)? "
490 << check_results(matImg_sobel_x, Iu, kernel_sobel_x.getRows() / 2, kernel_sobel_x.getCols() / 2)
491 << std::endl;
492 std::cout << "(Iv == matImg_sobel_y)? "
493 << check_results(matImg_sobel_y, Iv, kernel_sobel_x.getRows() / 2, kernel_sobel_x.getCols() / 2)
494 << std::endl;
495#endif
496
497 // Test Sobel separable filters
498 vpImage<double> I_sep_filtered;
499 vpColVector kernel_sep_x(5);
500 kernel_sep_x[0] = 1.0;
501 kernel_sep_x[1] = 2.0;
502 kernel_sep_x[2] = 0.0;
503 kernel_sep_x[3] = -2.0;
504 kernel_sep_x[4] = -1.0;
505 vpColVector kernel_sep_y(5);
506 kernel_sep_y[0] = 1.0;
507 kernel_sep_y[1] = 4.0;
508 kernel_sep_y[2] = 6.0;
509 kernel_sep_y[3] = 4.0;
510 kernel_sep_y[4] = 1.0;
511
513 vpImageFilter::sepFilter(I, I_sep_filtered, kernel_sep_x, kernel_sep_y);
514 t = vpTime::measureTimeMs() - t;
515 std::cout << "\nTime to do sepFilter: " << t << " ms" << std::endl;
516
517#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
518 test = check_results(matImg_sobel_x, Iu, I_sep_filtered.getRows() / 2, kernel_sobel_x.getCols() / 2);
519 std::cout << "(I_sep_filtered == matImg_sobel_x)? " << test << std::endl;
520
521 if (!test) {
522 std::cerr << "Failed separable filter!" << std::endl;
523 return EXIT_FAILURE;
524 }
525#endif
526 }
527 {
528 // Test Gaussian blur on grayscale image
529
530 std::cout << "\nTest Gaussian Blur on Klimt grayscale image:" << std::endl;
532 vpImage<double> I_blur;
533 // Test on real image
534
535 if (opt_ppath.empty()) {
536 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
537 vpImageIo::read(I, filename);
538 } else {
539 filename = opt_ppath;
540 vpImageIo::read(I, filename);
541 printf("Image \"%s\" read successfully\n", filename.c_str());
542 }
543
544 unsigned int gaussian_filter_size = 7;
545 double sigma = 3;
546 double t = vpTime::measureTimeMs();
547 vpImageFilter::gaussianBlur(I, I_blur, gaussian_filter_size, sigma);
548 t = vpTime::measureTimeMs() - t;
549 std::cout << "Time to do ViSP Gaussian Blur on grayscale images: " << t << " ms" << std::endl;
550
551#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
552 cv::Mat matImg, matImg_blur;
553 vpImageConvert::convert(I, matImg);
555 cv::GaussianBlur(matImg, matImg_blur, cv::Size(gaussian_filter_size, gaussian_filter_size), sigma, 0);
556 t = vpTime::measureTimeMs() - t;
557 std::cout << "Time to do OpenCV Gaussian Blur on grayscale images: " << t << " ms" << std::endl;
558
559 double threshold = 3.;
560 unsigned int margin = 3;
561 bool test = check_results(matImg_blur, I_blur, margin, threshold);
562 std::cout << "(I_blur == matImg_blur)? " << test << std::endl;
563
564 if (!test) {
565 std::cerr << "Failed Gaussian blur filter on grayscale image!" << std::endl;
566 return EXIT_FAILURE;
567 }
568#endif
569 }
570
571 {
572 // Test Gaussian blur on color image
573 std::cout << "\nTest Gaussian Blur on Klimt color image:" << std::endl;
574
575 vpImage<vpRGBa> I_rgb, I_rgb_blur;
576 // Test on real image
577
578 if (opt_ppath.empty()) {
579 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
580 vpImageIo::read(I_rgb, filename);
581 } else {
582 filename = opt_ppath;
583 vpImageIo::read(I_rgb, filename);
584 printf("Image \"%s\" read successfully\n", filename.c_str());
585 }
586
587 unsigned int gaussian_filter_size = 7;
588 double sigma = 3;
589 double t = vpTime::measureTimeMs();
590 vpImageFilter::gaussianBlur(I_rgb, I_rgb_blur, gaussian_filter_size, sigma);
591 t = vpTime::measureTimeMs() - t;
592 std::cout << "Time to do ViSP Gaussian Blur on color images: " << t << " ms" << std::endl;
593
594#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020408)
595 cv::Mat matImg_rgb, matImg_rgb_blur;
596 vpImageConvert::convert(I_rgb, matImg_rgb);
598 cv::GaussianBlur(matImg_rgb, matImg_rgb_blur, cv::Size(gaussian_filter_size, gaussian_filter_size), sigma, 0);
599 t = vpTime::measureTimeMs() - t;
600 std::cout << "Time to do OpenCV Gaussian Blur on color images: " << t << " ms" << std::endl;
601
602 double threshold = 3.;
603 unsigned int margin = 3;
604 bool test = check_results(matImg_rgb_blur, I_rgb_blur, margin, threshold);
605 std::cout << "(I_rgb_blur == matImg_rgb_blur)? " << test << std::endl;
606
607 if (!test) {
608 std::cerr << "Failed Gaussian blur filter on color image!" << std::endl;
609 return EXIT_FAILURE;
610 }
611#endif
612 }
613
614 } catch (const vpException &e) {
615 std::cerr << "Catch an exception: " << e.what() << std::endl;
616 return EXIT_FAILURE;
617 }
618
619 std::cout << "\ntestImageFilter is ok." << std::endl;
620 return EXIT_SUCCESS;
621}
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
error that can be emited by ViSP classes.
Definition: vpException.h:72
const char * what() const
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M, bool convolve=false)
static void gaussianBlur(const vpImage< unsigned char > &I, vpImage< double > &GI, unsigned int size=7, double sigma=0., bool normalize=true)
static void sepFilter(const vpImage< unsigned char > &I, vpImage< double > &If, const vpColVector &kernelH, const vpColVector &kernelV)
static double getSobelKernelX(double *filter, unsigned int size)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:149
unsigned int getWidth() const
Definition: vpImage.h:246
unsigned int getSize() const
Definition: vpImage.h:227
Type * bitmap
points toward the bitmap
Definition: vpImage.h:143
unsigned int getHeight() const
Definition: vpImage.h:188
unsigned int getRows() const
Definition: vpImage.h:218
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
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:154
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
VISP_EXPORT double measureTimeMs()