Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
tutorial-connected-components.cpp
1
2
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)
13#include <visp3/imgproc/vpImgproc.h>
15#endif
16
17int 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))
22
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(HAVE_OPENCV_HIGHGUI)
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#else
82 (void)argc;
83 (void)argv;
84#endif
85 return EXIT_SUCCESS;
86}
static vpColor getColor(const unsigned int &i)
Definition vpColor.h:307
static const vpColor red
Definition vpColor.h:211
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:132
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
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 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
VISP_EXPORT void connectedComponents(const vpImage< unsigned char > &I, vpImage< int > &labels, int &nbComponents, const vpImageMorphology::vpConnexityType &connexity=vpImageMorphology::CONNEXITY_4)