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