Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
testImageResize.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test image resize.
32  */
33 
40 #include <visp3/core/vpConfig.h>
41 
42 #if defined(VISP_HAVE_CATCH2)
43 #define CATCH_CONFIG_RUNNER
44 #include "common.hpp"
45 #include <catch.hpp>
46 #include <visp3/core/vpImageTools.h>
47 
48 static unsigned int g_input_width = 7;
49 static unsigned int g_input_height = 5;
50 static unsigned int g_output_width = 4;
51 static unsigned int g_output_height = 3;
52 
53 #ifdef ENABLE_VISP_NAMESPACE
54 using namespace VISP_NAMESPACE_NAME;
55 #endif
56 TEST_CASE("Nearest neighbor interpolation", "[image_resize]")
57 {
58  SECTION("unsigned char")
59  {
60  vpImage<unsigned char> I(g_input_height, g_input_width);
61  common_tools::fill(I);
62  vpImage<unsigned char> Iresize_ref(g_output_height, g_output_width);
63  common_tools::resizeRef(I, Iresize_ref, common_tools::g_nearest_neighbor);
64 
65  vpImage<unsigned char> Iresize;
66  vpImageTools::resize(I, Iresize, g_output_width, g_output_height, vpImageTools::INTERPOLATION_NEAREST);
67 
68  std::cout << "I:\n" << I << std::endl;
69  std::cout << "Iresize_ref:\n" << Iresize_ref << std::endl;
70  std::cout << "Iresize:\n" << Iresize << std::endl;
71 
72  CHECK((Iresize == Iresize_ref));
73  }
74 
75  SECTION("vpRGBa")
76  {
77  vpImage<vpRGBa> I(g_input_height, g_input_width);
78  common_tools::fill(I);
79  vpImage<vpRGBa> Iresize_ref(g_output_height, g_output_width);
80  common_tools::resizeRef(I, Iresize_ref, common_tools::g_nearest_neighbor);
81 
82  vpImage<vpRGBa> Iresize;
83  vpImageTools::resize(I, Iresize, g_output_width, g_output_height, vpImageTools::INTERPOLATION_NEAREST);
84 
85  std::cout << "I:\n" << I << std::endl;
86  std::cout << "Iresize_ref:\n" << Iresize_ref << std::endl;
87  std::cout << "Iresize:\n" << Iresize << std::endl;
88 
89  CHECK((Iresize == Iresize_ref));
90  }
91 }
92 
93 TEST_CASE("Bilinear interpolation", "[image_resize]")
94 {
95  SECTION("unsigned char")
96  {
97  vpImage<unsigned char> I(g_input_height, g_input_width);
98  common_tools::fill(I);
99  vpImage<unsigned char> Iresize_ref(g_output_height, g_output_width);
100  common_tools::resizeRef(I, Iresize_ref, common_tools::g_bilinear);
101 
102  vpImage<unsigned char> Iresize;
103  vpImageTools::resize(I, Iresize, g_output_width, g_output_height, vpImageTools::INTERPOLATION_LINEAR);
104 
105  std::cout << "I:\n" << I << std::endl;
106  std::cout << "Iresize_ref:\n" << Iresize_ref << std::endl;
107  std::cout << "Iresize:\n" << Iresize << std::endl;
108 
109  CHECK((Iresize == Iresize_ref));
110  }
111 
112  SECTION("vpRGBa")
113  {
114  vpImage<vpRGBa> I(g_input_height, g_input_width);
115  common_tools::fill(I);
116  vpImage<vpRGBa> Iresize_ref(g_output_height, g_output_width);
117  common_tools::resizeRef(I, Iresize_ref, common_tools::g_bilinear);
118 
119  vpImage<vpRGBa> Iresize;
120  vpImageTools::resize(I, Iresize, g_output_width, g_output_height, vpImageTools::INTERPOLATION_LINEAR);
121 
122  std::cout << "I:\n" << I << std::endl;
123  std::cout << "Iresize_ref:\n" << Iresize_ref << std::endl;
124  std::cout << "Iresize:\n" << Iresize << std::endl;
125 
126  const double max_pixel_error = 0.5;
127  double error = 0.0;
128  CHECK(common_tools::almostEqual(Iresize, Iresize_ref, max_pixel_error, error));
129  std::cout << "Error: " << error << std::endl;
130  }
131 }
132 
133 int main(int argc, char *argv[])
134 {
135  Catch::Session session; // There must be exactly one instance
136 
137  // Build a new parser on top of Catch's
138  using namespace Catch::clara;
139  auto cli = session.cli() // Get Catch's composite command line parser
140  | Opt(g_input_width, "g_input_width") // bind variable to a new option, with a hint string
141  ["--iw"] // the option names it will respond to
142  ("Input image width.") // description string for the help output
143  | Opt(g_input_height, "g_input_height") // bind variable to a new option, with a hint string
144  ["--ih"] // the option names it will respond to
145  ("Input image height.") |
146  Opt(g_output_width, "g_output_width") // bind variable to a new option, with a hint string
147  ["--ow"] // the option names it will respond to
148  ("Output image width.") |
149  Opt(g_output_height, "g_output_height") // bind variable to a new option, with a hint string
150  ["--oh"] // the option names it will respond to
151  ("Output image height.");
152 
153 // Now pass the new composite back to Catch so it uses that
154  session.cli(cli);
155 
156  // Let Catch (using Clara) parse the command line
157  session.applyCommandLine(argc, argv);
158 
159  std::cout << "Input image (wxh): " << g_input_width << "x" << g_input_height << std::endl;
160  std::cout << "Output image (wxh): " << g_output_width << "x" << g_output_height << std::endl;
161 
162  int numFailed = session.run();
163 
164  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
165  // This clamping has already been applied, so just return it here
166  // You can also do any post run clean-up here
167  return numFailed;
168 }
169 #else
170 int main() { return EXIT_SUCCESS; }
171 #endif
static void resize(const vpImage< Type > &I, vpImage< Type > &Ires, unsigned int width, unsigned int height, const vpImageInterpolationType &method=INTERPOLATION_NEAREST, unsigned int nThreads=0)
@ INTERPOLATION_LINEAR
Definition: vpImageTools.h:81
@ INTERPOLATION_NEAREST
Definition: vpImageTools.h:80