Test image resize.
#include <visp3/core/vpImage.h>
#include <visp3/core/vpDisplay.h>
#include <visp3/io/vpImageIo.h>
#include <visp3/core/vpImageTools.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/io/vpParseArgv.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayD3D.h>
#define GETOPTARGS "cdi:W:H:m:h"
namespace {
void usage(const char *name, const char *badparam, std::string ipath, unsigned int &w, unsigned int &h, int &m)
{
fprintf(stdout, "\n\
Test image resize.\n\
\n\
SYNOPSIS\n\
%s [-i <input image path>] [-W <width>] [-H <height>] [-m <method>] [-c] [-d]\n\
[-h]\n \
", name);
fprintf(stdout, "\n\
OPTIONS: Default\n\
-i <input image path> %s\n\
Set image input path.\n\
From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
image.\n\
Setting the VISP_INPUT_IMAGE_PATH environment\n\
variable produces the same behaviour than using\n\
this option.\n\
\n\
-W <width> %d\n\
Set the new image width.\n\
\n\
-H <height> %d\n\
Set the new image height.\n\
\n\
-m <method> %d\n\
Set resize interpolation method.\n\
\n\
-c \n\
Disable mouse click.\n\
\n\
-d \n\
Disable image display.\n\
\n\
-h\n\
Print the help.\n\n",
ipath.c_str(), w, h, m);
if (badparam)
fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
}
bool getOptions(int argc, const char **argv, std::string &ipath, unsigned int &w, unsigned int &h, int &method,
bool &opt_display, bool &opt_click)
{
const char *optarg_;
int c;
switch (c) {
case 'i': ipath = optarg_; break;
case 'W': w = (unsigned int) atoi(optarg_); break;
case 'H': h = (unsigned int) atoi(optarg_); break;
case 'm': method = atoi(optarg_); break;
case 'h': usage(argv[0], NULL, ipath, w, h, method); return false; break;
case 'c': opt_click = false; break;
case 'd': opt_display = false; break;
break;
default:
usage(argv[0], optarg_, ipath, w, h, method); return false; break;
}
}
if ((c == 1) || (c == -1)) {
usage(argv[0], NULL, ipath, w, h, method);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
return false;
}
return true;
}
}
int
main(int argc, const char ** argv)
{
try {
std::string env_ipath;
std::string opt_ipath;
std::string ipath;
std::string filename;
unsigned int width = 101;
unsigned int height = 207;
int method = 0;
bool opt_display = true;
bool opt_click = true;
if (! env_ipath.empty())
ipath = env_ipath;
if (getOptions(argc, argv, opt_ipath, width, height, method, opt_display, opt_click) == false) {
exit (EXIT_FAILURE);
}
if (!opt_ipath.empty())
ipath = opt_ipath;
if (opt_ipath.empty()) {
if (ipath != env_ipath) {
std::cout << std::endl
<< "WARNING: " << std::endl;
std::cout << " Since -i <visp image path=" << ipath << "> "
<< " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
<< " we skip the environment variable." << std::endl;
}
}
if (opt_ipath.empty() && env_ipath.empty()){
usage(argv[0], NULL, ipath, width, height, method);
std::cerr << std::endl
<< "ERROR:" << std::endl;
std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
<< std::endl
<< " environment variable to specify the location of the " << std::endl
<< " image path where test images are located." << std::endl << std::endl;
exit(EXIT_FAILURE);
}
for (int m = 0; m < 3; m++) {
std::cout << "Interpolation method: " << m << std::endl;
for (unsigned int cpt = 0; cpt < Itest.getSize(); cpt++) {
Itest.bitmap[cpt] = cpt;
}
vpImage<unsigned char> Itest_resize(Itest.getHeight()*2, Itest.getWidth()*2), Itest_resize2(Itest.getHeight(), Itest.getWidth());
std::cout << "Itest:\n" << Itest << std::endl;
std::cout << "Itest_resize:\n" << Itest_resize << std::endl;
std::cout << "Itest_resize2:\n" << Itest_resize2 << std::endl;
std::cout << "(Itest ==Itest_resize2)? " << (Itest == Itest_resize2) << std::endl;
Itest.resize(4,4);
for (unsigned int cpt = 0; cpt < Itest.getSize(); cpt++) {
Itest.bitmap[cpt] = cpt;
}
std::cout << "\nItest:\n" << Itest << std::endl;
std::cout << "Itest_resize:\n" << Itest_resize << std::endl;
std::cout << "Itest_resize2:\n" << Itest_resize2 << std::endl;
std::cout << "(Itest ==Itest_resize2)? " << (Itest == Itest_resize2) << std::endl << std::endl;
}
std::cout << "Read image: " << filename << std::endl;
std::cout <<
"Time to resize from " << I.
getWidth() <<
"x" << I.
getHeight() <<
" to " << width <<
"x" << height <<
": " << t <<
" ms" << std::endl;
#if defined (VISP_HAVE_X11)
#elif defined (VISP_HAVE_OPENCV)
#elif defined (VISP_HAVE_GTK)
#elif defined (VISP_HAVE_GDI)
#elif defined (VISP_HAVE_D3D9)
#else
std::cerr << "No display available!" << std::endl;
opt_display = false;
#endif
if (opt_display) {
#if defined (VISP_HAVE_X11) || defined (VISP_HAVE_OPENCV) || defined (VISP_HAVE_GTK) || defined (VISP_HAVE_GDI) || defined (VISP_HAVE_D3D9)
d1->
init(I, 0, 0,
"Grayscale image");
d2->
init(I_resize, (
int) I.
getWidth()+80, 0,
"Grayscale image resized");
#endif
if (opt_click) {
}
}
std::cout << "\nRead image: " << filename << std::endl;
std::cout <<
"Time to resize from " << I_color.
getWidth() <<
"x" << I_color.
getHeight() <<
" to " << width <<
"x" << height <<
": " << t <<
" ms" << std::endl;
#if defined (VISP_HAVE_X11)
#elif defined (VISP_HAVE_OPENCV)
#elif defined (VISP_HAVE_GTK)
#elif defined (VISP_HAVE_GDI)
#elif defined (VISP_HAVE_D3D9)
#else
std::cerr << "No display available!" << std::endl;
opt_display = false;
#endif
if (opt_display) {
#if defined (VISP_HAVE_X11) || defined (VISP_HAVE_OPENCV) || defined (VISP_HAVE_GTK) || defined (VISP_HAVE_GDI) || defined (VISP_HAVE_D3D9)
d3->
init(I_color, 0, 0,
"Color image");
d4->
init(I_color_resize, (
int) I_color.
getWidth()+80, 0,
"Color image resized");
#endif
if (opt_click) {
}
}
#if defined (VISP_HAVE_X11) || defined (VISP_HAVE_OPENCV) || defined (VISP_HAVE_GTK) || defined (VISP_HAVE_GDI) || defined (VISP_HAVE_D3D9)
delete d1;
delete d2;
delete d3;
delete d4;
#endif
std::cout << "\n(I_color == I_color_double_half)? " << (I_color == I_color_double_half) << std::endl;
double root_mean_square_error = 0.0;
for (unsigned int i = 0; i < I_color.getHeight(); i++) {
for (unsigned int j = 0; j < I_color.getWidth(); j++) {
vpColVector c_error = I_color[i][j] - I_color_double_half[i][j];
root_mean_square_error += c_error.
sumSquare();
}
}
std::cout << "Root Mean Square Error: " << sqrt(root_mean_square_error / (I_color.getSize()*3)) << std::endl;
std::cout << "\n(I_color == I_color_half_double)? " << (I_color == I_color_half_double) << std::endl;
root_mean_square_error = 0.0;
for (unsigned int i = 0; i < I_color.getHeight(); i++) {
for (unsigned int j = 0; j < I_color.getWidth(); j++) {
vpColVector c_error = I_color[i][j] - I_color_half_double[i][j];
root_mean_square_error += c_error.
sumSquare();
}
}
std::cout << "Root Mean Square Error: " << sqrt(root_mean_square_error / (I_color.getSize()*3)) << std::endl;
return EXIT_SUCCESS;
}
std::cerr << "Catch an exception: " << e << std::endl;
return EXIT_FAILURE;
}
}