Test image resize.
#include <visp3/core/vpDisplay.h>
#include <visp3/core/vpImage.h>
#include <visp3/core/vpImageTools.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/io/vpImageIo.h>
#include <visp3/io/vpParseArgv.h>
#include <visp3/gui/vpDisplayD3D.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/gui/vpDisplayX.h>
#define GETOPTARGS "cdi:W:H:m:bh"
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>] [-b] [-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 \"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> %u\n\
Set the new image width.\n\
\n\
-H <height> %u\n\
Set the new image height.\n\
\n\
-m <method> %d\n\
Set resize interpolation method.\n\
\n\
-b \n\
Run image resize benchmark.\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 &benchmark, 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 'b':
benchmark = true;
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;
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 benchmark = false;
bool opt_display = true;
bool opt_click = true;
if (!env_ipath.empty())
ipath = env_ipath;
if (getOptions(argc, argv, opt_ipath, width, height, method, benchmark, 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++) {
}
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;
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;
if (benchmark) {
#if defined(VISP_HAVE_OPENCV) && !defined(__mips__) && !defined(__mips) && !defined(mips) && !defined(__MIPS__)
std::vector<double> scales;
scales.push_back(2.0);
scales.push_back(3.0);
scales.push_back(4.0);
scales.push_back(5.0);
scales.push_back(1 / 2.0);
scales.push_back(1 / 3.0);
scales.push_back(1 / 4.0);
scales.push_back(1 / 5.0);
std::vector<vpImageTools::vpImageInterpolationType> interpolations;
std::vector<int> interpolationsCV;
interpolationsCV.push_back(cv::INTER_NEAREST);
interpolationsCV.push_back(cv::INTER_LINEAR);
interpolationsCV.push_back(cv::INTER_CUBIC);
std::vector<std::string> interpolationNames;
interpolationNames.push_back("INTERPOLATION_NEAREST");
interpolationNames.push_back("INTERPOLATION_LINEAR");
interpolationNames.push_back("INTERPOLATION_CUBIC");
{
cv::Mat img, img_resize_perf;
for (size_t iter = 0; iter < interpolations.size(); iter ++) {
std::cout << "\nInterpolation (gray): " << interpolationNames[iter] << std::endl;
for (size_t s = 0; s < scales.size(); s++) {
unsigned int width_resize = static_cast<unsigned int>(I.getWidth() * scales[s]);
unsigned int height_resize = static_cast<unsigned int>(I.getHeight() * scales[s]);
cv::Size new_size(static_cast<int>(width_resize), static_cast<int>(height_resize));
std::cout << "Resize from " << I.getWidth() << "x" << I.getHeight() << " to "
<< width_resize << "x" << height_resize << std::endl;
for (int nbIter = 0; nbIter < 10; nbIter++) {
}
for (int nbIter = 0; nbIter < 10; nbIter++) {
cv::resize(img, img_resize_perf, new_size, 0.0, 0.0, interpolationsCV[iter]);
}
std::cout << "ViSP (10 iterations): " << t << " ms ; Mean: " << t / 10 << " ms" << std::endl;
std::cout << "OpenCV (10 iterations): " << t_cv << " ms ; Mean: " << t_cv / 10 << " ms" << std::endl;
double diff = 0.0, diff_abs = 0.0;
for (int i = 0; i < img_resize_perf.rows; i++) {
for (int j = 0; j < img_resize_perf.cols; j++) {
int d = img_resize_perf.at<uchar>(i, j) - I_resize_perf[i][j];
diff += d;
}
}
std::cout <<
"Mean diff: " << (diff / I_resize_perf.
getSize()) << std::endl;
std::cout <<
"Mean abs diff: " << (diff_abs / I_resize_perf.
getSize()) << std::endl;
}
}
}
{
cv::Mat img, img_resize_perf;
for (size_t iter = 0; iter < interpolations.size(); iter++) {
std::cout << "\nInterpolation (color): " << interpolationNames[iter] << std::endl;
for (size_t s = 0; s < scales.size(); s++) {
unsigned int width_resize = static_cast<unsigned int>(I.getWidth() * scales[s]);
unsigned int height_resize = static_cast<unsigned int>(I.getHeight() * scales[s]);
cv::Size new_size(static_cast<int>(width_resize), static_cast<int>(height_resize));
std::cout << "Resize from " << I_color.getWidth() << "x" << I_color.getHeight() << " to "
<< width_resize << "x" << height_resize << std::endl;
for (int nbIter = 0; nbIter < 10; nbIter++) {
}
for (int nbIter = 0; nbIter < 10; nbIter++) {
cv::resize(img, img_resize_perf, new_size, 0.0, 0.0, interpolationsCV[iter]);
}
std::cout << "ViSP (10 iterations): " << t << " ms ; Mean: " << t / 10 << " ms" << std::endl;
std::cout << "OpenCV (10 iterations): " << t_cv << " ms ; Mean: " << t_cv / 10 << " ms" << std::endl;
double diff = 0.0, diff_abs = 0.0;
for (int i = 0; i < img_resize_perf.rows; i++) {
for (int j = 0; j < img_resize_perf.cols; j++) {
int d = (img_resize_perf.at<cv::Vec3b>(i, j)[0] - I_resize_perf[i][j].B) +
(img_resize_perf.at<cv::Vec3b>(i, j)[1] - I_resize_perf[i][j].G) +
(img_resize_perf.at<cv::Vec3b>(i, j)[2] - I_resize_perf[i][j].R);
diff += d;
}
}
std::cout <<
"Mean diff: " << (diff / I_resize_perf.
getSize()) << std::endl;
std::cout <<
"Mean abs diff: " << (diff_abs / I_resize_perf.
getSize()) << std::endl;
}
}
}
#endif
}
return EXIT_SUCCESS;
std::cerr << "Catch an exception: " << e << std::endl;
return EXIT_FAILURE;
}
}