Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testImageResize.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 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 https://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 "common.hpp"
47#include <catch.hpp>
48#include <visp3/core/vpImageTools.h>
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{
57 SECTION("unsigned char")
58 {
59 vpImage<unsigned char> I(g_input_height, g_input_width);
60 common_tools::fill(I);
61 vpImage<unsigned char> Iresize_ref(g_output_height, g_output_width);
62 common_tools::resizeRef(I, Iresize_ref, common_tools::g_nearest_neighbor);
63
65 vpImageTools::resize(I, Iresize, g_output_width, g_output_height, vpImageTools::INTERPOLATION_NEAREST);
66
67 std::cout << "I:\n" << I << std::endl;
68 std::cout << "Iresize_ref:\n" << Iresize_ref << std::endl;
69 std::cout << "Iresize:\n" << Iresize << std::endl;
70
71 CHECK((Iresize == Iresize_ref));
72 }
73
74 SECTION("vpRGBa")
75 {
76 vpImage<vpRGBa> I(g_input_height, g_input_width);
77 common_tools::fill(I);
78 vpImage<vpRGBa> Iresize_ref(g_output_height, g_output_width);
79 common_tools::resizeRef(I, Iresize_ref, common_tools::g_nearest_neighbor);
80
81 vpImage<vpRGBa> Iresize;
82 vpImageTools::resize(I, Iresize, g_output_width, g_output_height, vpImageTools::INTERPOLATION_NEAREST);
83
84 std::cout << "I:\n" << I << std::endl;
85 std::cout << "Iresize_ref:\n" << Iresize_ref << std::endl;
86 std::cout << "Iresize:\n" << Iresize << std::endl;
87
88 CHECK((Iresize == Iresize_ref));
89 }
90}
91
92TEST_CASE("Bilinear interpolation", "[image_resize]")
93{
94 SECTION("unsigned char")
95 {
96 vpImage<unsigned char> I(g_input_height, g_input_width);
97 common_tools::fill(I);
98 vpImage<unsigned char> Iresize_ref(g_output_height, g_output_width);
99 common_tools::resizeRef(I, Iresize_ref, common_tools::g_bilinear);
100
102 vpImageTools::resize(I, Iresize, g_output_width, g_output_height, vpImageTools::INTERPOLATION_LINEAR);
103
104 std::cout << "I:\n" << I << std::endl;
105 std::cout << "Iresize_ref:\n" << Iresize_ref << std::endl;
106 std::cout << "Iresize:\n" << Iresize << std::endl;
107
108 CHECK((Iresize == Iresize_ref));
109 }
110
111 SECTION("vpRGBa")
112 {
113 vpImage<vpRGBa> I(g_input_height, g_input_width);
114 common_tools::fill(I);
115 vpImage<vpRGBa> Iresize_ref(g_output_height, g_output_width);
116 common_tools::resizeRef(I, Iresize_ref, common_tools::g_bilinear);
117
118 vpImage<vpRGBa> Iresize;
119 vpImageTools::resize(I, Iresize, g_output_width, g_output_height, vpImageTools::INTERPOLATION_LINEAR);
120
121 std::cout << "I:\n" << I << std::endl;
122 std::cout << "Iresize_ref:\n" << Iresize_ref << std::endl;
123 std::cout << "Iresize:\n" << Iresize << std::endl;
124
125 const double max_pixel_error = 0.5;
126 double error = 0.0;
127 CHECK(common_tools::almostEqual(Iresize, Iresize_ref, max_pixel_error, error));
128 std::cout << "Error: " << error << std::endl;
129 }
130}
131
132int main(int argc, char *argv[])
133{
134 Catch::Session session; // There must be exactly one instance
135
136 // Build a new parser on top of Catch's
137 using namespace Catch::clara;
138 auto cli = session.cli() // Get Catch's composite command line parser
139 | Opt(g_input_width, "g_input_width") // bind variable to a new option, with a hint string
140 ["--iw"] // the option names it will respond to
141 ("Input image width.") // description string for the help output
142 | Opt(g_input_height, "g_input_height") // bind variable to a new option, with a hint string
143 ["--ih"] // the option names it will respond to
144 ("Input image height.") |
145 Opt(g_output_width, "g_output_width") // bind variable to a new option, with a hint string
146 ["--ow"] // the option names it will respond to
147 ("Output image width.") |
148 Opt(g_output_height, "g_output_height") // bind variable to a new option, with a hint string
149 ["--oh"] // the option names it will respond to
150 ("Output image height.");
151
152 // Now pass the new composite back to Catch so it uses that
153 session.cli(cli);
154
155 // Let Catch (using Clara) parse the command line
156 session.applyCommandLine(argc, argv);
157
158 std::cout << "Input image (wxh): " << g_input_width << "x" << g_input_height << std::endl;
159 std::cout << "Output image (wxh): " << g_output_width << "x" << g_output_height << std::endl;
160
161 int numFailed = session.run();
162
163 // numFailed is clamped to 255 as some unices only use the lower 8 bits.
164 // This clamping has already been applied, so just return it here
165 // You can also do any post run clean-up here
166 return numFailed;
167}
168#else
169int main() { return EXIT_SUCCESS; }
170#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)
Definition of the vpImage class member functions.
Definition vpImage.h:135