Visual Servoing Platform  version 3.6.1 under development (2024-11-24)
tutorial-connected-components.cpp
1 
3 #include <cstdlib>
4 #include <iostream>
5 #include <visp3/core/vpConfig.h>
6 #include <visp3/core/vpImage.h>
7 #include <visp3/gui/vpDisplayGDI.h>
8 #include <visp3/gui/vpDisplayOpenCV.h>
9 #include <visp3/gui/vpDisplayX.h>
10 #include <visp3/io/vpImageIo.h>
11 
12 #if defined(VISP_HAVE_MODULE_IMGPROC)
14 #include <visp3/imgproc/vpImgproc.h>
16 #endif
17 
18 int main(int argc, const char **argv)
19 {
21 #if defined(VISP_HAVE_MODULE_IMGPROC) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
23 
24 #ifdef ENABLE_VISP_NAMESPACE
25  using namespace VISP_NAMESPACE_NAME;
26 #endif
27 
28  std::string input_filename = "img.pgm";
30 
31  for (int i = 1; i < argc; i++) {
32  if (std::string(argv[i]) == "--input" && i + 1 < argc) {
33  input_filename = std::string(argv[i + 1]);
34  }
35  else if (std::string(argv[i]) == "--connexity" && i + 1 < argc) {
36  connexity = (vpImageMorphology::vpConnexityType)atoi(argv[i + 1]);
37  }
38  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
39  std::cout << "Usage: " << argv[0]
40  << " [--input <input image>] [--connexity <0: 4-connexity, "
41  "1: 8-connexity>] [--help]"
42  << std::endl;
43  return EXIT_SUCCESS;
44  }
45  }
46 
49  vpImageIo::read(I, input_filename);
51 
52 #ifdef VISP_HAVE_X11
53  vpDisplayX d, d2;
54 #elif defined(VISP_HAVE_GDI)
55  vpDisplayGDI d, d2;
56 #elif defined(HAVE_OPENCV_HIGHGUI)
57  vpDisplayOpenCV d, d2;
58 #endif
59  d.init(I, 0, 0, "Input image");
60 
62  vpImage<int> labels;
63  int nbComponents = 0;
64  VISP_NAMESPACE_NAME::connectedComponents(I, labels, nbComponents, connexity);
65  std::cout << "nbComponents=" << nbComponents << std::endl;
67 
69  vpImage<vpRGBa> I_conn(I.getHeight(), I.getWidth());
70  for (unsigned int i = 0; i < I_conn.getHeight(); i++) {
71  for (unsigned int j = 0; j < I_conn.getWidth(); j++) {
72  if (labels[i][j] != 0) {
73  I_conn[i][j] =
74  vpRGBa(vpColor::getColor((unsigned int)labels[i][j]).R, vpColor::getColor((unsigned int)labels[i][j]).G,
75  vpColor::getColor((unsigned int)labels[i][j]).B);
76  }
77  }
78  }
80  d2.init(I_conn, I.getWidth(), 10, "Connected components");
81 
83  vpDisplay::display(I_conn);
84  vpDisplay::displayText(I_conn, 20, 20, "Click to quit.", vpColor::red);
86  vpDisplay::flush(I_conn);
87  vpDisplay::getClick(I_conn);
88 #else
89  (void)argc;
90  (void)argv;
91 #endif
92  return EXIT_SUCCESS;
93 }
static vpColor getColor(const unsigned int &i)
Definition: vpColor.h:311
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="") VP_OVERRIDE
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
Definition: vpRGBa.h:65
VISP_EXPORT void connectedComponents(const VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, VISP_NAMESPACE_ADDRESSING vpImage< int > &labels, int &nbComponents, const VISP_NAMESPACE_ADDRESSING vpImageMorphology::vpConnexityType &connexity=VISP_NAMESPACE_ADDRESSING vpImageMorphology::CONNEXITY_4)