Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
perfImageMorphology.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 image morphology.
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 "common.hpp"
44 #include <visp3/core/vpImageMorphology.h>
45 #include <visp3/core/vpImageTools.h>
46 #include <visp3/core/vpIoTools.h>
47 #include <visp3/io/vpImageIo.h>
48 
49 static std::string ipath = vpIoTools::getViSPImagesDataPath();
50 
51 TEST_CASE("Benchmark binary image morphology", "[benchmark]")
52 {
53  std::string imagePath = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
55  vpImageIo::read(I, imagePath);
56 
57  vpImage<unsigned char> I_Klimt_binarized = I;
58  vpImageTools::binarise(I_Klimt_binarized, (unsigned char)127, (unsigned char)127, (unsigned char)0, (unsigned char)1,
59  (unsigned char)1, true);
60 
61  SECTION("Dilatation")
62  {
63  SECTION("4-connexity")
64  {
66  BENCHMARK("Benchmark dilatation (naive code)")
67  {
68  common_tools::imageDilatationRef(I_Klimt_binarized, connexity);
69  return I_Klimt_binarized;
70  };
71 
72  BENCHMARK("Benchmark dilatation (ViSP)")
73  {
74  vpImageMorphology::dilatation(I_Klimt_binarized, (unsigned char)1, (unsigned char)0, connexity);
75  return I_Klimt_binarized;
76  };
77  }
78 
79  SECTION("8-connexity")
80  {
82  BENCHMARK("Benchmark dilatation (naive code)")
83  {
84  common_tools::imageDilatationRef(I_Klimt_binarized, connexity);
85  return I_Klimt_binarized;
86  };
87 
88  BENCHMARK("Benchmark dilatation (ViSP)")
89  {
90  vpImageMorphology::dilatation(I_Klimt_binarized, (unsigned char)1, (unsigned char)0, connexity);
91  return I_Klimt_binarized;
92  };
93  }
94  }
95 
96  SECTION("Erosion")
97  {
98  SECTION("4-connexity")
99  {
101  BENCHMARK("Benchmark erosion (naive code)")
102  {
103  common_tools::imageErosionRef(I_Klimt_binarized, connexity);
104  return I_Klimt_binarized;
105  };
106 
107  BENCHMARK("Benchmark erosion (ViSP)")
108  {
109  vpImageMorphology::erosion(I_Klimt_binarized, (unsigned char)1, (unsigned char)0, connexity);
110  return I_Klimt_binarized;
111  };
112  }
113 
114  SECTION("8-connexity")
115  {
117  BENCHMARK("Benchmark erosion (naive code)")
118  {
119  common_tools::imageErosionRef(I_Klimt_binarized, connexity);
120  return I_Klimt_binarized;
121  };
122 
123  BENCHMARK("Benchmark erosion (ViSP)")
124  {
125  vpImageMorphology::erosion(I_Klimt_binarized, (unsigned char)1, (unsigned char)0, connexity);
126  return I_Klimt_binarized;
127  };
128  }
129  }
130 }
131 
132 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGPROC)
133 TEST_CASE("Benchmark gray image morphology", "[benchmark]")
134 {
135  std::string imagePath = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
137  vpImageIo::read(I, imagePath);
138 
139  cv::Mat img, imgMorph;
140  vpImageConvert::convert(I, img);
141  vpImageConvert::convert(I, imgMorph);
142  cv::Mat cross_SE = cv::getStructuringElement(cv::MORPH_CROSS, cv::Size(3, 3));
143  cv::Mat rect_SE = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
144 
145  SECTION("Dilatation")
146  {
147  SECTION("4-connexity")
148  {
150  BENCHMARK("Benchmark dilatation (naive code)")
151  {
152  common_tools::imageDilatationRef(I, connexity);
153  return I;
154  };
155 
156  BENCHMARK("Benchmark dilatation (ViSP)")
157  {
158  vpImageMorphology::dilatation<unsigned char>(I, connexity);
159  return I;
160  };
161 
162  BENCHMARK("Benchmark dilatation (OpenCV)")
163  {
164  cv::morphologyEx(imgMorph, imgMorph, cv::MORPH_DILATE, cross_SE);
165  return I;
166  };
167  }
168 
169  SECTION("8-connexity")
170  {
172  BENCHMARK("Benchmark dilatation (naive code)")
173  {
174  common_tools::imageDilatationRef(I, connexity);
175  return I;
176  };
177 
178  BENCHMARK("Benchmark dilatation (ViSP)")
179  {
180  vpImageMorphology::dilatation<unsigned char>(I, connexity);
181  return I;
182  };
183 
184  BENCHMARK("Benchmark dilatation (OpenCV)")
185  {
186  cv::morphologyEx(imgMorph, imgMorph, cv::MORPH_DILATE, rect_SE);
187  return I;
188  };
189  }
190  }
191 
192  SECTION("Erosion")
193  {
194  SECTION("4-connexity")
195  {
197  BENCHMARK("Benchmark erosion (naive code)")
198  {
199  common_tools::imageErosionRef(I, connexity);
200  return I;
201  };
202 
203  BENCHMARK("Benchmark erosion (ViSP)")
204  {
205  vpImageMorphology::erosion<unsigned char>(I, connexity);
206  return I;
207  };
208 
209  BENCHMARK("Benchmark dilatation (OpenCV)")
210  {
211  cv::morphologyEx(imgMorph, imgMorph, cv::MORPH_ERODE, cross_SE);
212  return I;
213  };
214  }
215 
216  SECTION("8-connexity")
217  {
219  BENCHMARK("Benchmark erosion (naive code)")
220  {
221  common_tools::imageErosionRef(I, connexity);
222  return I;
223  };
224 
225  BENCHMARK("Benchmark erosion (ViSP)")
226  {
227  vpImageMorphology::erosion<unsigned char>(I, connexity);
228  return I;
229  };
230 
231  BENCHMARK("Benchmark dilatation (OpenCV)")
232  {
233  cv::morphologyEx(imgMorph, imgMorph, cv::MORPH_ERODE, rect_SE);
234  return I;
235  };
236  }
237  }
238 }
239 #endif
240 
241 int main(int argc, char *argv [])
242 {
243  Catch::Session session; // There must be exactly one instance
244 
245  bool runBenchmark = false;
246  // Build a new parser on top of Catch's
247  using namespace Catch::clara;
248  auto cli = session.cli() // Get Catch's composite command line parser
249  | Opt(runBenchmark) // bind variable to a new option, with a hint string
250  ["--benchmark"] // the option names it will respond to
251  ("run benchmark?"); // description string for the help output
252 
253  // Now pass the new composite back to Catch so it uses that
254  session.cli(cli);
255 
256  // Let Catch (using Clara) parse the command line
257  session.applyCommandLine(argc, argv);
258 
259  if (runBenchmark) {
260  int numFailed = session.run();
261 
262  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
263  // This clamping has already been applied, so just return it here
264  // You can also do any post run clean-up here
265  return numFailed;
266  }
267 
268  return EXIT_SUCCESS;
269 }
270 #else
271 #include <iostream>
272 
273 int main() { return EXIT_SUCCESS; }
274 #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)
Definition: vpImageIo.cpp:143
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: vpImageTools.h:462
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1786
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:2147