Visual Servoing Platform  version 3.6.1 under development (2024-10-09)
perfImageResize.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  * Benchmark image resize.
32  */
33 
38 #include <visp3/core/vpConfig.h>
39 
40 #if defined(VISP_HAVE_CATCH2) && defined(VISP_HAVE_THREADS)
41 
42 #include <catch_amalgamated.hpp>
43 
44 #include "common.hpp"
45 #include <thread>
46 #include <visp3/core/vpImageTools.h>
47 #include <visp3/core/vpIoTools.h>
48 #include <visp3/io/vpImageIo.h>
49 
50 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_IMGPROC)
51 #include <opencv2/imgcodecs.hpp>
52 #include <opencv2/imgproc/imgproc.hpp>
53 #endif
54 
55 #ifdef ENABLE_VISP_NAMESPACE
56 using namespace VISP_NAMESPACE_NAME;
57 #endif
58 static const std::string ipath = vpIoTools::getViSPImagesDataPath();
59 static std::string imagePathColor = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
60 static std::string imagePathGray = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
61 static unsigned int g_resize_width = 293;
62 static unsigned int g_resize_height = 137;
63 
64 TEST_CASE("Nearest Neighbor image resize (naive code)", "[benchmark]")
65 {
66  SECTION("unsigned char")
67  {
68  vpImage<unsigned char> I, Iresize(g_resize_height, g_resize_width);
69  vpImageIo::read(I, imagePathGray);
70 
71  BENCHMARK("Benchmark Nearest Neighbor uchar image resize (naive code)")
72  {
73  common_tools::resizeRef(I, Iresize, common_tools::g_nearest_neighbor);
74  return Iresize;
75  };
76  }
77 
78  SECTION("vpRGBa")
79  {
80  vpImage<vpRGBa> I, Iresize(g_resize_height, g_resize_width);
81  vpImageIo::read(I, imagePathColor);
82 
83  BENCHMARK("Benchmark Nearest Neighbor RGBa image resize (naive code)")
84  {
85  common_tools::resizeRef(I, Iresize, common_tools::g_nearest_neighbor);
86  return Iresize;
87  };
88  }
89 }
90 
91 TEST_CASE("Nearest Neighbor image resize (ViSP)", "[benchmark]")
92 {
93  SECTION("unsigned char")
94  {
95  vpImage<unsigned char> I, Iresize(g_resize_height, g_resize_width);
96  vpImageIo::read(I, imagePathGray);
97 
98  BENCHMARK("Benchmark Nearest Neighbor uchar image resize (ViSP) (1 thread)")
99  {
101  return Iresize;
102  };
103 
104  const unsigned int nThreads = std::thread::hardware_concurrency();
105  std::stringstream buffer;
106  buffer << "Benchmark Nearest Neighbor uchar image resize (ViSP) (" << nThreads << "threads)";
107  BENCHMARK(buffer.str().c_str())
108  {
110  return Iresize;
111  };
112  }
113 
114  SECTION("vpRGBa")
115  {
116  vpImage<vpRGBa> I, Iresize(g_resize_height, g_resize_width);
117  vpImageIo::read(I, imagePathColor);
118 
119  BENCHMARK("Benchmark Nearest Neighbor RGBa image resize (ViSP) (1 thread)")
120  {
122  return Iresize;
123  };
124 
125  const unsigned int nThreads = std::thread::hardware_concurrency();
126  std::stringstream buffer;
127  buffer << "Benchmark Nearest Neighbor RGBa image resize (ViSP) (" << nThreads << " threads)";
128  BENCHMARK(buffer.str().c_str())
129  {
131  return Iresize;
132  };
133  }
134 }
135 
136 TEST_CASE("Bilinear image resize (naive code)", "[benchmark]")
137 {
138  SECTION("unsigned char")
139  {
140  vpImage<unsigned char> I, Iresize(g_resize_height, g_resize_width);
141  vpImageIo::read(I, imagePathGray);
142 
143  BENCHMARK("Benchmark Bilinear uchar image resize (naive code)")
144  {
145  common_tools::resizeRef(I, Iresize, common_tools::g_bilinear);
146  return Iresize;
147  };
148  }
149 
150  SECTION("vpRGBa")
151  {
152  vpImage<vpRGBa> I, Iresize(g_resize_height, g_resize_width);
153  vpImageIo::read(I, imagePathColor);
154 
155  BENCHMARK("Benchmark Bilinear RGBa image resize (naive code)")
156  {
157  common_tools::resizeRef(I, Iresize, common_tools::g_bilinear);
158  return Iresize;
159  };
160  }
161 }
162 
163 TEST_CASE("Bilinear image resize (ViSP)", "[benchmark]")
164 {
165  SECTION("unsigned char")
166  {
167  vpImage<unsigned char> I, Iresize(g_resize_height, g_resize_width);
168  vpImageIo::read(I, imagePathGray);
169 
170  BENCHMARK("Benchmark Bilinear uchar image resize (ViSP)")
171  {
173  return Iresize;
174  };
175  }
176 
177  SECTION("vpRGBa")
178  {
179  vpImage<vpRGBa> I, Iresize(g_resize_height, g_resize_width);
180  vpImageIo::read(I, imagePathColor);
181 
182  BENCHMARK("Benchmark Bilinear RGBa image resize (ViSP)")
183  {
185  return Iresize;
186  };
187  }
188 }
189 
190 TEST_CASE("Area image resize (ViSP)", "[benchmark]")
191 {
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 Area uchar image resize (ViSP)")
198  {
200  return Iresize;
201  };
202  }
203 
204  SECTION("vpRGBa")
205  {
206  vpImage<vpRGBa> I, Iresize(g_resize_height, g_resize_width);
207  vpImageIo::read(I, imagePathColor);
208 
209  BENCHMARK("Benchmark Area RGBa image resize (ViSP)")
210  {
212  return Iresize;
213  };
214  }
215 }
216 
217 TEST_CASE("Bicubic image resize (ViSP)", "[benchmark]")
218 {
219  SECTION("unsigned char")
220  {
221  vpImage<unsigned char> I, Iresize(g_resize_height, g_resize_width);
222  vpImageIo::read(I, imagePathGray);
223 
224  BENCHMARK("Benchmark Bicubic uchar image resize (ViSP) (1 thread)")
225  {
227  return Iresize;
228  };
229 
230  const unsigned int nThreads = std::thread::hardware_concurrency();
231  std::stringstream buffer;
232  buffer << "Benchmark Bicubic uchar image resize (ViSP) (" << nThreads << " threads)";
233  BENCHMARK(buffer.str().c_str())
234  {
236  return Iresize;
237  };
238  }
239 
240  SECTION("vpRGBa")
241  {
242  vpImage<vpRGBa> I, Iresize(g_resize_height, g_resize_width);
243  vpImageIo::read(I, imagePathColor);
244 
245  BENCHMARK("Benchmark Bicubic RGBa image resize (ViSP) (1 thread)")
246  {
248  return Iresize;
249  };
250 
251  const unsigned int nThreads = std::thread::hardware_concurrency();
252  std::stringstream buffer;
253  buffer << "Benchmark Bicubic RGBa image resize (ViSP) (" << nThreads << " threads)";
254  BENCHMARK(buffer.str().c_str())
255  {
257  return Iresize;
258  };
259  }
260 }
261 
262 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_IMGPROC)
263 TEST_CASE("Nearest Neighbor image resize (OpenCV)", "[benchmark]")
264 {
265  SECTION("unsigned char")
266  {
267  cv::Mat img, img_resize;
268  img = cv::imread(imagePathGray, cv::IMREAD_GRAYSCALE);
269 
270  BENCHMARK("Benchmark Nearest Neighbor uchar image resize (OpenCV)")
271  {
272  cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_NEAREST);
273  return img_resize;
274  };
275  }
276 
277  SECTION("BGR")
278  {
279  cv::Mat img, img_resize;
280  img = cv::imread(imagePathColor, cv::IMREAD_COLOR);
281 
282  BENCHMARK("Benchmark Nearest Neighbor BGR image resize (OpenCV)")
283  {
284  cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_NEAREST);
285  return img_resize;
286  };
287  }
288 }
289 
290 TEST_CASE("Bilinear image resize (OpenCV)", "[benchmark]")
291 {
292  SECTION("unsigned char")
293  {
294  cv::Mat img, img_resize;
295  img = cv::imread(imagePathGray, cv::IMREAD_GRAYSCALE);
296 
297  BENCHMARK("Benchmark Bilinear uchar image resize (OpenCV)")
298  {
299  cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_LINEAR);
300  return img_resize;
301  };
302  }
303 
304  SECTION("BGR")
305  {
306  cv::Mat img, img_resize;
307  img = cv::imread(imagePathColor, cv::IMREAD_COLOR);
308 
309  BENCHMARK("Benchmark Bilinear BGR image resize (OpenCV)")
310  {
311  cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_LINEAR);
312  return img_resize;
313  };
314  }
315 }
316 
317 TEST_CASE("Area image resize (OpenCV)", "[benchmark]")
318 {
319  SECTION("unsigned char")
320  {
321  cv::Mat img, img_resize;
322  img = cv::imread(imagePathGray, cv::IMREAD_GRAYSCALE);
323 
324  BENCHMARK("Benchmark Area uchar image resize (OpenCV)")
325  {
326  cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_AREA);
327  return img_resize;
328  };
329  }
330 
331  SECTION("BGR")
332  {
333  cv::Mat img, img_resize;
334  img = cv::imread(imagePathColor, cv::IMREAD_COLOR);
335 
336  BENCHMARK("Benchmark Area BGR image resize (OpenCV)")
337  {
338  cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_AREA);
339  return img_resize;
340  };
341  }
342 }
343 
344 TEST_CASE("Bicubic image resize (OpenCV)", "[benchmark]")
345 {
346  SECTION("unsigned char")
347  {
348  cv::Mat img, img_resize;
349  img = cv::imread(imagePathGray, cv::IMREAD_GRAYSCALE);
350 
351  BENCHMARK("Benchmark Bicubic uchar image resize (OpenCV)")
352  {
353  cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_CUBIC);
354  return img_resize;
355  };
356  }
357 
358  SECTION("BGR")
359  {
360  cv::Mat img, img_resize;
361  img = cv::imread(imagePathColor, cv::IMREAD_COLOR);
362 
363  BENCHMARK("Benchmark Bicubic BGR image resize (OpenCV)")
364  {
365  cv::resize(img, img_resize, cv::Size(g_resize_width, g_resize_height), 0, 0, cv::INTER_CUBIC);
366  return img_resize;
367  };
368  }
369 }
370 #endif
371 
372 int main(int argc, char *argv[])
373 {
374  Catch::Session session;
375 
376  bool runBenchmark = false;
377  auto cli = session.cli()
378  | Catch::Clara::Opt(runBenchmark)["--benchmark"]("run benchmark?")
379  | Catch::Clara::Opt(imagePathColor, "imagePathColor")["--imagePathColor"]("Path to color image")
380  | Catch::Clara::Opt(imagePathGray, "imagePathColor")["--imagePathGray"]
381  | Catch::Clara::Opt(g_resize_width, "g_resize_width")["--width"]("Resize width")
382  | Catch::Clara::Opt(g_resize_height, "g_resize_height")["--height"]("Resize height");
383 
384  session.cli(cli);
385 
386  session.applyCommandLine(argc, argv);
387 
388  if (runBenchmark) {
389  vpImage<vpRGBa> I_color;
390  vpImageIo::read(I_color, imagePathColor);
391  std::cout << "imagePathColor:\n\t" << imagePathColor << "\n\t" << I_color.getWidth() << "x" << I_color.getHeight()
392  << std::endl;
393 
394  vpImage<unsigned char> I_gray;
395  vpImageIo::read(I_gray, imagePathGray);
396  std::cout << "imagePathGray:\n\t" << imagePathGray << "\n\t" << I_gray.getWidth() << "x" << I_gray.getHeight()
397  << std::endl;
398  std::cout << "Resize to: " << g_resize_width << "x" << g_resize_height << std::endl;
399 
400  int numFailed = session.run();
401 
402  return numFailed;
403  }
404 
405  return EXIT_SUCCESS;
406 }
407 #else
408 #include <iostream>
409 
410 int main() { return EXIT_SUCCESS; }
411 #endif
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
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
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427