Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
catchImageResize.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 
44 #include "common.hpp"
45 #include <catch_amalgamated.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;
136 
137  using namespace Catch::Clara;
138  auto cli = session.cli()
139  | Catch::Clara::Opt(g_input_width, "g_input_width")["--iw"]("Input image width.")
140  | Catch::Clara::Opt(g_input_height, "g_input_height")["--ih"]("Input image height.")
141  | Catch::Clara::Opt(g_output_width, "g_output_width")["--ow"]("Output image width.")
142  | Catch::Clara::Opt(g_output_height, "g_output_height")["--oh"]("Output image height.");
143 
144  session.cli(cli);
145  session.applyCommandLine(argc, argv);
146 
147  std::cout << "Input image (wxh): " << g_input_width << "x" << g_input_height << std::endl;
148  std::cout << "Output image (wxh): " << g_output_width << "x" << g_output_height << std::endl;
149 
150  int numFailed = session.run();
151  return numFailed;
152 }
153 #else
154 int main() { return EXIT_SUCCESS; }
155 #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