Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
perfGaussianFilter.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 Gaussian filter.
32  */
33 
38 #include <visp3/core/vpConfig.h>
39 
40 #if defined(VISP_HAVE_SIMDLIB) && defined(VISP_HAVE_CATCH2)
41 #define CATCH_CONFIG_ENABLE_BENCHMARKING
42 #define CATCH_CONFIG_RUNNER
43 #include <catch.hpp>
44 
45 #include <visp3/core/vpGaussianFilter.h>
46 #include <visp3/core/vpImageFilter.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 imagePath = vpIoTools::createFilePath(ipath, "faces/1280px-Solvay_conference_1927.png");
60 
61 TEST_CASE("vpGaussianFilter", "[benchmark]")
62 {
63  SECTION("unsigned char")
64  {
65  vpImage<unsigned char> I, I_blur;
66  vpImageIo::read(I, imagePath);
67 
68  const float sigma = 5.0f;
69  vpGaussianFilter gaussianFilter(I.getWidth(), I.getHeight(), sigma);
70  BENCHMARK("Benchmark vpGaussianFilter uchar")
71  {
72  gaussianFilter.apply(I, I_blur);
73  return I_blur;
74  };
75  }
76 
77  SECTION("vpRGBa")
78  {
79  vpImage<vpRGBa> I, I_blur;
80  vpImageIo::read(I, imagePath);
81 
82  const float sigma = 5.0f;
83  vpGaussianFilter gaussianFilter(I.getWidth(), I.getHeight(), sigma);
84  BENCHMARK("Benchmark vpGaussianFilter vpRGBa")
85  {
86  gaussianFilter.apply(I, I_blur);
87  return I_blur;
88  };
89  }
90 
91  SECTION("vpRGBa + deinterleave")
92  {
93  vpImage<vpRGBa> I, I_blur;
94  vpImageIo::read(I, imagePath);
95 
96  const float sigma = 5.0f;
97  const bool deinterleave = true;
98  vpGaussianFilter gaussianFilter(I.getWidth(), I.getHeight(), sigma, deinterleave);
99  BENCHMARK("Benchmark vpGaussianFilter vpRGBa")
100  {
101  gaussianFilter.apply(I, I_blur);
102  return I_blur;
103  };
104  }
105 }
106 
107 TEST_CASE("vpImageFilter::gaussianBlur", "[benchmark]")
108 {
109  SECTION("unsigned char")
110  {
112  vpImageIo::read(I, imagePath);
113 
114  vpImage<double> I_blur;
115  const unsigned int kernelSize = 7;
116  const double sigma = 5.0;
117  BENCHMARK("Benchmark vpImageFilter::gaussianBlur uchar")
118  {
119  vpImageFilter::gaussianBlur(I, I_blur, kernelSize, sigma);
120  return I_blur;
121  };
122  }
123 
124  SECTION("vpRGBa")
125  {
126  vpImage<vpRGBa> I, I_blur;
127  vpImageIo::read(I, imagePath);
128 
129  const unsigned int kernelSize = 7;
130  const double sigma = 5.0;
131  BENCHMARK("Benchmark vpImageFilter::gaussianBlur vpRGBa")
132  {
133  vpImageFilter::gaussianBlur(I, I_blur, kernelSize, sigma);
134  return I_blur;
135  };
136  }
137 }
138 
139 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS) && defined(HAVE_OPENCV_IMGPROC)
140 
141 TEST_CASE("Gaussian filter (OpenCV)", "[benchmark]")
142 {
143  SECTION("unsigned char")
144  {
145  cv::Mat img, img_blur;
146  img = cv::imread(imagePath, cv::IMREAD_GRAYSCALE);
147 
148  const double sigma = 5.0;
149  BENCHMARK("Benchmark Gaussian filter uchar (OpenCV)")
150  {
151  cv::GaussianBlur(img, img_blur, cv::Size(), sigma);
152  return img_blur;
153  };
154  }
155 
156  SECTION("BGR")
157  {
158  cv::Mat img, img_blur;
159  img = cv::imread(imagePath, cv::IMREAD_COLOR);
160 
161  const double sigma = 5.0;
162  BENCHMARK("Benchmark Gaussian filter BGR (OpenCV)")
163  {
164  cv::GaussianBlur(img, img_blur, cv::Size(), sigma);
165  return img_blur;
166  };
167  }
168 }
169 #endif
170 
171 int main(int argc, char *argv[])
172 {
173  Catch::Session session; // There must be exactly one instance
174 
175  bool runBenchmark = false;
176  // Build a new parser on top of Catch's
177  using namespace Catch::clara;
178  auto cli = session.cli() // Get Catch's composite command line parser
179  | Opt(runBenchmark) // bind variable to a new option, with a hint string
180  ["--benchmark"] // the option names it will respond to
181  ("run benchmark?") // description string for the help output
182  ;
183 
184 // Now pass the new composite back to Catch so it uses that
185  session.cli(cli);
186 
187  // Let Catch (using Clara) parse the command line
188  session.applyCommandLine(argc, argv);
189 
190  if (runBenchmark) {
191  int numFailed = session.run();
192 
193  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
194  // This clamping has already been applied, so just return it here
195  // You can also do any post run clean-up here
196  return numFailed;
197  }
198 
199  return EXIT_SUCCESS;
200 }
201 #else
202 #include <iostream>
203 
204 int main() { return EXIT_SUCCESS; }
205 #endif
Gaussian filter class.
static void gaussianBlur(const vpImage< ImageType > &I, vpImage< FilterType > &GI, unsigned int size=7, FilterType sigma=0., bool normalize=true, const vpImage< bool > *p_mask=nullptr)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
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