Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
tutorial-connected-components.cpp
1 
3 #include <cstdlib>
4 #include <iostream>
5 #include <visp3/core/vpImage.h>
6 #include <visp3/gui/vpDisplayGDI.h>
7 #include <visp3/gui/vpDisplayOpenCV.h>
8 #include <visp3/gui/vpDisplayX.h>
9 #include <visp3/io/vpImageIo.h>
10 
11 #if defined(VISP_HAVE_MODULE_IMGPROC)
12 #include <visp3/imgproc/vpImgproc.h>
15 #endif
16 
17 int main(int argc, const char **argv)
18 {
20 #if defined(VISP_HAVE_MODULE_IMGPROC) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
21 
23  std::string input_filename = "img.pgm";
25 
26  for (int i = 1; i < argc; i++) {
27  if (std::string(argv[i]) == "--input" && i + 1 < argc) {
28  input_filename = std::string(argv[i + 1]);
29  } else if (std::string(argv[i]) == "--connexity" && i + 1 < argc) {
30  connexity = (vpImageMorphology::vpConnexityType)atoi(argv[i + 1]);
31  } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
32  std::cout << "Usage: " << argv[0]
33  << " [--input <input image>] [--connexity <0: 4-connexity, "
34  "1: 8-connexity>] [--help]"
35  << std::endl;
36  return EXIT_SUCCESS;
37  }
38  }
39 
42  vpImageIo::read(I, input_filename);
44 
45 #ifdef VISP_HAVE_X11
46  vpDisplayX d, d2;
47 #elif defined(VISP_HAVE_GDI)
48  vpDisplayGDI d, d2;
49 #elif defined(VISP_HAVE_OPENCV)
50  vpDisplayOpenCV d, d2;
51 #endif
52  d.init(I, 0, 0, "Input image");
53 
55  vpImage<int> labels;
56  int nbComponents = 0;
57  vp::connectedComponents(I, labels, nbComponents, connexity);
58  std::cout << "nbComponents=" << nbComponents << std::endl;
60 
62  vpImage<vpRGBa> I_conn(I.getHeight(), I.getWidth());
63  for (unsigned int i = 0; i < I_conn.getHeight(); i++) {
64  for (unsigned int j = 0; j < I_conn.getWidth(); j++) {
65  if (labels[i][j] != 0) {
66  I_conn[i][j] =
67  vpRGBa(vpColor::getColor((unsigned int)labels[i][j]).R, vpColor::getColor((unsigned int)labels[i][j]).G,
68  vpColor::getColor((unsigned int)labels[i][j]).B);
69  }
70  }
71  }
73  d2.init(I_conn, I.getWidth(), 10, "Connected components");
74 
76  vpDisplay::display(I_conn);
77  vpDisplay::displayText(I_conn, 20, 20, "Click to quit.", vpColor::red);
79  vpDisplay::flush(I_conn);
80  vpDisplay::getClick(I_conn);
81 
82  return EXIT_SUCCESS;
83 #else
84  (void)argc;
85  (void)argv;
86  return 0;
87 #endif
88 }
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
VISP_EXPORT void connectedComponents(const vpImage< unsigned char > &I, vpImage< int > &labels, int &nbComponents, const vpImageMorphology::vpConnexityType &connexity=vpImageMorphology::CONNEXITY_4)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
static void flush(const vpImage< unsigned char > &I)
Definition: vpRGBa.h:66
static const vpColor red
Definition: vpColor.h:179
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
unsigned int getHeight() const
Definition: vpImage.h:186
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:243
static vpColor getColor(const unsigned int &i)
Definition: vpColor.h:248
unsigned int getWidth() const
Definition: vpImage.h:244