Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testConnectedComponents.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2023 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 * Test connected components.
32 */
33#include <map>
34#include <set>
35#include <visp3/core/vpImageTools.h>
36#include <visp3/core/vpIoTools.h>
37#include <visp3/imgproc/vpImgproc.h>
38#include <visp3/io/vpImageIo.h>
39#include <visp3/io/vpParseArgv.h>
40
47// List of allowed command line options
48#define GETOPTARGS "cdi:o:h"
49
50void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user);
51bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user);
52bool checkLabels(const vpImage<int> &label1, const vpImage<int> &label2);
53
54/*
55 Print the program options.
56
57 \param name : Program name.
58 \param badparam : Bad parameter name.
59 \param ipath: Input image path.
60 \param opath : Output image path.
61 \param user : Username.
62 */
63void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
64{
65 fprintf(stdout, "\n\
66Test connected components.\n\
67\n\
68SYNOPSIS\n\
69 %s [-i <input image path>] [-o <output image path>]\n\
70 [-h]\n \
71",
72 name);
73
74 fprintf(stdout, "\n\
75OPTIONS: Default\n\
76 -i <input image path> %s\n\
77 Set image input path.\n\
78 From this path read \"Klimt/Klimt.pgm\"\n\
79 image.\n\
80 Setting the VISP_INPUT_IMAGE_PATH environment\n\
81 variable produces the same behaviour than using\n\
82 this option.\n\
83\n\
84 -o <output image path> %s\n\
85 Set image output path.\n\
86 From this directory, creates the \"%s\"\n\
87 subdirectory depending on the username, where \n\
88 output result images are written.\n\
89\n\
90 -h\n\
91 Print the help.\n\n",
92 ipath.c_str(), opath.c_str(), user.c_str());
93
94 if (badparam)
95 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
96}
97
109bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user)
110{
111 const char *optarg_;
112 int c;
113 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
114
115 switch (c) {
116 case 'i':
117 ipath = optarg_;
118 break;
119 case 'o':
120 opath = optarg_;
121 break;
122 case 'h':
123 usage(argv[0], NULL, ipath, opath, user);
124 return false;
125 break;
126
127 case 'c':
128 case 'd':
129 break;
130
131 default:
132 usage(argv[0], optarg_, ipath, opath, user);
133 return false;
134 break;
135 }
136 }
137
138 if ((c == 1) || (c == -1)) {
139 // standalone param or error
140 usage(argv[0], NULL, ipath, opath, user);
141 std::cerr << "ERROR: " << std::endl;
142 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
143 return false;
144 }
145
146 return true;
147}
148
149bool checkLabels(const vpImage<int> &label1, const vpImage<int> &label2)
150{
151 if (label1.getHeight() != label2.getHeight() || label1.getWidth() != label2.getWidth())
152 return false;
153
154 std::map<int, std::vector<vpImagePoint> > map_label1, map_label2;
155 for (unsigned int i = 0; i < label1.getHeight(); i++) {
156 for (unsigned int j = 0; j < label1.getWidth(); j++) {
157 if ((label1[i][j] > 0 && label2[i][j] == 0) || (label1[i][j] == 0 && label2[i][j] > 0)) {
158 std::cerr << "label1[i][j] > 0 && label2[i][j] == 0 || label1[i][j] "
159 "== 0 && label2[i][j] > 0"
160 << std::endl;
161 return false;
162 }
163
164 if (label1[i][j])
165 map_label1[label1[i][j]].push_back(vpImagePoint(i, j));
166
167 if (label2[i][j])
168 map_label2[label2[i][j]].push_back(vpImagePoint(i, j));
169 }
170 }
171
172 if (map_label1.size() != map_label2.size()) {
173 std::cerr << "map_label1.size() != map_label2.size()" << std::endl;
174 return false;
175 }
176
177 for (std::map<int, std::vector<vpImagePoint> >::const_iterator it1 = map_label1.begin(); it1 != map_label1.end();
178 ++it1) {
179 // Get corresponding label in the other method
180 unsigned int i = (unsigned int)it1->second.front().get_i(), j = (unsigned int)it1->second.front().get_j();
181 int lab2 = label2[i][j];
182
183 std::vector<vpImagePoint>::const_iterator it_pt1 = it1->second.begin();
184 for (; it_pt1 != it1->second.end(); ++it_pt1) {
185 i = (unsigned int)it_pt1->get_i();
186 j = (unsigned int)it_pt1->get_j();
187 if (label2[i][j] != lab2) {
188 std::cerr << "label2[i][j] != lab2" << std::endl;
189 return false;
190 }
191 }
192 }
193
194 return true;
195}
196
197int main(int argc, const char **argv)
198{
199 try {
200 std::string env_ipath;
201 std::string opt_ipath;
202 std::string opt_opath;
203 std::string ipath;
204 std::string opath;
205 std::string filename;
206 std::string username;
207
208 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
209 // environment variable value
211
212 // Set the default input path
213 if (!env_ipath.empty())
214 ipath = env_ipath;
215
216// Set the default output path
217#if defined(_WIN32)
218 opt_opath = "C:/temp";
219#else
220 opt_opath = "/tmp";
221#endif
222
223 // Get the user login name
224 vpIoTools::getUserName(username);
225
226 // Read the command line options
227 if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
228 exit(EXIT_FAILURE);
229 }
230
231 // Get the option values
232 if (!opt_ipath.empty())
233 ipath = opt_ipath;
234 if (!opt_opath.empty())
235 opath = opt_opath;
236
237 // Append to the output path string, the login name of the user
238 opath = vpIoTools::createFilePath(opath, username);
239
240 // Test if the output path exist. If no try to create it
241 if (vpIoTools::checkDirectory(opath) == false) {
242 try {
243 // Create the dirname
245 } catch (...) {
246 usage(argv[0], NULL, ipath, opt_opath, username);
247 std::cerr << std::endl << "ERROR:" << std::endl;
248 std::cerr << " Cannot create " << opath << std::endl;
249 std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
250 exit(EXIT_FAILURE);
251 }
252 }
253
254 // Compare ipath and env_ipath. If they differ, we take into account
255 // the input path comming from the command line option
256 if (!opt_ipath.empty() && !env_ipath.empty()) {
257 if (ipath != env_ipath) {
258 std::cout << std::endl << "WARNING: " << std::endl;
259 std::cout << " Since -i <visp image path=" << ipath << "> "
260 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
261 << " we skip the environment variable." << std::endl;
262 }
263 }
264
265 // Test if an input path is set
266 if (opt_ipath.empty() && env_ipath.empty()) {
267 usage(argv[0], NULL, ipath, opt_opath, username);
268 std::cerr << std::endl << "ERROR:" << std::endl;
269 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
270 << " environment variable to specify the location of the " << std::endl
271 << " image path where test images are located." << std::endl
272 << std::endl;
273 exit(EXIT_FAILURE);
274 }
275
276 //
277 // Here starts really the test
278 //
279
280 // Read Klimt.ppm
281 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
283 std::cout << "Read image: " << filename << std::endl;
284 vpImageIo::read(I, filename);
285 vpImageTools::binarise(I, (unsigned char)127, (unsigned char)255, (unsigned char)0, (unsigned char)255,
286 (unsigned char)255);
287 std::cout << "Image: " << I.getWidth() << "x" << I.getHeight() << std::endl;
288
289 vpImage<int> labels_connex4;
290 int nbComponents = 0;
291 double t = vpTime::measureTimeMs();
292 vp::connectedComponents(I, labels_connex4, nbComponents, vpImageMorphology::CONNEXITY_4);
293 t = vpTime::measureTimeMs() - t;
294 std::cout << "\n4-connexity connected components:" << std::endl;
295 std::cout << "Time: " << t << " ms" << std::endl;
296 std::cout << "nbComponents=" << nbComponents << std::endl;
297
298 vpImage<int> labels_connex8;
300 vp::connectedComponents(I, labels_connex8, nbComponents, vpImageMorphology::CONNEXITY_8);
301 t = vpTime::measureTimeMs() - t;
302 std::cout << "\n8-connexity connected components:" << std::endl;
303 std::cout << "Time: " << t << " ms" << std::endl;
304 std::cout << "nbComponents=" << nbComponents << std::endl;
305
306 // Save results
307 vpImage<vpRGBa> labels_connex4_color(labels_connex4.getHeight(), labels_connex4.getWidth(), vpRGBa(0, 0, 0, 0));
308 for (unsigned int i = 0; i < labels_connex4.getHeight(); i++) {
309 for (unsigned int j = 0; j < labels_connex4.getWidth(); j++) {
310 if (labels_connex4[i][j] != 0) {
311 labels_connex4_color[i][j] = vpRGBa(vpColor::getColor((unsigned int)labels_connex4[i][j]).R,
312 vpColor::getColor((unsigned int)labels_connex4[i][j]).G,
313 vpColor::getColor((unsigned int)labels_connex4[i][j]).B);
314 }
315 }
316 }
317
318 filename = vpIoTools::createFilePath(opath, "Klimt_connected_components_4.ppm");
319 vpImageIo::write(labels_connex4_color, filename);
320
321 vpImage<vpRGBa> labels_connex8_color(labels_connex8.getHeight(), labels_connex8.getWidth(), vpRGBa(0, 0, 0, 0));
322 for (unsigned int i = 0; i < labels_connex8.getHeight(); i++) {
323 for (unsigned int j = 0; j < labels_connex8.getWidth(); j++) {
324 if (labels_connex8[i][j] != 0) {
325 labels_connex8_color[i][j] = vpRGBa(vpColor::getColor((unsigned int)labels_connex8[i][j]).R,
326 vpColor::getColor((unsigned int)labels_connex8[i][j]).G,
327 vpColor::getColor((unsigned int)labels_connex8[i][j]).B);
328 }
329 }
330 }
331
332 filename = vpIoTools::createFilePath(opath, "Klimt_connected_components_8.ppm");
333 vpImageIo::write(labels_connex8_color, filename);
334
335#if (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_IMGPROC)
336 cv::Mat matImg;
337 vpImageConvert::convert(I, matImg);
338
339 cv::Mat matLabels_4;
340 double t_opencv = vpTime::measureTimeMs();
341 cv::connectedComponents(matImg, matLabels_4, 4);
342 t_opencv = vpTime::measureTimeMs() - t_opencv;
343
344 std::set<int> set_labels_connex4_opencv;
345 vpImage<int> labels_connex4_opencv((unsigned int)matLabels_4.rows, (unsigned int)matLabels_4.cols);
346 for (int i = 0; i < matLabels_4.rows; i++) {
347 for (int j = 0; j < matLabels_4.cols; j++) {
348 labels_connex4_opencv[i][j] = matLabels_4.at<int>(i, j);
349
350 if (matLabels_4.at<int>(i, j))
351 set_labels_connex4_opencv.insert(matLabels_4.at<int>(i, j));
352 }
353 }
354
355 std::cout << "\n4-connexity connected components (OpenCV):" << std::endl;
356 std::cout << "Time: " << t_opencv << " ms" << std::endl;
357 std::cout << "nb components: " << set_labels_connex4_opencv.size() << std::endl;
358 bool check_label = checkLabels(labels_connex4_opencv, labels_connex4);
359 std::cout << "checkLabels(labels_connex4_opencv, labels_connex4): " << check_label << std::endl;
360 // std::cout << "(labels_connex4_opencv == labels_connex4)? " <<
361 // (labels_connex4_opencv == labels_connex4) << std::endl;
362 if (!check_label) {
363 throw vpException(vpException::fatalError, "(labels_connex4_opencv != labels_connex4)");
364 }
365
366 cv::Mat matLabels_8;
367 t_opencv = vpTime::measureTimeMs();
368 cv::connectedComponents(matImg, matLabels_8, 8);
369 t_opencv = vpTime::measureTimeMs() - t_opencv;
370
371 std::set<int> set_labels_connex8_opencv;
372 vpImage<int> labels_connex8_opencv((unsigned int)matLabels_8.rows, (unsigned int)matLabels_8.cols);
373 for (int i = 0; i < matLabels_8.rows; i++) {
374 for (int j = 0; j < matLabels_8.cols; j++) {
375 labels_connex8_opencv[i][j] = matLabels_8.at<int>(i, j);
376
377 if (matLabels_8.at<int>(i, j))
378 set_labels_connex8_opencv.insert(matLabels_8.at<int>(i, j));
379 }
380 }
381
382 std::cout << "\n8-connexity connected components (OpenCV):" << std::endl;
383 std::cout << "nb components: " << set_labels_connex8_opencv.size() << std::endl;
384 std::cout << "Time: " << t_opencv << " ms" << std::endl;
385 check_label = checkLabels(labels_connex8_opencv, labels_connex8);
386 std::cout << "checkLabels(labels_connex8_opencv, labels_connex8): " << check_label << std::endl;
387 // std::cout << "(labels_connex8_opencv == labels_connex8)? " <<
388 // (labels_connex8_opencv == labels_connex8) << std::endl;
389
390 if (!check_label) {
391 throw vpException(vpException::fatalError, "(labels_connex8_opencv != labels_connex8)");
392 }
393#endif
394
395 return EXIT_SUCCESS;
396 } catch (const vpException &e) {
397 std::cerr << "Catch an exception: " << e.what() << std::endl;
398 return EXIT_FAILURE;
399 }
400}
static vpColor getColor(const unsigned int &i)
Definition vpColor.h:307
error that can be emitted by ViSP classes.
Definition vpException.h:59
@ fatalError
Fatal error.
Definition vpException.h:84
const char * what() const
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 write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
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:135
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getHeight() const
Definition vpImage.h:184
static std::string getViSPImagesDataPath()
static bool checkDirectory(const std::string &dirname)
static std::string getUserName()
static std::string createFilePath(const std::string &parent, const std::string &child)
static void makeDirectory(const std::string &dirname)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
VISP_EXPORT void connectedComponents(const vpImage< unsigned char > &I, vpImage< int > &labels, int &nbComponents, const vpImageMorphology::vpConnexityType &connexity=vpImageMorphology::CONNEXITY_4)
VISP_EXPORT double measureTimeMs()