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