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