Visual Servoing Platform version 3.5.0
perfImageResize.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 * Benchmark image resize.
33 *
34 *****************************************************************************/
35
36#include <visp3/core/vpConfig.h>
37
38#ifdef VISP_HAVE_CATCH2
39#define CATCH_CONFIG_ENABLE_BENCHMARKING
40#define CATCH_CONFIG_RUNNER
41#include <catch.hpp>
42
43#include <thread>
44#include <visp3/core/vpIoTools.h>
45#include <visp3/core/vpImageTools.h>
46#include <visp3/io/vpImageIo.h>
47#include "common.hpp"
48
49static const std::string ipath = vpIoTools::getViSPImagesDataPath();
50static std::string imagePathColor = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
51static std::string imagePathGray = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
52static unsigned int g_resize_width = 293;
53static unsigned int g_resize_height = 137;
54
55TEST_CASE("Nearest Neighbor image resize (naive code)", "[benchmark]") {
56 SECTION("unsigned char")
57 {
58 vpImage<unsigned char> I, Iresize(g_resize_height, g_resize_width);
59 vpImageIo::read(I, imagePathGray);
60
61 BENCHMARK("Benchmark Nearest Neighbor uchar image resize (naive code)") {
62 common_tools::resizeRef(I, Iresize, common_tools::g_nearest_neighbor);
63 return Iresize;
64 };
65 }
66
67 SECTION("vpRGBa")
68 {
69 vpImage<vpRGBa> I, Iresize(g_resize_height, g_resize_width);
70 vpImageIo::read(I, imagePathColor);
71
72 BENCHMARK("Benchmark Nearest Neighbor RGBa image resize (naive code)") {
73 common_tools::resizeRef(I, Iresize, common_tools::g_nearest_neighbor);
74 return Iresize;
75 };
76 }
77}
78
79TEST_CASE("Nearest Neighbor image resize (ViSP)", "[benchmark]") {
80 SECTION("unsigned char")
81 {
82 vpImage<unsigned char> I, Iresize(g_resize_height, g_resize_width);
83 vpImageIo::read(I, imagePathGray);
84
85 BENCHMARK("Benchmark Nearest Neighbor uchar image resize (ViSP) (1 thread)") {
87 return Iresize;
88 };
89
90 const unsigned int nThreads = std::thread::hardware_concurrency();
91 char buffer[256];
92 sprintf(buffer, "Benchmark Nearest Neighbor uchar image resize (ViSP) (%d threads)", nThreads);
93 BENCHMARK(buffer) {
95 return Iresize;
96 };
97 }
98
99 SECTION("vpRGBa")
100 {
101 vpImage<vpRGBa> I, Iresize(g_resize_height, g_resize_width);
102 vpImageIo::read(I, imagePathColor);
103
104 BENCHMARK("Benchmark Nearest Neighbor RGBa image resize (ViSP) (1 thread)") {
106 return Iresize;
107 };
108
109 const unsigned int nThreads = std::thread::hardware_concurrency();
110 char buffer[256];
111 sprintf(buffer, "Benchmark Nearest Neighbor RGBa image resize (ViSP) (%d threads)", nThreads);
112 BENCHMARK(buffer) {
114 return Iresize;
115 };
116 }
117}
118
119TEST_CASE("Bilinear image resize (naive code)", "[benchmark]") {
120 SECTION("unsigned char")
121 {
122 vpImage<unsigned char> I, Iresize(g_resize_height, g_resize_width);
123 vpImageIo::read(I, imagePathGray);
124
125 BENCHMARK("Benchmark Bilinear uchar image resize (naive code)") {
126 common_tools::resizeRef(I, Iresize, common_tools::g_bilinear);
127 return Iresize;
128 };
129 }
130
131 SECTION("vpRGBa")
132 {
133 vpImage<vpRGBa> I, Iresize(g_resize_height, g_resize_width);
134 vpImageIo::read(I, imagePathColor);
135
136 BENCHMARK("Benchmark Bilinear RGBa image resize (naive code)") {
137 common_tools::resizeRef(I, Iresize, common_tools::g_bilinear);
138 return Iresize;
139 };
140 }
141}
142
143TEST_CASE("Bilinear image resize (ViSP)", "[benchmark]") {
144 SECTION("unsigned char")
145 {
146 vpImage<unsigned char> I, Iresize(g_resize_height, g_resize_width);
147 vpImageIo::read(I, imagePathGray);
148
149 BENCHMARK("Benchmark Bilinear uchar image resize (ViSP)") {
151 return Iresize;
152 };
153 }
154
155 SECTION("vpRGBa")
156 {
157 vpImage<vpRGBa> I, Iresize(g_resize_height, g_resize_width);
158 vpImageIo::read(I, imagePathColor);
159
160 BENCHMARK("Benchmark Bilinear RGBa image resize (ViSP)") {
162 return Iresize;
163 };
164 }
165}
166
167TEST_CASE("Area image resize (ViSP)", "[benchmark]") {
168 SECTION("unsigned char")
169 {
170 vpImage<unsigned char> I, Iresize(g_resize_height, g_resize_width);
171 vpImageIo::read(I, imagePathGray);
172
173 BENCHMARK("Benchmark Area uchar image resize (ViSP)") {
175 return Iresize;
176 };
177 }
178
179 SECTION("vpRGBa")
180 {
181 vpImage<vpRGBa> I, Iresize(g_resize_height, g_resize_width);
182 vpImageIo::read(I, imagePathColor);
183
184 BENCHMARK("Benchmark Area RGBa image resize (ViSP)") {
186 return Iresize;
187 };
188 }
189}
190
191TEST_CASE("Bicubic image resize (ViSP)", "[benchmark]") {
192 SECTION("unsigned char")
193 {
194 vpImage<unsigned char> I, Iresize(g_resize_height, g_resize_width);
195 vpImageIo::read(I, imagePathGray);
196
197 BENCHMARK("Benchmark Bicubic uchar image resize (ViSP) (1 thread)") {
199 return Iresize;
200 };
201
202 const unsigned int nThreads = std::thread::hardware_concurrency();
203 char buffer[256];
204 sprintf(buffer, "Benchmark Bicubic uchar image resize (ViSP) (%d threads)", nThreads);
205 BENCHMARK(buffer) {
207 return Iresize;
208 };
209 }
210
211 SECTION("vpRGBa")
212 {
213 vpImage<vpRGBa> I, Iresize(g_resize_height, g_resize_width);
214 vpImageIo::read(I, imagePathColor);
215
216 BENCHMARK("Benchmark Bicubic RGBa image resize (ViSP) (1 thread)") {
218 return Iresize;
219 };
220
221 const unsigned int nThreads = std::thread::hardware_concurrency();
222 char buffer[256];
223 sprintf(buffer, "Benchmark Bicubic RGBa image resize (ViSP) (%d threads)", nThreads);
224 BENCHMARK(buffer) {
226 return Iresize;
227 };
228 }
229}
230
231#if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
232TEST_CASE("Nearest Neighbor image resize (OpenCV)", "[benchmark]") {
233 SECTION("unsigned char")
234 {
235 cv::Mat img, img_resize;
236 img = cv::imread(imagePathGray, cv::IMREAD_GRAYSCALE);
237
238 BENCHMARK("Benchmark Nearest Neighbor uchar image resize (OpenCV)") {
239 cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_NEAREST);
240 return img_resize;
241 };
242 }
243
244 SECTION("BGR")
245 {
246 cv::Mat img, img_resize;
247 img = cv::imread(imagePathColor, cv::IMREAD_COLOR);
248
249 BENCHMARK("Benchmark Nearest Neighbor BGR image resize (OpenCV)") {
250 cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_NEAREST);
251 return img_resize;
252 };
253 }
254}
255
256TEST_CASE("Bilinear image resize (OpenCV)", "[benchmark]") {
257 SECTION("unsigned char")
258 {
259 cv::Mat img, img_resize;
260 img = cv::imread(imagePathGray, cv::IMREAD_GRAYSCALE);
261
262 BENCHMARK("Benchmark Bilinear uchar image resize (OpenCV)") {
263 cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_LINEAR);
264 return img_resize;
265 };
266 }
267
268 SECTION("BGR")
269 {
270 cv::Mat img, img_resize;
271 img = cv::imread(imagePathColor, cv::IMREAD_COLOR);
272
273 BENCHMARK("Benchmark Bilinear BGR image resize (OpenCV)") {
274 cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_LINEAR);
275 return img_resize;
276 };
277 }
278}
279
280TEST_CASE("Area image resize (OpenCV)", "[benchmark]") {
281 SECTION("unsigned char")
282 {
283 cv::Mat img, img_resize;
284 img = cv::imread(imagePathGray, cv::IMREAD_GRAYSCALE);
285
286 BENCHMARK("Benchmark Area uchar image resize (OpenCV)") {
287 cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_AREA);
288 return img_resize;
289 };
290 }
291
292 SECTION("BGR")
293 {
294 cv::Mat img, img_resize;
295 img = cv::imread(imagePathColor, cv::IMREAD_COLOR);
296
297 BENCHMARK("Benchmark Area BGR image resize (OpenCV)") {
298 cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_AREA);
299 return img_resize;
300 };
301 }
302}
303
304TEST_CASE("Bicubic image resize (OpenCV)", "[benchmark]") {
305 SECTION("unsigned char")
306 {
307 cv::Mat img, img_resize;
308 img = cv::imread(imagePathGray, cv::IMREAD_GRAYSCALE);
309
310 BENCHMARK("Benchmark Bicubic uchar image resize (OpenCV)") {
311 cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_CUBIC);
312 return img_resize;
313 };
314 }
315
316 SECTION("BGR")
317 {
318 cv::Mat img, img_resize;
319 img = cv::imread(imagePathColor, cv::IMREAD_COLOR);
320
321 BENCHMARK("Benchmark Bicubic BGR image resize (OpenCV)") {
322 cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_CUBIC);
323 return img_resize;
324 };
325 }
326}
327#endif
328
329int main(int argc, char *argv[])
330{
331 Catch::Session session; // There must be exactly one instance
332
333 bool runBenchmark = false;
334 // Build a new parser on top of Catch's
335 using namespace Catch::clara;
336 auto cli = session.cli() // Get Catch's composite command line parser
337 | Opt(runBenchmark) // bind variable to a new option, with a hint string
338 ["--benchmark"] // the option names it will respond to
339 ("run benchmark?") // description string for the help output
340 | Opt(imagePathColor, "imagePathColor")
341 ["--imagePathColor"]
342 ("Path to color image")
343 | Opt(imagePathGray, "imagePathColor")
344 ["--imagePathGray"]
345 | Opt(g_resize_width, "g_resize_width")
346 ["--width"]
347 ("Resize width")
348 | Opt(g_resize_height, "g_resize_height")
349 ["--height"]
350 ("Resize height");
351
352 // Now pass the new composite back to Catch so it uses that
353 session.cli(cli);
354
355 // Let Catch (using Clara) parse the command line
356 session.applyCommandLine(argc, argv);
357
358 if (runBenchmark) {
359 vpImage<vpRGBa> I_color;
360 vpImageIo::read(I_color, imagePathColor);
361 std::cout << "imagePathColor:\n\t" << imagePathColor << "\n\t" << I_color.getWidth() << "x" << I_color.getHeight() << std::endl;
362
364 vpImageIo::read(I_gray, imagePathGray);
365 std::cout << "imagePathGray:\n\t" << imagePathGray << "\n\t" << I_gray.getWidth() << "x" << I_gray.getHeight() << std::endl;
366 std::cout << "Resize to: " << g_resize_width << "x" << g_resize_height << std::endl;
367
368 int numFailed = session.run();
369
370 // numFailed is clamped to 255 as some unices only use the lower 8 bits.
371 // This clamping has already been applied, so just return it here
372 // You can also do any post run clean-up here
373 return numFailed;
374 }
375
376 return EXIT_SUCCESS;
377}
378#else
379#include <iostream>
380
381int main()
382{
383 return 0;
384}
385#endif
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:149
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
unsigned int getWidth() const
Definition: vpImage.h:246
unsigned int getHeight() const
Definition: vpImage.h:188
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1365
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1670