Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-connected-components.cpp
1
2
3#include <cstdlib>
4#include <iostream>
5#include <visp3/core/vpConfig.h>
6#include <visp3/core/vpImage.h>
7#include <visp3/gui/vpDisplayFactory.h>
8#include <visp3/io/vpImageIo.h>
9
10#if defined(VISP_HAVE_MODULE_IMGPROC)
12#include <visp3/imgproc/vpImgproc.h>
14#endif
15
16int main(int argc, const char **argv)
17{
19#if defined(VISP_HAVE_MODULE_IMGPROC) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
21
22#ifdef ENABLE_VISP_NAMESPACE
23 using namespace VISP_NAMESPACE_NAME;
24#endif
25
26 std::string input_filename = "img.pgm";
28
29 for (int i = 1; i < argc; i++) {
30 if (std::string(argv[i]) == "--input" && i + 1 < argc) {
31 input_filename = std::string(argv[i + 1]);
32 }
33 else if (std::string(argv[i]) == "--connexity" && i + 1 < argc) {
34 connexity = (vpImageMorphology::vpConnexityType)atoi(argv[i + 1]);
35 }
36 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
37 std::cout << "Usage: " << argv[0]
38 << " [--input <input image>] [--connexity <0: 4-connexity, "
39 "1: 8-connexity>] [--help]"
40 << std::endl;
41 return EXIT_SUCCESS;
42 }
43 }
44
47 vpImageIo::read(I, input_filename);
49
50#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
51 std::shared_ptr<vpDisplay> display = vpDisplayFactory::createDisplay(I, 0, 0, "Input image");
52#else
53 vpDisplay *display = vpDisplayFactory::allocateDisplay(I, 0, 0, "Input image");
54#endif
55
57 vpImage<int> labels;
58 int nbComponents = 0;
59 VISP_NAMESPACE_NAME::connectedComponents(I, labels, nbComponents, connexity);
60 std::cout << "nbComponents=" << nbComponents << std::endl;
62
64 vpImage<vpRGBa> I_conn(I.getHeight(), I.getWidth());
65 for (unsigned int i = 0; i < I_conn.getHeight(); i++) {
66 for (unsigned int j = 0; j < I_conn.getWidth(); j++) {
67 if (labels[i][j] != 0) {
68 I_conn[i][j] =
69 vpRGBa(vpColor::getColor(static_cast<unsigned int>(labels[i][j])).R,
70 vpColor::getColor(static_cast<unsigned int>(labels[i][j])).G,
71 vpColor::getColor(static_cast<unsigned int>(labels[i][j])).B);
72 }
73 }
74 }
76#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
77 std::shared_ptr<vpDisplay> display2 = vpDisplayFactory::createDisplay(I_conn, I.getWidth(), 10, "Connected components");
78#else
79 vpDisplay *display2 = vpDisplayFactory::allocateDisplay(I_conn, I.getWidth(), 10, "Connected components");
80#endif
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
89#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
90 if (display != nullptr) {
91 delete display;
92 }
93
94 if (display2 != nullptr) {
95 delete display2;
96 }
97#endif
98#else
99 (void)argc;
100 (void)argv;
101#endif
102 return EXIT_SUCCESS;
103}
static vpColor getColor(const unsigned int &i)
Definition vpColor.h:300
static const vpColor red
Definition vpColor.h:198
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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:131
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)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.