Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
perfImageMorphology.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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 morphology.
32 */
33
37
38#include <visp3/core/vpConfig.h>
39
40#if defined(VISP_HAVE_CATCH2)
41
42#include <catch_amalgamated.hpp>
43
44#include "common.hpp"
45#include <visp3/core/vpImageMorphology.h>
46#include <visp3/core/vpImageTools.h>
47#include <visp3/core/vpIoTools.h>
48#include <visp3/io/vpImageIo.h>
49
50#ifdef ENABLE_VISP_NAMESPACE
51using namespace VISP_NAMESPACE_NAME;
52#endif
53VP_ATTRIBUTE_NO_DESTROY static std::string ipath = vpIoTools::getViSPImagesDataPath();
54
55TEST_CASE("Benchmark binary image morphology", "[benchmark]")
56{
57 std::string imagePath = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
59 vpImageIo::read(I, imagePath);
60
61 vpImage<unsigned char> I_Klimt_binarized = I;
62 vpImageTools::binarise(I_Klimt_binarized, (unsigned char)127, (unsigned char)127, (unsigned char)0, (unsigned char)1,
63 (unsigned char)1, true);
64
65 SECTION("Dilatation")
66 {
67 SECTION("4-connexity")
68 {
70 BENCHMARK("Benchmark dilatation (naive code)")
71 {
72 common_tools::imageDilatationRef(I_Klimt_binarized, connexity);
73 return I_Klimt_binarized;
74 };
75
76 BENCHMARK("Benchmark dilatation (ViSP)")
77 {
78 vpImageMorphology::dilatation(I_Klimt_binarized, (unsigned char)1, (unsigned char)0, connexity);
79 return I_Klimt_binarized;
80 };
81 }
82
83 SECTION("8-connexity")
84 {
86 BENCHMARK("Benchmark dilatation (naive code)")
87 {
88 common_tools::imageDilatationRef(I_Klimt_binarized, connexity);
89 return I_Klimt_binarized;
90 };
91
92 BENCHMARK("Benchmark dilatation (ViSP)")
93 {
94 vpImageMorphology::dilatation(I_Klimt_binarized, (unsigned char)1, (unsigned char)0, connexity);
95 return I_Klimt_binarized;
96 };
97 }
98 }
99
100 SECTION("Erosion")
101 {
102 SECTION("4-connexity")
103 {
105 BENCHMARK("Benchmark erosion (naive code)")
106 {
107 common_tools::imageErosionRef(I_Klimt_binarized, connexity);
108 return I_Klimt_binarized;
109 };
110
111 BENCHMARK("Benchmark erosion (ViSP)")
112 {
113 vpImageMorphology::erosion(I_Klimt_binarized, (unsigned char)1, (unsigned char)0, connexity);
114 return I_Klimt_binarized;
115 };
116 }
117
118 SECTION("8-connexity")
119 {
121 BENCHMARK("Benchmark erosion (naive code)")
122 {
123 common_tools::imageErosionRef(I_Klimt_binarized, connexity);
124 return I_Klimt_binarized;
125 };
126
127 BENCHMARK("Benchmark erosion (ViSP)")
128 {
129 vpImageMorphology::erosion(I_Klimt_binarized, (unsigned char)1, (unsigned char)0, connexity);
130 return I_Klimt_binarized;
131 };
132 }
133 }
134}
135
136#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGPROC)
137TEST_CASE("Benchmark gray image morphology", "[benchmark]")
138{
139 std::string imagePath = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
141 vpImageIo::read(I, imagePath);
142
143 cv::Mat img, imgMorph;
145 vpImageConvert::convert(I, imgMorph);
146 cv::Mat cross_SE = cv::getStructuringElement(cv::MORPH_CROSS, cv::Size(3, 3));
147 cv::Mat rect_SE = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
148
149 SECTION("Dilatation")
150 {
151 SECTION("4-connexity")
152 {
154 BENCHMARK("Benchmark dilatation (naive code)")
155 {
156 common_tools::imageDilatationRef(I, connexity);
157 return I;
158 };
159
160 BENCHMARK("Benchmark dilatation (ViSP)")
161 {
163 return I;
164 };
165
166 BENCHMARK("Benchmark dilatation (OpenCV)")
167 {
168 cv::morphologyEx(imgMorph, imgMorph, cv::MORPH_DILATE, cross_SE);
169 return I;
170 };
171 }
172
173 SECTION("8-connexity")
174 {
176 BENCHMARK("Benchmark dilatation (naive code)")
177 {
178 common_tools::imageDilatationRef(I, connexity);
179 return I;
180 };
181
182 BENCHMARK("Benchmark dilatation (ViSP)")
183 {
185 return I;
186 };
187
188 BENCHMARK("Benchmark dilatation (OpenCV)")
189 {
190 cv::morphologyEx(imgMorph, imgMorph, cv::MORPH_DILATE, rect_SE);
191 return I;
192 };
193 }
194 }
195
196 SECTION("Erosion")
197 {
198 SECTION("4-connexity")
199 {
201 BENCHMARK("Benchmark erosion (naive code)")
202 {
203 common_tools::imageErosionRef(I, connexity);
204 return I;
205 };
206
207 BENCHMARK("Benchmark erosion (ViSP)")
208 {
210 return I;
211 };
212
213 BENCHMARK("Benchmark dilatation (OpenCV)")
214 {
215 cv::morphologyEx(imgMorph, imgMorph, cv::MORPH_ERODE, cross_SE);
216 return I;
217 };
218 }
219
220 SECTION("8-connexity")
221 {
223 BENCHMARK("Benchmark erosion (naive code)")
224 {
225 common_tools::imageErosionRef(I, connexity);
226 return I;
227 };
228
229 BENCHMARK("Benchmark erosion (ViSP)")
230 {
232 return I;
233 };
234
235 BENCHMARK("Benchmark dilatation (OpenCV)")
236 {
237 cv::morphologyEx(imgMorph, imgMorph, cv::MORPH_ERODE, rect_SE);
238 return I;
239 };
240 }
241 }
242}
243#endif
244
245int main(int argc, char *argv[])
246{
247 Catch::Session session;
248
249 bool runBenchmark = false;
250 auto cli = session.cli()
251 | Catch::Clara::Opt(runBenchmark)["--benchmark"]("run benchmark?");
252
253 session.cli(cli);
254 session.applyCommandLine(argc, argv);
255
256 if (runBenchmark) {
257 int numFailed = session.run();
258
259 return numFailed;
260 }
261
262 return EXIT_SUCCESS;
263}
264#else
265#include <iostream>
266
267int main() { return EXIT_SUCCESS; }
268#endif
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void dilatation(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
static void erosion(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
static void binarise(vpImage< Type > &I, Type threshold1, Type threshold2, Type value1, Type value2, Type value3, bool useLUT=true)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)