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