36 #include <visp3/core/vpDisplay.h>
37 #include <visp3/core/vpImage.h>
38 #include <visp3/core/vpImageTools.h>
39 #include <visp3/core/vpIoTools.h>
40 #include <visp3/io/vpImageIo.h>
41 #include <visp3/io/vpParseArgv.h>
43 #include <visp3/gui/vpDisplayD3D.h>
44 #include <visp3/gui/vpDisplayGDI.h>
45 #include <visp3/gui/vpDisplayGTK.h>
46 #include <visp3/gui/vpDisplayOpenCV.h>
47 #include <visp3/gui/vpDisplayX.h>
55 #define GETOPTARGS "cdi:W:H:m:bh"
69 void usage(
const char *name,
const char *badparam, std::string ipath,
unsigned int &w,
unsigned int &h,
int &m)
75 %s [-i <input image path>] [-W <width>] [-H <height>] [-m <method>] [-b] [-c] [-d]\n\
81 -i <input image path> %s\n\
82 Set image input path.\n\
83 From this path read \"Klimt/Klimt.pgm\"\n\
85 Setting the VISP_INPUT_IMAGE_PATH environment\n\
86 variable produces the same behaviour than using\n\
90 Set the new image width.\n\
93 Set the new image height.\n\
96 Set resize interpolation method.\n\
99 Run image resize benchmark.\n\
102 Disable mouse click.\n\
105 Disable image display.\n\
108 Print the help.\n\n", ipath.c_str(), w, h, m);
111 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
128 bool getOptions(
int argc,
const char **argv, std::string &ipath,
unsigned int &w,
unsigned int &h,
int &method,
129 bool &benchmark,
bool &opt_display,
bool &opt_click)
140 w = (
unsigned int)atoi(optarg_);
143 h = (
unsigned int)atoi(optarg_);
146 method = atoi(optarg_);
152 usage(argv[0], NULL, ipath, w, h, method);
164 usage(argv[0], optarg_, ipath, w, h, method);
170 if ((c == 1) || (c == -1)) {
172 usage(argv[0], NULL, ipath, w, h, method);
173 std::cerr <<
"ERROR: " << std::endl;
174 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
182 int main(
int argc,
const char **argv)
185 std::string env_ipath;
186 std::string opt_ipath;
188 std::string filename;
189 unsigned int width = 101;
190 unsigned int height = 207;
192 bool benchmark =
false;
193 bool opt_display =
true;
194 bool opt_click =
true;
201 if (!env_ipath.empty())
205 if (getOptions(argc, argv, opt_ipath, width, height, method, benchmark, opt_display, opt_click) ==
false) {
210 if (!opt_ipath.empty())
215 if (opt_ipath.empty()) {
216 if (ipath != env_ipath) {
217 std::cout << std::endl <<
"WARNING: " << std::endl;
218 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
219 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
220 <<
" we skip the environment variable." << std::endl;
225 if (opt_ipath.empty() && env_ipath.empty()) {
226 usage(argv[0], NULL, ipath, width, height, method);
227 std::cerr << std::endl <<
"ERROR:" << std::endl;
228 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
229 <<
" environment variable to specify the location of the " << std::endl
230 <<
" image path where test images are located." << std::endl
238 for (
int m = 0; m < 3; m++) {
239 std::cout <<
"Interpolation method: " << m << std::endl;
242 for (
unsigned int cpt = 0; cpt < Itest.getSize(); cpt++) {
243 Itest.bitmap[cpt] = cpt;
246 Itest_resize2(Itest.getHeight(), Itest.getWidth());
249 std::cout <<
"Itest:\n" << Itest << std::endl;
250 std::cout <<
"Itest_resize:\n" << Itest_resize << std::endl;
251 std::cout <<
"Itest_resize2:\n" << Itest_resize2 << std::endl;
252 std::cout <<
"(Itest ==Itest_resize2)? " << (Itest == Itest_resize2) << std::endl;
255 for (
unsigned int cpt = 0; cpt < Itest.getSize(); cpt++) {
256 Itest.bitmap[cpt] = cpt;
262 std::cout <<
"\nItest:\n" << Itest << std::endl;
263 std::cout <<
"Itest_resize:\n" << Itest_resize << std::endl;
264 std::cout <<
"Itest_resize2:\n" << Itest_resize2 << std::endl;
265 std::cout <<
"(Itest ==Itest_resize2)? " << (Itest == Itest_resize2) << std::endl << std::endl;
273 std::cout <<
"Read image: " << filename << std::endl;
280 std::cout <<
"Time to resize from " << I.
getWidth() <<
"x" << I.
getHeight() <<
" to " << width <<
"x" << height
281 <<
": " << t <<
" ms" << std::endl;
283 #if defined(VISP_HAVE_X11)
285 #elif defined(VISP_HAVE_OPENCV)
287 #elif defined(VISP_HAVE_GTK)
289 #elif defined(VISP_HAVE_GDI)
291 #elif defined(VISP_HAVE_D3D9)
294 std::cerr <<
"No display available!" << std::endl;
299 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || \
300 defined(VISP_HAVE_D3D9)
301 d1->
init(I, 0, 0,
"Grayscale image");
302 d2->
init(I_resize, (
int)I.
getWidth() + 80, 0,
"Grayscale image resized");
321 std::cout <<
"\nRead image: " << filename << std::endl;
328 std::cout <<
"Time to resize from " << I_color.
getWidth() <<
"x" << I_color.
getHeight() <<
" to " << width <<
"x"
329 << height <<
": " << t <<
" ms" << std::endl;
331 #if defined(VISP_HAVE_X11)
333 #elif defined(VISP_HAVE_OPENCV)
335 #elif defined(VISP_HAVE_GTK)
337 #elif defined(VISP_HAVE_GDI)
339 #elif defined(VISP_HAVE_D3D9)
342 std::cerr <<
"No display available!" << std::endl;
347 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || \
348 defined(VISP_HAVE_D3D9)
349 d3->
init(I_color, 0, 0,
"Color image");
350 d4->
init(I_color_resize, (
int)I_color.
getWidth() + 80, 0,
"Color image resized");
363 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || \
364 defined(VISP_HAVE_D3D9)
376 std::cout <<
"\n(I_color == I_color_double_half)? " << (I_color == I_color_double_half) << std::endl;
378 double root_mean_square_error = 0.0;
379 for (
unsigned int i = 0; i < I_color.getHeight(); i++) {
380 for (
unsigned int j = 0; j < I_color.getWidth(); j++) {
381 vpColVector c_error = I_color[i][j] - I_color_double_half[i][j];
382 root_mean_square_error += c_error.
sumSquare();
385 std::cout <<
"Root Mean Square Error: " << sqrt(root_mean_square_error / (I_color.getSize() * 3)) << std::endl;
390 vpImageTools::resize(I_color_half, I_color_half_double, I_color.getWidth(), I_color.getHeight(),
392 std::cout <<
"\n(I_color == I_color_half_double)? " << (I_color == I_color_half_double) << std::endl;
394 root_mean_square_error = 0.0;
395 for (
unsigned int i = 0; i < I_color.getHeight(); i++) {
396 for (
unsigned int j = 0; j < I_color.getWidth(); j++) {
397 vpColVector c_error = I_color[i][j] - I_color_half_double[i][j];
398 root_mean_square_error += c_error.
sumSquare();
401 std::cout <<
"Root Mean Square Error: " << sqrt(root_mean_square_error / (I_color.getSize() * 3)) << std::endl;
404 #if defined(VISP_HAVE_OPENCV) && !defined(__mips__) && !defined(__mips) && !defined(mips) && !defined(__MIPS__)
405 std::vector<double> scales;
406 scales.push_back(2.0);
407 scales.push_back(3.0);
408 scales.push_back(4.0);
409 scales.push_back(5.0);
410 scales.push_back(1 / 2.0);
411 scales.push_back(1 / 3.0);
412 scales.push_back(1 / 4.0);
413 scales.push_back(1 / 5.0);
415 std::vector<vpImageTools::vpImageInterpolationType> interpolations;
420 std::vector<int> interpolationsCV;
421 interpolationsCV.push_back(cv::INTER_NEAREST);
422 interpolationsCV.push_back(cv::INTER_LINEAR);
423 interpolationsCV.push_back(cv::INTER_CUBIC);
425 std::vector<std::string> interpolationNames;
426 interpolationNames.push_back(
"INTERPOLATION_NEAREST");
427 interpolationNames.push_back(
"INTERPOLATION_LINEAR");
428 interpolationNames.push_back(
"INTERPOLATION_CUBIC");
431 cv::Mat img, img_resize_perf;
434 for (
size_t iter = 0; iter < interpolations.size(); iter ++) {
435 std::cout <<
"\nInterpolation (gray): " << interpolationNames[iter] << std::endl;
437 for (
size_t s = 0; s < scales.size(); s++) {
438 unsigned int width_resize =
static_cast<unsigned int>(I.
getWidth() * scales[s]);
439 unsigned int height_resize =
static_cast<unsigned int>(I.
getHeight() * scales[s]);
440 cv::Size new_size(
static_cast<int>(width_resize),
static_cast<int>(height_resize));
442 << width_resize <<
"x" << height_resize << std::endl;
445 for (
int nbIter = 0; nbIter < 10; nbIter++) {
451 for (
int nbIter = 0; nbIter < 10; nbIter++) {
452 cv::resize(img, img_resize_perf, new_size, 0.0, 0.0, interpolationsCV[iter]);
456 std::cout <<
"ViSP (10 iterations): " << t <<
" ms ; Mean: " << t / 10 <<
" ms" << std::endl;
457 std::cout <<
"OpenCV (10 iterations): " << t_cv <<
" ms ; Mean: " << t_cv / 10 <<
" ms" << std::endl;
459 double diff = 0.0, diff_abs = 0.0;
460 for (
int i = 0; i < img_resize_perf.rows; i++) {
461 for (
int j = 0; j < img_resize_perf.cols; j++) {
462 int d = img_resize_perf.at<uchar>(i, j) - I_resize_perf[i][j];
468 std::cout <<
"Mean diff: " << (diff / I_resize_perf.
getSize()) << std::endl;
469 std::cout <<
"Mean abs diff: " << (diff_abs / I_resize_perf.
getSize()) << std::endl;
476 cv::Mat img, img_resize_perf;
479 for (
size_t iter = 0; iter < interpolations.size(); iter++) {
480 std::cout <<
"\nInterpolation (color): " << interpolationNames[iter] << std::endl;
482 for (
size_t s = 0; s < scales.size(); s++) {
483 unsigned int width_resize =
static_cast<unsigned int>(I.
getWidth() * scales[s]);
484 unsigned int height_resize =
static_cast<unsigned int>(I.
getHeight() * scales[s]);
485 cv::Size new_size(
static_cast<int>(width_resize),
static_cast<int>(height_resize));
486 std::cout <<
"Resize from " << I_color.getWidth() <<
"x" << I_color.getHeight() <<
" to "
487 << width_resize <<
"x" << height_resize << std::endl;
490 for (
int nbIter = 0; nbIter < 10; nbIter++) {
491 vpImageTools::resize(I_color, I_resize_perf, width_resize, height_resize, interpolations[iter]);
496 for (
int nbIter = 0; nbIter < 10; nbIter++) {
497 cv::resize(img, img_resize_perf, new_size, 0.0, 0.0, interpolationsCV[iter]);
501 std::cout <<
"ViSP (10 iterations): " << t <<
" ms ; Mean: " << t / 10 <<
" ms" << std::endl;
502 std::cout <<
"OpenCV (10 iterations): " << t_cv <<
" ms ; Mean: " << t_cv / 10 <<
" ms" << std::endl;
504 double diff = 0.0, diff_abs = 0.0;
505 for (
int i = 0; i < img_resize_perf.rows; i++) {
506 for (
int j = 0; j < img_resize_perf.cols; j++) {
507 int d = (img_resize_perf.at<cv::Vec3b>(i, j)[0] - I_resize_perf[i][j].B) +
508 (img_resize_perf.at<cv::Vec3b>(i, j)[1] - I_resize_perf[i][j].G) +
509 (img_resize_perf.at<cv::Vec3b>(i, j)[2] - I_resize_perf[i][j].R);
515 std::cout <<
"Mean diff: " << (diff / I_resize_perf.
getSize()) << std::endl;
516 std::cout <<
"Mean abs diff: " << (diff_abs / I_resize_perf.
getSize()) << std::endl;
525 std::cerr <<
"Catch an exception: " << e << std::endl;